Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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){}

share|improve this question
    
what is not working ? –  Maximus2012 Mar 25 '14 at 20:17
2  
@undefined echo can take multiple arguments, separated by commas. It's equivalent to concatenating them in to one string and passing them as a single argument. See php.net/echo –  IMSoP Mar 25 '14 at 20:18
    
I'm getting an array(0){} result. And yes, I do for echos. For string variables, I use . –  markovchain Mar 25 '14 at 20:19
    
@markovchain You are building a javascript object not an array. –  Sark Mar 25 '14 at 20:21
    
Where are you seeing the output from PHP? There doesn't seem to be a response callback for that $.ajax call. If you use your browser's dev tools (or Firebug or similar) can you see the request URL being constructed correctly? –  IMSoP Mar 25 '14 at 20:22

3 Answers 3

up vote 1 down vote accepted
$.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>';
share|improve this answer
    
The dataType is html here, not json. –  Joe Frambach Mar 25 '14 at 20:49
    
@JoeFrambach thanks and updated to 'html'. –  Sark Mar 25 '14 at 20:52
    
It doesn't print the result in the PHP like this. This time what it prints is NULL instead of an empty array. I've also tried replacing the dataType with html. It prints into the console the html code for the entire page, and it worked. It did display on the console. –  markovchain Mar 25 '14 at 20:53
    
@markovchain try without var_dump(), i have updated my answer. –  Sark Mar 25 '14 at 20:56
    
@markovchain also you have syntax error at echo, replace the ',' with '.' –  Sark Mar 25 '14 at 20:58
  $.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);
share|improve this answer

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
    });
share|improve this answer
    
That would be a relief if that was the error, but unfortunately not –  markovchain Mar 25 '14 at 21:01
    
Could you please explain why $_POST has anything in it? –  Joe Frambach Mar 25 '14 at 21:01
    
The page has a large form basically, where the information for the result object here comes from. –  markovchain Mar 25 '14 at 21:03
    
I thought userpage.php was just echo '<div id="data"><pre>', var_dump($_GET), '</pre></div>';. That's no large form. –  Joe Frambach Mar 25 '14 at 21:05
    
No, it's not the entire userpage.php. But the other codes aren't relevant to the question so I didn't show it, such as codes for connecting to the database, etc. There's also a hidden input field where the token comes from for protection purposes. I was just trying to print into the same page the result of the ajax call. –  markovchain Mar 25 '14 at 21:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.