[英]Passing multiple parameters to PHP through AJAX
我正在尝试使用在JavaScript上创建的变量来更新表。 我正在尝试使用ajax发送信息。 由于某种原因,每当我运行代码时,表就会更新为空白,而不是使用我在JavaScript上创建的VARIABLES。 有什么可能的原因的想法吗?
PHP:
$stmt = $conn->prepare("UPDATE seats SET firstClass=? , economicClass=? WHERE 1");
$stmt->bind_param("ss", $first, $eco);
$eco = $_POST["eco"];
$first = $_POST["first"];
JAVASCRIPT / AJAX:
var seatsInEconomicClass = 0;
var seatsInFirstClass = 0;
function ajaxWay() {
$.post("reservePhp.php",
{
data : {eco:"seatsInEconomicClass",first:"seatsInFirstClass"}
}, display);
}
您可以按以下方式使用jquery AJAX并按如下所示传递多个参数:
$.post('reservePhp.php',{param1:value,param2:value,...},function(data){
//data contains the response on which you can perform any type of action on the html page
});
您接近:
var seatsInEconomicClass = 0;
var seatsInFirstClass = 0;
function ajaxWay() {
$.post("reservePhp.php", { eco: seatsInEconomicClass, first: seatsInFirstClass}).done(function(data){
//code to run after the ajax call
});
}
$.post( "test.php", { name: "John", time: "2pm" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
这里的“ John”和“ 2pm”是实际值。 而在您的代码中,您正在使用变量。 如果将它们放在quotes
它们本身将被视为字符串,并且它们的值不会到达php。 到达的是这些变量的名称作为字符串。
在您的PHP代码中更改这些行的顺序。
$stmt = $conn->prepare("UPDATE seats SET firstClass=? , economicClass=? WHERE 1");
$eco = $_POST["eco"];
$first = $_POST["first"];
$stmt->bind_param("ss", $first, $eco);
首先,您需要初始化,然后按照@Thamilan的注释中的说明进行绑定
you have need to add a get or post method for this ajax call
你可以用这个
var seatsInEconomicClass = 0;
var seatsInFirstClass = 0;
$.ajax({
type:"post",
url:"reservePhp.php",
data:{"seatsInEconomicClass": seatsInEconomicClass,"seatsInFirstClass": seatsInFirstClass},
success: function(response){
alert(response);
}
});
您可以使用以下方法在reservePhp.php上访问这些变量:
<?php
$seatsInEconomicClass = $_POST['seatsInEconomicClass'];
$seatsInFirstClass = $_POST['seatsInFirstClass'];
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.