I have to get selected value user selected from a drop down list in an HTML form in a php variable. I find many javascripts for that but all of them are straight printing value on any event. I need to get back selected value in a php variable so that I could use it. And also because the control goes to javascript like in the script underneath so any php statement after dropdown list in this kind of file is not executed. How could I do it.
<html>
<head>
<title>The Basics</title>
<head>
<script type="text/javascript">
function disp_text()
{
var w = document.myform.mylist.selectedIndex;
var selected_text = document.myform.mylist.options[w].text;
alert(selected_text);
}
</script>
</head>
<body>
<FORM NAME="myform">
<SELECT NAME="mylist" onChange="disp_text()">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
</FORM>
</body>
</html>
No, it doesn't work. I opened the HTML form on local host & select one option. But there was no value in GetTitle.php. Is not like that as control doesn't transfer from HTML form to php file so when php is opened seperately, there is no value in GET? Can't you specify some method through which I could get value on same page as I have to POST another value by same form. I don't know yet if I can receive more then 1 value via GET or POST but in case, I can use SESSION variables if I get SELECT value on same page.
@DzSoundNirvana:
I wanted to pass the value of a JS variable to a php variable on the same page without having a page refresh or calling a 2nd php script. I didnt wanna do a page refresh. This solution do not do that. If it could be done without using Javascript(i.e. only using HTML <SELECTt>) then its even more preferable as at the end I need the selected value of a dropdown list on same HTML/PHP page in a php variable.
<html>
<head>
<title>The Basics</title>
<head>
<script type="text/javascript">
function disp_text()
{
var w = document.myform.mylist.selectedIndex;
var selected_text = document.myform.mylist.options[w].text;
alert(selected_text);
}
</script>
</head>
<body>
<FORM NAME="myform">
<SELECT NAME="mylist" onChange="disp_text()">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
</FORM>
</body>
</html>
No, it doesn't work. I opened the HTML form on local host & select one option. But there was no value in GetTitle.php. Is not like that as control doesn't transfer from HTML form to php file so when php is opened seperately, there is no value in GET? Can't you specify some method through which I could get value on same page as I have to POST another value by same form. I don't know yet if I can receive more then 1 value via GET or POST but in case, I can use SESSION variables if I get SELECT value on same page.
@DzSoundNirvana:
I wanted to pass the value of a JS variable to a php variable on the same page without having a page refresh or calling a 2nd php script. I didnt wanna do a page refresh. This solution do not do that. If it could be done without using Javascript(i.e. only using HTML <SELECTt>) then its even more preferable as at the end I need the selected value of a dropdown list on same HTML/PHP page in a php variable.