0

I am trying to send information using AJAX from JavaScript in one file to PHP in another file. When I inspect the page on chrome after I have pressed the button, it shows the values under the form data sub section in networks, however, if I var_dump[$_POST] it shows it is empty.

Here is my JavaScript:

function details(id) {
    var data = {"id" : id};

    jQuery.ajax({
        url: "/Example/includes/detail.php",
        type: 'POST',
        data: data,
        success: function(data){
            jQuery('body').append(data);
        },
        error: function(){
            alert("Error");
        }
    }); 
}

Here is the PHP:

<?php
$id = $_POST['id'];
$id = (int)$id;
?>

Any help would be greatly appreciated. Thanks

BBill
  • 23
  • 4
  • Does this answer help? http://stackoverflow.com/questions/42677423/sending-object-data-from-ajax-to-php/42677898#42677898 – John Pavek Mar 13 '17 at 19:47
  • 1
    Possible duplicate of [Sending Object Data from AJAX to PHP](http://stackoverflow.com/questions/42677423/sending-object-data-from-ajax-to-php) – John Pavek Mar 13 '17 at 19:48
  • `var_dump[$_POST]` is incorrect, use normal brackets instead of square brackets, or better `print_r($_POST);`. The rest of your code look good, aside from the fact that a closing curly bracket is missing at the end. – Mario A Mar 13 '17 at 20:05
  • @JohnPavek unfortunately it doesnt work, I have tried it and I just get a 0 which I know is not what is being passed. – BBill Mar 13 '17 at 20:38
  • when is the js code called (i.e. `details(id)`)? How is your code different than [this phpfiddle](http://phpfiddle.org/main/code/v8am-74w5) (besides the fact that it prints the $id value with `echo` and the AJAX request is triggered by the button click)? – Sᴀᴍ Onᴇᴌᴀ Mar 13 '17 at 21:19
  • The js code is called on button click as well. The only difference between the code in the phpfiddle and what I want to do is that the js and php are not in the same file i.e. one php file has the js and another has the php. – BBill Mar 14 '17 at 13:59

0 Answers0