繁体   English   中英

使用jQuery将变量传递给.php

[英]Using jQuery pass a variable to .php

我正在尝试进行选择并将变量传递给.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>

使用我的代码它会工作。 但请确保您的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'];
?>

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

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

使用以下代码

<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'];
 }
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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