i have a html file like that:
<?
$q=$_REQUEST['???'];//sth. like that
echo $q;
?>
<form>
<select name="users" onchange="showUser()">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
and i want to use the selected by onchange value in the same page in php block.
i only want to select some of the options and hold it a variable in php like $q as i indicated upon. how can i do that in the same page?
You need to have a function in javascript which will handle AJAX request
your onchange=""
function call will have the argument as this.value
which means it will carry the value selected in select box to javascript function
In javascript function showUser it will create a xmlhttp object to execute the ajax methods " open()
" and " send()
"
like:
if (window.XMLHttpRequest)
{ //For Other browsers
xmlhttp=new XMLHttpRequest();
}
else
{ //For internet Explorer
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Now a function is created which needs to be executed when response is ready as
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("yourcontainerID").innerHTML=xmlhttp.responseText;
//to display the response in the html container
}
}
Now a request is sent php file and parameter can be obtained using url query string or post variable as well using open()
method
here let assume we are using url query string hence,
xmlhttp.open("GET","urphpfile.php?q="+str,true);
xmlhttp.send();
not to recieve the "q" variable in php file you can do
$q=$_GET["q"];// here you may use $_REQUEST["q"] which can hadle both $_POST and $_GET
in ur php file....
and you are done....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.