javascript - json last error returns 4 -
i trying send array js php through ajax call.to achieve first converted string json using json.stringify() , @ other end used json_decode() decode, didn't work out.
using json_last_error()
found error 4.
js code looks this
var form_data = $(this).serialize(); form_data+='&extraitems='+json.stringify(extraitems); var button_content = $(this).find('button[type=submit]'); form_data=form_data+'&landtype='+landtype; button_content.html('adding...'); //loading button text $.ajax({ //make ajax request cart_process.php url: "/cart/cart_process.php", type: "post", datatype: "json", //expect json value server data: form_data }).done(function (data) { //on ajax success $("#cart-info").html(data.items); //total items in cart-info element button_content.html('add cart'); //reset button text original text if ($(".shopping-cart-box").css("display") == "block") { //if cart box still visible $(".cart-box").trigger("click"); //trigger click update cart box. } })
i appended array form data.
at php side
foreach($_post $key => $value){ $new_product[$key] = filter_var($value, filter_sanitize_string); //create new product array } //we need product name , price database. if(isset($new_product['extraitems'])) { $newprice=0; $extraitems=json_decode($new_product['extraitems'],true); var_export($extraitems); error_log(stripslashes($new_product['extraitems'])); if(is_array($extraitems)) { foreach($extraitems $key=>$value) { if($value=='cherry'||$value=='chocolate chip'||$value=='butterscotch chip'||$value=='gems') $newprice+=50; else if($value=='vanilla'||$value=='pineapple'||$value=='chocolate'||$value=='dark chocolate'||$value=='strawberry'||$value=='blackcurrant'||$value=='grape'||$value=='mango'||$value=='butterscotch') $newprice+=100; } } unset($new_product['extraitems']); }
this snap of firebug console.
how correct problem?
update:
var_export()
'"[\\"vanilla\\",\\"chocolate\\"]"'
the above console log shows before using json.stringify()
second 1 show after using on array.
i found solution, not working due code
foreach($_post $key => $value){ $new_product[$key] = filter_var($value, filter_sanitize_string); //create new product array }
because json decode doesn't work on filter sanitized strings.
Comments
Post a Comment