HTML button to start a function

Does anyone have some basic examples of using a simple button in html to start a function?

This has a couple of different answers.

The simplest is to just link to the imp agent with a variable attached to the URL.
The agent would GET the value from the URL. Likewise, using an HTML ‘form’ with a submit button could be used. That is how to POST to the agent.

Doing it that way, anyone that views your web page source can see your Imp URL. Not such a good idea to let people know what your Imp URL is … but that is the simplest method.

To hide the Imp URL … now you’re talking about using some PHP on your website to do the communication unseen by the website user. This could even involve a simple ‘login’ to add some security if you wish.

So, I guess you have to pick your poison and define some more specific requirements.

For the long run, it would be good to hide the URL in php.

Let’s start with the PHP script to POST a value to the agent.

You can put in your own Imp URL and upload it to your website. Call it “send.php”

For now, it will simply send “ON” to your agent. Just a test to make sure your PHP and CURL is functioning.

You can run it from your browser ( and see what your agent gets.
If you don’t have anything in your agent, see the bottom of this post.


//enter YOUR URL here
$url = ‘’;

//we’ll just send a variable named ‘data’ and a value of ‘ON’

$fields = array(
foreach($fields as $key=>$value) { $fields_string .= $key.’=’.$value.’&’; }
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$result = curl_exec($ch);


This would be an example agent code for the example PHP script above…

http.onrequest(function(req, resp){ if (req.method == "POST"){ local body = http.urldecode(req.body); server.log(; //device.send("data",; } resp.send(200, "OK"); });

If you can see the value “ON” within your agent server log, we can move on to the next part.

Check out the Snackman code in the Electric Imp GitHub page.