markovchain
markovchain

Reputation: 503

Passing this javascript object from jQuery to PHP?

This question stems from this thread.

I've followed the answer below but am having trouble with passing the object into PHP. I think it's only a minor problem but I can't find it.

My ajax call

 $('.actResult').click(function() {
        var result = {};
        $('.actResult tr').each(function(){
            var $tds = $(this).find('td');
            result[$tds.eq(0).html()] = $tds.eq(1).text();
        });
        console.log(result);
        $.ajax({
            type: 'get',
            url: 'userpage.php',
            data: result
        });
        $('.FindResults').dialog("close");
    });

In userpage.php, I'm using this:

echo '<div id="data"><pre>', var_dump($_GET), '</pre></div>';

Possibly I might need to use stringify or json_decode, but this source tells me it's enough to do an ajax call.

The output is giving me an

array(0){
}

Which is strange. The array prints into the console so it's generated properly. The console also tells me the ajax is executed successfully. I'm using $_GET just because $_POST already has so many variables, it's easier to inspect $_GET for this request.

UPDATE:

From the comments below, the ajax call doesn't do anything when the query is successful. So I changed the call:

$.ajax({
            type: 'get',
            url: 'userpage.php',
            data: result,
            success: function(){
                $('#data').text( data );
            }
        });

And the PHP

echo '<input type="text" id="data" /><pre>', var_dump($_GET), '</pre>';

I tried it with a div instead of a textbox. The result still is array(0){}

Upvotes: 0

Views: 96

Answers (3)

Luca Filosofi
Luca Filosofi

Reputation: 31173

  $.ajax({
    type: 'GET',
    url: 'userpage.php',
    data: result,
    dataType : 'json',
    success: function(data){
     console.log(data);
   },
   error: function(jqXHR, textStatus){
    console.log(jqXHR);
     console.log(textStatus);
  }
});
  • look at the console log and check the problem...

ahh another thing on the userpage use like this:

echo json_encode($_GET);

Upvotes: 1

000
000

Reputation: 27247

You know what, I'm going out on a limb here, and taking a wild guess.

I think you're lying to us.

Based on this sentence in your question: I'm using $_GET just because $_POST already has so many variables.

You're doing a POST, not a GET. Why would $_POST contain anything if you're sending a GET?

Change your ajax from this

    $.ajax({
        type: 'post',
        url: 'userpage.php',
        data: result
    });

to this

    $.ajax({
        type: 'get',
        url: 'userpage.php',
        data: result
    });

Upvotes: 0

Sark
Sark

Reputation: 4546

$.ajax({ 
   type       : "GET",  
   data       : { result : JSON.stringify(result) },  
   dataType   : "html",  
   url        : "userpage.php",  
   beforeSend : function(){ },
   success    : function(data){ console.log( data ) },
   error      : function(jqXHR, textStatus, errorThrown) { }
});

in your php

echo '<div id="data"><pre>'. $_GET["result"] .'</pre></div>';

Upvotes: 1

Related Questions