I am making a notification system in which I get the row numbers of the user from the table whose new notifications just came in. I wanna save those row number in an array and pass it to the jquery which called the php to get the row numbers... it works perfect now i wanna pass that json returned object with all the array values to a different php page which will get the values from that array one by one and perform some action on each value.... How can i do that ? I have searched quite a lot but found nothing that meets the requirement. Can any one help me?
3 Answers
If I understand you correctly, you have a JSON Object in Javascript/JQuery and you want to send that to PHP via Ajax again. Then why don't you do it like this:
In Javascript:
function sendJSONToPHPAgain(jsonData)
$.ajax({
url: 'endpoint.php',
data: {
json: JSON.stringify(jsonData)
},
contentType: "application/json",
});
}
In PHP (endpoint.php):
$json = json_decode($_REQUEST['json'], $assoc=true);
// now you have a nice php array to work with ...
For example, use it like that:
var d = {a:2, b:3, c:"Hello World"};
sendJSONToPHPAgain(d);
Or am I missing something?
Comments
$last_id = json_decode($_POST['arr'],true);
$size = sizeof($last_id);
for($i = 0; $i < $size; $i++)
{
$element = $last_id[$i];
$reply = mysql_query("select * from user_messages where id = '$element'");
while($new_message = mysql_fetch_array($reply))
{
echo'
<div class="incomming_msg_wrapper">
<p class="incomming_msg">'.$new_message['message'].'</p>
</div> <br>
';
}
}
**Thats how I got it working and it works fine. All I wanted to do is to access the elements of the json type array send to php via jquery by $.post() function one by one **
Comments
i've found two "gists" tha may help you out: Convert array php to js array and Convert json to php code