return false

The problem in my script is that it`s redirecting me to the php file instead of only executing it in the background. Prob a simple solution for you guys :D. So it should only trigger the .php file (which turns on a light in the house).

<ul data-role="listview" data-inset="true">
  <li>
  <label for="slider"></label>Spot
  <select name="slider" id="slider" data-role="slider" ONCHANGE="location = this.options[this.selectedIndex].value;  ">
    <option value="schakelen.php?kanaal=a3&actie=uit&optie=0">OFF</option>
    <option value="schakelen.php?kanaal=a3&actie=aan&optie=0">ON</option>
  </select>
</li>

As you discovered, the problem with the method you are using is that it doesn’t execute your remote script (schakelen.php), it actually redirects your browser to it. What you are trying to do is to effectively stay on the same page, but send $_GET parameters to the script.

The first thing you will need to do is to put your select in an html form and set the method to post. You will need to pass the changed value to a php form handler that you will add to the same page as your form. You can then change your ONCHANGE to submit the form, instead of redirecting the browser. This will cause the browser to reload the current page, having passed the new slider value in the post.

It is tempting to try to include (or require) the schakelen.php script with the appropriate url for the desired $_GET variables; however, this will fail, as neither “include” nor “require” will allow you to append the extra GET elements to the url.

The answer is to use curl. The challenge with curl is that you cannot use a relative path, so you will either need to use an absolute path, or your can get the current directory using the $_SERVER array - as I have demonstrated in the following code.

I would also recommend that you set the new slider value to the default in the html select.

Here is how I put it together:[php]

Light Controller <?php $off = $on = ''; $prefix = dirname($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']); if(!empty($_POST['slider']) && $_POST['slider'] == "OFF") { $off=' selected="selected" '; myCurl($prefix.'/schakelen.php?kanaal=a3&actie=uit&optie=0'); } elseif(!empty($_POST['slider']) && $_POST['slider'] == "ON") { $on=' selected="selected" '; myCurl($prefix.'/schakelen.php?kanaal=a3&actie=aan&optie=0'); } function myCurl($url) { $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL, $url); curl_exec($curl_handle); curl_close($curl_handle); } ?>
  • Spot >OFF >ON
[/php]

There are certainly other (and probably better) ways of doing this, but this should work. I suspect AJAX would be the best option, but I do not have enough knowledge with it to figure this one out.

I have tested the above script and am confident it will work, but you may need to make a few changes for your specific situation. Let me know if you have any difficulties and I’ll be glad to help.

Jay

Sponsor our Newsletter | Privacy Policy | Terms of Service