0

I am trying to use ajax and pass varuables from js to php in order to insert them in a mysql table. the code below is successful in calling the php script but not passing the data

i would like to pass "num" from the following js function to "insertRow.php" - the function does activate "insertRow.php" but will not pass the data

function InsertToTable(num){
    var data=num;
    $.ajax({
        url: "insertRow.php",
        data: data,
        type: "POST"
        });

once the data is passed i would like to insert it in to a mysql table, the following will work if i don't use "$num" but just put a value instead

<?php
$num = $_Post['data'];
//print_r($_Post("data"));

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Schedule";   

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql ="INSERT INTO `temp1` (`column1`) VALUES ('$num')";

$conn->query($sql);     
?>
4

2 Answers 2

2

You can do this...

data: {data : num},

OR, since you set data = num; you could do this:

data: {data: data},

Where the first data is the identifier and the second is the value.

The will give you access to data in the POST (which is case sensitive) variable:

$_POST['data'] // should be equal to num
Sign up to request clarification or add additional context in comments.

7 Comments

Althought this is legit, data: {data: data} will be very confusing.
Agreed @Cid, because especially in this case num is already set and does not need to be transfigured into another variable.
What about var data = { data:num }; so you can type data: { data: data.data } ?
@Cid LOL ¯\_(ツ)_/¯
¯\_(ツ)_/¯ It works on my computer <- my pre-made comment for some questions
|
0

Seems like a typo,

Insead of

$num = $_Post['data'];

Try

$num = $_POST['data'];

Also, it's good practice to send AJAX a return, so in the end you can use

echo json_encode(true); 

Or set it to false in case of an error, with this you can see if the query worked or not in the success clause of AJAX.

Here's an example of a more complex AJAX call.

            $.ajax({
                url: 'scripts/AjaxHandler/Controller.php',
                type: 'POST',
                dataType: 'JSON',
                data: {
                    action: 'doThis',
                    var1: var1,
                    var2: var2
                },
                success: function (data) {
                    if(data == true) {
                        alert('success');
                    }else{
                        alert('failure');
                    }
                },
                error: function (error) {
                    console.log(error);
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.