0

At the moment I have the following markup code on my website:

<select>
   <option>1</option>
   <option>2</option>
   <option>3</option>
</select>

I want to have a PHP script in the same directory as my webpage fire when I select a particular option. How would I go about doing this? Thank you.

4
  • 2
    why don't you use ajax? Commented Apr 14, 2014 at 6:51
  • There are so many different ways to do this that it's a little hard to give you just one answer. You could wrap it in a form and use POST or you could use AJAX... Commented Apr 14, 2014 at 6:52
  • A-J-A-X Commented Apr 14, 2014 at 6:54
  • Sorry guys, I should have mentioned that I don't have a hell of a lot of experience with web development (mainly an application developer) so I am liable to say stupid things at times :P Commented Apr 14, 2014 at 6:59

2 Answers 2

2

call a ajax on change of select like this

<select onchange="javascript:callAjax(this.value);">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
</select>

<script>
function callAjax(sel_opt)
{
   var url = "your_php_file.php?select_option="+sel_opt;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            var result = xmlhttp.responseText;
            // put your result where ever you want
        }
    }

    xmlhttp.open("GET",url,true);

    xmlhttp.send();
}
</script>

your_php_file.php

<?php

// your code

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry to the above comments, I mainly do application programming and I have just started doing a lot more web programming work, so I wasn't really aware of Ajax. New experiences everywhere :P
1

Do an Ajax call. Here's an example. hope this helps to understand.

cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.