使用jQuery将一个变量传递到.php

Using jQuery pass a variable to .php

本文关键字:变量 php 一个 jQuery 使用      更新时间:2023-09-26

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

<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 中的post值

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