3

JavaScript/JQuery

var arr=[];

$('.element').each(function(i)
{
    arr.push({"id":i,"value":$(this).attr('data-value')});
});

$.get('/json.php?arr='+arr,function(result)
{
    alert(result);
});

PHP

<?php

$j = json_decode($_GET['arr'], true);

foreach($j as $k => $v)
{
    echo $v['id'].':'.$v['value'].'<br />';
}

?>

Problem

But the problem is that the URL looks like /json.php?arr=[object Object],[object Object] instead of /json.php?arr=[{"id":1,"value":"value 1"},{"id":2,"value":"value 2"}]. Do I need to convert the object to a string? But I don't want to use another library other than JQuery. Is this possible? :/

0

2 Answers 2

6

Try JSON.stringify

$.get('/json.php?arr='+JSON.stringify(arr),function(result)
Sign up to request clarification or add additional context in comments.

Comments

2

If you aren't tied to the idea of passing if via JSON, you can provide it in the data part of the jQuery request:

$.get( url, data, callback );

And then retrieve it later:

foreach( $_POST as $key => $value ){
   //....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.