I have drop down menu.
If visitor selects certain option from drop down menu I need to show certain content (without page reload; so I need to use Ajax).
At the same time I need save selection of drop down menu after visitor click on Submit button and page reloads.
Below is code.
The first menu remembers selection but does not display content (Ajax does not work).
The second menu only Ajax works, but does not remember selection.
And the third menu remember selection, but something wrong with Ajax.
Could someone advice what to change to get Ajax work and remember selection after page reload?
file.php
file
__gethint.php
If visitor selects certain option from drop down menu I need to show certain content (without page reload; so I need to use Ajax).
At the same time I need save selection of drop down menu after visitor click on Submit button and page reloads.
Below is code.
The first menu remembers selection but does not display content (Ajax does not work).
The second menu only Ajax works, but does not remember selection.
And the third menu remember selection, but something wrong with Ajax.
Could someone advice what to change to get Ajax work and remember selection after page reload?
file.php
Code:
<head>
<script type="text/javascript">
function showData1(str)
{
if (str=="")
{
document.getElementById("showData1").innerHTML="";
return;
}
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("showData1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","__gethint.php?q1="+str,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function showData2(str)
{
if (str=="")
{
document.getElementById("showData2").innerHTML="";
return;
}
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("showData2").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","__gethint.php?q2="+str,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function showData3(str)
{
if (str=="")
{
document.getElementById("showData3").innerHTML="";
return;
}
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("showData3").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","__gethint.php?q3="+str,true);
xmlhttp.send();
}
</script>
</head>
<a name="calculate"></a>
<form method="post" enctype="multipart/form-data" action="<?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?>#calculate" name="quote">
<div id="calculator-left">Salary book
<?php $options = array( 1=>'is submitted', 'is not submitted' ); $showData1 = $_REQUEST['showData1']; ?>
<select name="showData1" onChange="showData1(this.value) style="margin-bottom:3px; width:100px">
<?php foreach ( $options as $i=>$opt ) : ?>
<option value="<?php echo $i?>" <?php echo $i == $showData1 ? 'selected' : ''?>><?php echo $opt ?></option> <?php endforeach; ?> </select>
</div>
<div id="showData1">Use the select box to change page content</div>
<div class="clear"> </div>
<div id="calculator-left">
<select onChange="showData2(this.value)">
<option value="">Select page Content:</option>
<option value="1">Page Content 1</option>
<option value="2">Page Content 2</option>
<option value="3">Page Content 3</option>
<option value="4">Page Content 4</option>
</select>
</div>
<div id="showData2">Use the select box to change page content</div>
<div class="clear"> </div>
<div id="calculator-left">
<select name="town" onChange="showData3(this.value)">
<?php
$towns = array('ipswich','bury');
foreach($towns as $town){
$selected = (isset($_POST['town'])&&$_POST['town']==$town)? '
selected="selected"':'';
print("<option value=\"$town\"$selected>$town</option>");
}
?>
</select>
</div>
<div id="showData3">Use the select box to change page content</div>
<div class="clear"> </div>
<input type = "submit" value = "Calculate"></input>
</form>
</body>
</html>
__gethint.php
Code:
<?php
$q1=$_GET["q1"];
if ($q1==1) {echo "Page Content 1q1 would be shown";}
if ($q1==2) {echo "Page Content 2q1 would be shown";}
if ($q1==3) {echo "Page Content 3q1 would be shown";}
if ($q1==4) {echo "Page Content 4q1 would be shown";}
$q=$_GET["q2"];
if ($q2==1) {echo "Page Content 1q2 would be shown";}
if ($q2==2) {echo "Page Content 2q2 would be shown";}
if ($q2==3) {echo "Page Content 3q2 would be shown";}
if ($q2==4) {echo "Page Content 4q2 would be shown";}
$q=$_GET["q3"];
if ($q3==1) {echo "Page Content 1q3 would be shown";}
if ($q3==2) {echo "Page Content 2q3 would be shown";}
if ($q3==3) {echo "Page Content 3q3 would be shown";}
if ($q3==4) {echo "Page Content 4q3 would be shown";}
?>
Comment