php - Pass Javascript array containing objects using JQuery post -
i have javascript array containing javascript objects:
var fruits = []; var fruit = new object; fruit.name = apple; fruit.color = red; fruits.push(fruit); how pass using jquery's post $.post contents accessible through php's $_post array?
how object properties accessed php?
you end object looking like:
var fruit ={ name:'apple', color: 'red' } you can post object php
$.post('path/to/server', fruit, function(resp){ // validate response , }); default contenttype $.ajax of $.post shorthand method application/x-www-form-urlencoded same sending form.
jquery internals take care of encoding data object passed argument $.ajax or $.post or other shorthand methods
in php
$fruitname = $_post['name']; $fruitcolor = $_post['color']; if wanted send whole array do:
$.post(url, {fruits: fruits}); then in php:
$fruitsarray = $_post['fruits']; foreach($fruitsarray $item){ $name = $item['name']; }
Comments
Post a Comment