简体   繁体   中英

AJAX dropdown menu selection

I have an AJAX dropdown menu that present a range of options for the user based on three different parameters - once these are selected, a list is generated below.

As the user clicks and item, he is taken to:

legat.php?id=[number] 

My problem is, that when the user presses the " go back " button in the browser, the AJAX list does no longer show the generated list.

The inputs ("dropdown selections ") in the dropdowns are the same, but the list below is not generated.

This is big usability flaw, since the user will then have to select the three parameters again.

How do I solve this?

JavaScript:

function findLegater(val1, val2, val3) {

var stu = document.getElementById(val1).value
var ud = document.getElementById(val2).value
var kva = document.getElementById(val3).value

if (stu=="N" && ud =="0" && kva =="0") {
document.getElementById("txtHint").innerHTML="";
}
else {
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("txtHint").innerHTML=xmlhttp.responseText;
    }
}

xmlhttp.open("GET","getlegat.php?s="+stu+"&u="+ud+"&k="+kva,true);
xmlhttp.send();
}
}

PHP (to receive input):

$s=$_GET["s"];
$u=$_GET["u"];
$k=$_GET["k"];

include 'msql/msql-connect.php'; 

if ($s!= "N" && $u != "0" && $k!="0") {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%' and studie LIKE '%$u%' and                  kvartal LIKE '%$k%'";
}
else if ($s!= "N" && $u == "0" && $k=="0")  {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%'";
}
else if ($s!= "N" && $u != "0" && $k=="0")  {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%' and studie LIKE '%$u%'"; 
}
else if($s== "N" && $u != "0" && $k=="0")  {
$query = "SELECT * FROM legater WHERE studie LIKE '%$u%'";
} 
else if($s== "N" && $u != "0" && $k!="0")  {
$query = "SELECT * FROM legater WHERE studie LIKE '%$u%' and kvartal LIKE '%$k%'"; 
} 
else if($s== "N" && $u == "0" && $k!="0")  {
$query = "SELECT * FROM legater WHERE kvartal LIKE '%$k%'";
} 
else if($s!= "N" && $u == "0" && $k!="0")  {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%' and kvartal LIKE '%$k%'"; 
} 

// Execute query
$result = mysql_query($query) or die ("Error in" . $query);
$number = mysql_num_rows($result);

// Check result
if ($number == 0) {
  echo "Ingen legater fundet";
}
else {
  while ($row = mysql_fetch_array($result)) {
    echo "<li><a href=\"legat.php?id=" . $row['id'] . "\">" . $row['navn'] . "</a>    </li>";
  }
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

This is not a 'new' problem at all. This problem has been discovered by many people, and fixed in many places.

This will lead to many different solutions: Making Browser Back button work while using AJAX requests

您可以为此使用会话 ...当用户从下拉列表中进行选择时,将其保存到会话.. 在页面加载时, 从会话获取ID并将其发送到服务器,然后在会话结束后获取结果。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM