使用jQuery将变量传递给.php

时间:2015-03-20 17:45:05

标签: javascript php jquery

我正在尝试进行选择并将变量传递给.php页面。我可以使用日期选择器上的jQuery示例来完成它...但无法在菜单选择器上找到它。当我将此控件与datepicker结合使用时,它允许我传递两个变量,但是当我自己使用它时,它不会将其移除。

<meta name="viewport" content="width=device-width">
  <meta charset="utf-8">
  <title>DailyRecords</title>
  <link rel="stylesheet" href="jquery/jquery-ui.css">
  <script src="jquery/js/jquery-1.10.2.js"></script>
    <script src="jquery/jquery-ui.js"></script>

  <html>
  <head>
  <html> 
    <head> 

        <script>
        function showUser(str) {
            if (str == "") {
          document.getElementById("txtHint").innerHTML = "";
    return;
} 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","roster.php?q="+str,true);
    xmlhttp.send();
}
  }
  </script>

  </head>

  <body>
  <script>
  $(function() {
$( "#restaurant" ).selectmenu({

});
    });
</script>

  <table>
  <form>
  <td>
  <select name="restaurant" id="restaurant" onchange="showUser(this.value)">
    <option value="">selection</optoin>
    <option value="101">DA</option>
    <option value="102">FV</option>
    <option value="103">CS</option>
    <option value="104">TO</option>
    </select>
  </td>  
  </form>

  </table>

  <div id="txtHint"><b>Select restaurant</b></div>


  </body>
  </html>

3 个答案:

答案 0 :(得分:3)

使用我的代码它会起作用。但请确保您的jquery文件 jquery / js / jquery-1.10.2.js 在正确的路径中可用吗?

<html>
<head> 
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
<title>DailyRecords</title>
<link rel="stylesheet" href="jquery/jquery-ui.css">
<script src="jquery/js/jquery-1.10.2.js"></script>
<script src="jquery/jquery-ui.js"></script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
}
else
{
$.ajax({

url: "roster.php",
data : "q="+str, //if you want to pass one variable 
//data : "name="+name+"&natives="+natives, //if you want to pass more than 1 variable
type : "POST", // if you want to pass get method put "GET"
success :function(text){
alert(text);
document.getElementById('txtHint').innerHTML=text;
}
});
}
}
</script>
</head>
<body>   
<table>
<form>
<td>
<select name="restaurant" id="restaurant" onchange="showUser(this.value)">
<option value="">selection</optoin>
<option value="101">DA</option>
<option value="102">FV</option>
<option value="103">CS</option>
<option value="104">TO</option>
</select>
</td>  
</form>
</table>
<b>Select restaurant</b> <div id="txtHint"></div>
</body>
</html>

roster.php文件

<?php
echo $_POST['q'];
?>

答案 1 :(得分:0)

因为你使用jquery,你可以像下面这样做:

<script>
function showUser(str) 
{
 var datastring ;
 datastring = "q="+str;
 $.post("roster.php",datastring,function(){
     //your result here
 });
}
 </script>

答案 2 :(得分:0)

使用以下代码

<html>
<head> 
<meta name="viewport" content="width=device-width">
<meta charset="utf-8">
<title>DailyRecords</title>
<link rel="stylesheet" href="jquery/jquery-ui.css">
<script src="jquery/js/jquery-1.10.2.js"></script>
<script src="jquery/jquery-ui.js"></script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
}
else
{
  $.ajax({
     url: "roster.php",
     data: {"q":str}, //if you want to pass one variable 
     type : "POST", // if you want to pass get method put "GET"
     success :function(data){
            console.log(data);
            document.getElementById('txtHint').innerHTML=data;
      }
  });
}
}
</script>
</head>
<body>   
<table>
<form>
<td>
<select name="restaurant" id="restaurant" onchange="showUser(this.value)">
<option value="">selection</optoin>
<option value="101">DA</option>
<option value="102">FV</option>
<option value="103">CS</option>
<option value="104">TO</option>
</select>
</td>  
</form>
</table>
<b>Select restaurant</b> <div id="txtHint"></div>
</body>
</html>

在poster.php中获取帖子值

<?php
 if(isset($_POST['q'])){
     echo $_POST['q'];
 }
?>