Rita_Ruah Posted January 23, 2014 Share Posted January 23, 2014 (edited) Hello, I have an array with this structure: array (size=2) 0 => array (size=2) 'name' => string 'MR A' (length=4) 'location' => string 'A LAND' (length=6) 1 => array (size=2) 'name' => string 'MR B' (length=4) 'location' => string 'B LAND' (length=6) I am trying to send it via Ajax converting the $array with json_encode($array) which results into: [{ name:"MR A", location:"A LAND" }, { name: "MR B", location: "B LAND" }] . But it's giving me an error... each time i make a var_dump from the other file, I find myself stuck with a "undefined". Is the array bad? How should I proceed? Tks EDIT: Please move to the right section (PHP Questions). Edited January 23, 2014 by Rita_Ruah Quote Link to comment Share on other sites More sharing options...
requinix Posted January 23, 2014 Share Posted January 23, 2014 json_encode() will not output that but instead [{"name":"MR A","location":"A LAND"},{"name":"MR B","location":"B LAND"}]Note the quotes around "name" and "location" and the lack of whitespace. Quote Link to comment Share on other sites More sharing options...
Rita_Ruah Posted January 23, 2014 Author Share Posted January 23, 2014 But I am receiving that with php json_encode... what gives? Quote Link to comment Share on other sites More sharing options...
Rita_Ruah Posted January 23, 2014 Author Share Posted January 23, 2014 Even with that output I cant send no data... how would you post this data into a jquery ajax call to other php file? Quote Link to comment Share on other sites More sharing options...
requinix Posted January 23, 2014 Share Posted January 23, 2014 Before anything else, how about you post all the code you have right now, including whatever PHP isn't working and/or whatever Javascript/AJAX isn't working. Quote Link to comment Share on other sites More sharing options...
Rita_Ruah Posted January 23, 2014 Author Share Posted January 23, 2014 <body> <?php $array = array(); for($i = 0; $i < 2; $i++){ $array[$i]['name'] = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5); $array[$i]['location'] = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5); } $array = json_encode($array); ?> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> <script> var data = <?php echo $array; ?>; $.ajax({ type: "POST", url: "process.php", data: data, success: function (msg) { console.log(msg); } }); </script> </body> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 23, 2014 Share Posted January 23, 2014 Shouldn't data be string value ie in quotes var data = "<?php echo $array; ?>"; Quote Link to comment Share on other sites More sharing options...
Rita_Ruah Posted January 23, 2014 Author Share Posted January 23, 2014 btw, process.php is just: <?php echo($_POST); ?> Quote Link to comment Share on other sites More sharing options...
Rita_Ruah Posted January 23, 2014 Author Share Posted January 23, 2014 (edited) Shouldn't data be string value ie in quotes var data = "<?php echo $array; ?>"; It gives me an error Btw, here's how it looks when running: var data = [{"name":"fspvo","location":"e6x3p"},{"name":"bx85i","location":"rlxf9"}]; Edited January 23, 2014 by Rita_Ruah Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted January 24, 2014 Share Posted January 24, 2014 (edited) I've had the most success with the following: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> <script> //var data = <?php echo $array; ?>; var data = <?php echo htmlspecialchars(json_encode($array), ENT_NOQUOTES); ?>; $.ajax({ type: "POST", url: "process.php", data: data, success: function (msg) { console.log(msg); } }); </script> Where array is a PHP array, not encoded. BTW, you can turn on Firebug and see the POST data in the console log under the AJAX entry. Edited January 24, 2014 by rwhite35 Quote Link to comment Share on other sites More sharing options...
kicken Posted January 24, 2014 Share Posted January 24, 2014 The data parameter to the ajax function needs to be either a string or an object of name/value pairs. What you need to do is create an object with a property and stick your encoded json as the value of the property. For example: var data = { array: <?php echo htmlspecialchars(json_encode($array), ENT_NOQUOTES); ?> }; $.ajax({ type: "POST", url: "process.php", data: data, success: function (msg) { console.log(msg); } }); Quote Link to comment Share on other sites More sharing options...
Rita_Ruah Posted January 24, 2014 Author Share Posted January 24, 2014 Hello, first of all thanks for the replies... I tried again and managed to made it work with the simple change of structure of my array: // From this: // $array[$i]['name'] // To this: // $array['contacts'][$i]['name'] Thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.