Rene Roger Rene Roger - 4 months ago 8
PHP Question

Passing multiple parameters to PHP through AJAX

I am trying to update a table using variables that I created on my JavaScript. I am trying to use ajax to send the information. For some reason whenever I run the code the table is updated with blank and not with the VARIABLES that I created on JavaScript.
Any ideas of what could be causing it?

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

Answer

You're close:

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

From jQuery API

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

here "John" and "2pm" are actual values. whereas in your code you're using variables. if you put them in quotes they'll be treated as strings themselves and their value won't reach to php. what'll reach is the name of those variables as strings.