ram esh
ram esh

Reputation: 259

AJAX pass variable from javascript to PHP

I am passing a variable from javascript to another PHP page via ajax. I am not able to do it. This is the code I have so far.

<script>
function loadXMLDoc()
        {
        $.ajax({
                    type: "POST",
                    url: "update.php",
                    data: { value : masterdata },
                    success: function(data)
                    {
                        alert("success!");
                    }
                }); 

}
$("button").on('click',function(){ loadXMLDoc(); });
</script>

The masterdata variable comes from another javascript function in which I have declared the variable globally, as in this below function.

<script>

    var masterdata;      
    $("[data-slider]")

       .each(function () {
           var range;
            var input = $(this);
            $("<span>").addClass("output")
                .insertAfter(input);
            range = input.data("slider-range").split(",");
            $("<span>").addClass("range")
                .html(range[0])
                .insertBefore(input);
            $("<span>").addClass("range")
                .html(range[1])
                .insertAfter(input);
        })
        .bind("slider:ready slider:changed", function (event, data) {
            $(this).nextAll(".output:first")
                .html(data.value.toFixed(2));
                masterdata = data.value;

        });


</script>

In my update.php file, I am trying to access the variable using $_REQUEST. The code is as below.

<?php

    $uid = $_REQUEST['value'];
    echo "Am I getting printed";
    echo $uid;
    // Do whatever you want with the $uid
?>

However, if I click on Update button nothing happens. I just get an alert saying Success for my ajax call. Can someone please help me out?

Upvotes: 3

Views: 5526

Answers (2)

Terry
Terry

Reputation: 66103

I would recommend JSON-encoding the output you have obtained from your update.php script, and pass it back to the AJAX function. In your update.php file, you can simply use the PHP function json_encode($var), and echo it:

<?php
    $uid = $_REQUEST['value'];
    echo json_encode($uid);
?>

From your AJAX function, you should specify the dataType (usually JS will intelligently guess the type of data returned, but we should always specify it just in case):

function loadXMLDoc() {
    $.ajax({
        type: "POST",
        url: "update.php",
        data: { value : masterdata },
        dataType: "json",
        success: function(data) {
           // Log data into console for reference (and checking, perhaps)
           console.log(data);

           // Parse data here
        }
    }); 
}

Upvotes: 0

Labib Ismaiel
Labib Ismaiel

Reputation: 1340

in your success function, you're not asking it to display the returned data that's why you are only seeing the alert, change it to:

success: function(data)
   {
     alert(data);
   }

because data here is the values returned from your request

and for the php part, keep in mind one thing, just adjust your php page to display the data you need, and remember it doesn't matter how you make this data, with echo or just plain html, the result of the php code is what you will get in your variable in ajax. and if you go JSON, you can do in the php page:

$uid = $_REQUEST['value'];
$x.uid = $uid;
$x.something = "something";
return $x;

this way you will have all the data you need on client side as an object and then you can manipulate it the way you like, in a much more compact way, just search for RESTful api if you want to read more about the topic

Upvotes: 1

Related Questions