dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Sending JQuery object via Ajax to PHP problem

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    8

    Sending JQuery object via Ajax to PHP problem

    HI all,
    I have problem to understand how sending and comunication between jqeury and php goes. I have simple task. I need to send json object to php and than do some stuff with it (json_decode, make it XML, save to database...). I have index.php with button SAVE (id = "save") and external .js file:
    ...
    var obj; //there is something in

    $("#save").on('click', function(){
    $.ajax({
    type: "POST",
    url: "./save.php",
    data: {"obj"bj},
    success: function()
    {
    alert("Changes saved.");
    }
    });
    });

    Third file is simple external php file (save.php):

    <?php

    $obj = json_decode($_POST['xmlObject']);
    echo $obj;

    ?>


    The strange thing is that alert("Changes saved.") occures, but there is no evidence of processing echo $obj.
    Please tell me what I am doing wrong :/. One thing I dont really understand is how php file knows when it should be executed.

    Thanks a very lot for any answer.

  2. #2
    Join Date
    Jan 2013
    Posts
    2
    So set up a var

    var obj = 'Test Data'; ///Or use a input field like this $("#someid").val();

    $.post('./save.php', {xmlObject: obj}, function(data) {
    alert(data);//this will return data back from the PHP file
    });


    Now the php file

    <?php

    $obj = $_POST['xmlObject'];

    if (isset($obj)) {

    echo $obj;

    }



    Hope this helps

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles