0

I want to use the result value and pass it to php variable, here is my code...

billingCoffee.php

$("#linkAddSize").click(function(e){
            e.preventDefault();
            var txtCoffeeName = document.getElementById("txtCoffeeName").value;
            var cmbSizes = document.getElementById("cmbSizes").value;
            var txtPrice = document.getElementById("txtPrice").value;
            $.ajax({
                url: "addSizeandPrice.php",
                type: "POST",
                data: {coffeename: txtCoffeeName, sizes: cmbSizes, price: txtPrice},
                datatype: "json",
                success: function (result){
                    //set it php variable
                }
            });
        });

addSizeandPrice.php

    if($tableresult){
        $query = "INSERT INTO tbl$CoffeeName (CoffeeSize, Price) VALUES ('$Size', '$Price');";
        $insertresult = mysqli_query($con, $query);

        if($insertresult){
            SESSION_START();
            $_SESSION['nameCoffee'] = $CoffeeName;
            echo $_SESSION['nameCoffee'];
        }

        else{
            echo "Something went wrong!";
        }
    }

I want to use the variable without refreshing the page... and I got this idea to use AJAX but don't know how to set it in php variable.

bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • You're going to have to be a bit more specific. There are a number of variables in play, and it's not clear what variable (or even what kind of variable - query parameter, JavaScript variable, PHP variable, etc) you're referring to. – skeggse Aug 22 '15 at 18:35
  • 1
    Get the *values* supplied in the POST AJAX - which is just another HTTP request - data via `$_POST` (if this was a GET request it would be `$_GET`, naturally.) – user2864740 Aug 22 '15 at 18:35

1 Answers1

0

You are using POST as the method to send variables to your PHP script. So in PHP, they will be in the superglobal named $_POST

For example,

$coffeename = $_POST['coffeename'];

Further reading: http://php.net/manual/en/reserved.variables.post.php

Please also do some research about preventing SQL injection.

Community
  • 1
  • 1
rjdown
  • 9,162
  • 3
  • 32
  • 45