-4

How to pass a variable from js file to php file.i want variable from checker.js to test.php

function SendData(id){
    $.ajax({
        type: "POST",
        url: "test.php",
        data: "id=" + id,
        cashe: false,
        success: function(response){
            alert("Record successfully updated")
        }
    })
}
$(document).ready(function(){
    SendData(10)
})

test.php file

<?php
var_dump($_POST);
?>

index.html

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="checker.js"></script>
</head>
<body>
</body>
</html>
3
  • 1
    "how to get a variable in php file from js?" — Err… see the code in your question? Like that.
    – Quentin
    Oct 29, 2012 at 13:53
  • 5
    what happens when change alert("Record successfully updated") to alert(response) ? You haven't provided very much information to know what works or doesn't. Your code shown should work if path to server file is correct
    – charlietfl
    Oct 29, 2012 at 13:54
  • 2
    @user1783058 — How can you tell? You aren't doing anything with the output in your JS.
    – Quentin
    Oct 29, 2012 at 14:03

2 Answers 2

0

Try this and see what is logged in your console:

function SendData(id){
    $.ajax({
        type: "POST",
        url: "test.php",
        data: "id=" + id,
        cache: false,
        success: function(response){
            console.log(response);
        }
    });
}
$(document).ready(function(){
    SendData(10)
});

I am almost certain something is being returned by the server, you're just not looking for it.

If you want to retrieve your ID, you could change your php to:

<?php
var_dump(json_encode($_POST));
?>

and your js to:

function SendData(id){
    $.ajax({
        type: "POST",
        url: "test.php",
        data: "id=" + id,
        cache: false,
        success: function(response){
            var id = JSON.parse(response).id;
            //do something with id
        }
    });
}
$(document).ready(function(){
    SendData(10)
});
2
  • array(1) { ["id"]=> string(2) "88" } there is! and how then to get this id?
    – Dima
    Oct 29, 2012 at 14:19
  • @user1783058 I have added how to retrieve the id. Oct 29, 2012 at 14:38
0

You have a syntax error (or unknown key) in SendData function. Change cashe into cache.

And you can't see what the PHP has code written. alert(response);

If you want to replace the content of the index.html to the response of the test.php use:

function SendData(id){
    $.ajax({
        type: "POST",
        url: "test.php",
        data: "id=" + id,
        cache: false,
        success: function(response){
            $("body").html(response);
        }
    })
}
$(document).ready(function(){
    SendData(10)
})
5
  • 3
    That's not technically a syntax error. I assumed jQuery ignored unknown keys in the object passed? Oct 29, 2012 at 13:56
  • 1
    agree with typo but that shouldn't break the code eiether
    – charlietfl
    Oct 29, 2012 at 13:57
  • Okay, but how do I know then that in a post array? me in that file to execute the query to the database of test.php
    – Dima
    Oct 29, 2012 at 13:59
  • Do you want to see the executed PHP response on the whole page?
    – androbin
    Oct 29, 2012 at 14:01
  • @Asad Ya but by default cache is set to true so it could break expected behaviour
    – A. Wolff
    Oct 29, 2012 at 14:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.