通过AJAX将多个参数传递给PHP

时间:2016-08-09 04:46:09

标签: javascript php ajax database

我正在尝试使用我在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);
    }

4 个答案:

答案 0 :(得分:1)

您可以按如下方式使用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
});

答案 1 :(得分:1)

你很亲密:

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
        });
    }

来自 jQuery API

 $.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

这里“John”和“2pm”是实际值。而在你的代码中你使用变量。如果你把它们放在quotes中,它们将被视为字符串本身,它们的价值将无法达到php。这些变量的名称是字符串。

答案 2 :(得分:0)

在这些行的PHP代码更改顺序中。

$stmt = $conn->prepare("UPDATE seats SET firstClass=? , economicClass=? WHERE 1");

$eco = $_POST["eco"];
$first = $_POST["first"];
$stmt->bind_param("ss", $first, $eco);

首先,您需要初始化然后按照@Thamilan

的评论中所述绑定它们

答案 3 :(得分:0)

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

?>
相关问题