Jump to content

Recommended Posts

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 by Rita_Ruah


<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>

 

 

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 by Rita_Ruah

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 by rwhite35

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);
	  }
});
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.