views:

87

answers:

2

I have the following object that gets created in my javascript application.

    poll_data[active_question] = {
        'question': $('div.question_wrap textarea').attr('value'),
        'answers':  [
            $('div.answer_wrap input#0').attr('value'),
            $('div.answer_wrap input#1').attr('value'),
            $('div.answer_wrap input#2').attr('value'),
            $('div.answer_wrap input#3').attr('value'),
            $('div.answer_wrap input#4').attr('value'),
            $('div.answer_wrap input#5').attr('value')
        ]
    };

active_question is set to 'poll', 0, 1, 2, 3, 4, or 5 depending on the question being worked on at the moment. I am trying to post this object to a php script using the following JS code.

    $.ajax({
        url:            '/somewebsite/poll/create?json=show',
        type:           'POST',
        // dataType:        'json',
        data:           poll_data,
        contentType:        'application/json; charset=utf-8',
        success:        function(data) {
            alert(data);
        }
    });

My PHP code is very simple.

    echo json_encode($_POST); exit;

When I run the script and click the button that triggers the submission of the data, I receive the alert (so the actual ajax code works), but the result from my PHP script is just an empty array. I think that this is an issue with the way the object is constructed, but I am not sure, and have not been able to find a work around.

Thanks in advance.

+1  A: 

Okay, a few things:

poll_data is not a valid JSON object. You would have to use poll_data[active_question], which IS a valid JSON object. jQuery should serialize this correctly. Remove the contentType -- I am pretty sure that is for php (not positive) but your code wouldn't work for me until I removed it. Finally, the appending of json=show to the query string doesn't do anything..it will just be ignored.

A couple minor things too: you can use .val() instead of .attr('value'), and have you looked into .serialize() to create your post data for you?

tandu
Ok, your missing the point. Even if change my PHP code to. echo var_dump($_POST); There is no data, PHP is not receiving the JSON object.
Nicholas Curtis
Edited my answer -- hope it helps.
tandu
Ok, great that helped alot. I changed my javascript code to the following. $.ajax({ url: '/polple/poll/create', type: 'POST', dataType: 'json', data: {'poll': poll_data['poll'], 'a': poll_data[0], 'b': poll_data[1]}, success: function(data) { alert(data); } });Now everything seems to be working fine. Also thank you for the heads up on .val instead of .attr('value'), and the ?json=show is actually for my PHP code it tells the code to output json instead of HTML. Thanks for the help.
Nicholas Curtis
A: 

do this on server

$data;
$data->question=$_POST['question']
$data->answer=$_POST['answers']
echo json_encode($data);

do this for ajax request

 $.ajax({
        url: '/somewebsite/poll/create?json=show',
        type:'POST',
        //modified data proprty
        data:poll_data[active_question],
        success: function(data) {
            alert(data);
        }
    });
Praveen Prasad
problem with this is that no POST data is available on the server.
Nicholas Curtis
If a do a simple var_dump($_POST); on the server i get the following content.array(0) {}
Nicholas Curtis
Request Method:POSTStatus Code:200 OKRequest HeadersAccept:application/json, text/javascript, */*Content-Type:application/json; charset=UTF-8Origin:http://localhostReferer:http://localhost/polple/poll/createUser-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3X-Requested-With:XMLHttpRequestResponse HeadersCache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0Connection:Keep-AliveContent-Length:13Content-Type:text/htmlKeep-Alive:timeout=5, max=94Pragma:no-cache
Nicholas Curtis
i have edited the codes please check
Praveen Prasad