Rectangle 27 0

php Displaying data from the database when user selects single value?


<SELECT name='name' id='123' onchange='userhint(this.value)'><OPTION value='usercode'...
function userhint(str)
{

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)
    {
    document.getElementById("changingdiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","myphpfile.php?q="+str,true);
xmlhttp.send();
}

As your select. The key is putting the onchange call in the SELECT.

Depending on how you want to do it, the php can either create the complete HTML of the new div or return a json string which you can then use to populate your original areas. For a beginner, the first option is definitely easiest (and is expected by the very basic js above) but the json method will use a fair bit less bandwidth and makes it easier to re-use the same file to feed multiple pages.

Fundamentally, what you need is:

This can best be achieved using AJAX. There is a tutorial on w3schools.com which explains how ajax works. You will simply have to modify the code they supply.

This will replace the content of 'changingdiv' with whatever myphpfile.php returns in response to a get request of q. You are clearly capable of writing that file yourself.

You will also need a form goes here

Note