使用jQuery将一个变量传递到.php
Using jQuery pass a variable to .php
我正在尝试进行选择并将变量传递到.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'];
}
?>
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 通过javascript重定向html传递php变量
- 如何在php变量中嵌入JQuery代码
- 在javascript函数中使用php变量
- 将地理编码结果转换为php变量以发布到mysql数据库
- JavaScript,PHP-用JavaScript将多个数据库记录发送到变量或表
- 如何将变量php传递给javascript
- 显示动态变量(php和javascript)的总数
- 传递变量PHP、Javascript location.reload和HTML输入
- 需要将变量 php 的值传递给 javascript,但我不能
- 将变量从 .html 传递到太大而无法容纳在 URL 中的变量.php
- 如何在函数中传递javascript中的变量php
- 在Jquery中插入变量PHP
- 选择正确的变量 php/javascript
- 将HTML存储在变量PHP中,然后回显它
- 将变量PHP Smarty传递给Javascript
- 传递HTML表单post数据到不同的文件/网页javascript变量(php)
- 问题与传递变量(php, sql, jquery拖拽)
- 设置PHP中的javascaript变量(PHP在javascript中)
- 将变量php问题化为代码jquery