1

I need pass jQuery variable to PHP variable, so I made simple code to test and it doesn't work.

It's only a testing files. Finally, I have to do something more advanced but i can't make it works!

I have 3 files

In character.html I have:

    <a href="character-save.php" class="saved">SAVE</a>

and in character.js (it's external javascript for character.html)

    $(document).ready(function() {
    $('.saved').click( function () {
    var avatarID = 123;
    $ajax({
    url :'character-save.php',
    data:{"avatarID":avatarID},
    type: 'post',
    dataType: 'json',
    });
    });
    });

in character-save.php i try this:

    <?php
    header('Content-type: application/json');
    $result = $_POST['avatarID'];
    $result = htmlentities($result, UTF-8);
    echo json_encode($result);
    exit();
    ?>

And it doesn't print 123

3
  • 1
    First - 'UTF-8'. Second - what does it print? (though I guess it prints nothing) Commented Nov 30, 2013 at 10:30
  • how are you reading back ajax post result? Commented Nov 30, 2013 at 10:36
  • ok, my mistake. I've removed 'UFT-8' and I used the solution given below to get response alert. Nothing has changed except that character-save.php gives me 'null'. I'm really newone in ajax solutions. Commented Nov 30, 2013 at 10:56

4 Answers 4

2

In your php file, you have a mistaks with UTF-8, it should be included with parentheses.

<?php
   header('Content-type: application/json');
   $result = $_POST['avatarID'];
   $result = htmlentities($result, 'UTF-8');  // It should be 'UTF-8'
   echo json_encode($result);
   exit();
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Your syntax is wrong its not $ajax its $.ajax don't forget the . dot.

Also you need some way of checking response so either update your html by adding ajax callback and necessary jquery, use alert or log reponse to console. Something like this should give you a good indication.

$.ajax({
    url :'character-save.php',
    data:{"avatarID":avatarID},
    type: 'post',
    dataType: 'json'
}).done(function(response) {
    alert(response.data);
});

In your PHP change $result = htmlentities($result, UTF-8); to $result = htmlentities($result); also validate your json by putting result in an array then encode that array as json and echo it like this:

<?php
header('Content-type: application/json');
$result = $_POST['avatarID'];
$result = htmlentities($result);
$return["data"] = $result;
echo json_encode($return);
exit();
?>

5 Comments

My syntax it's good, i made mistake only here. Sorry. I try this and another alerts, nothing worked.
what browser are you using?
I made it, it gives only "" and I use Firefox.
its returning null because you are returning only 123 which is not proper json put result in an array and then receive it, check my updated answer.
welcome, next time try using network tab of developer tools its available in chrome and firefox there you can monitor ajax requests their parameter, status, response and almost everything.
0

In your javascript you have to add e.preventDefault(); to prevent the page redirecting to character-save.php

And inside the click function add e

see the updated javascript section below

$(document).ready(function() {
$('.saved').click( function (e) {
    e.preventDefault();
    var avatarID = 123;
    $.ajax({
        url :'character-save.php',
        data:{"avatarID":avatarID},
        type: 'post',
        dataType: 'json',
        success: function(msg){
            alert(msg);
        }
    });
});

});

Comments

0

Try this,

$(document).ready(function() {
$('.saved').click( function () {
  var avatarID = 123;
  $ajax({
    type:"POST",
    url: 'character-save.php&avatarID='+avatarID ,
    success:function(response){
       alert(response);
    });
});
});

In character-save.php try this:

 <?php  echo $_GET['avatarID'];
 ?>

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.