Passing JavaScript Array to PHP Through Jquery $.Ajax

Passing JavaScript array to PHP through jQuery $.ajax

data: { activitiesArray: activities },

That's it! Now you can access it in PHP:

<?php $myArray = $_REQUEST['activitiesArray']; ?>

How to pass Javascript array to PHP file using AJAX?

Convert js array in json format by JSON.stringify

function ProcessAJAXRequest()
{
$.ajax
({
type: "POST",
url: "myphpfile.php",
data: {"id" : 1, "myJSArray" : JSON.stringify(myArray)},
success: function (data)
{
alert(data);
}
});
}

And In the PHP use json_decode function to get value in array

json_decode($_POST["myJSArray"]);

Send array with Ajax to PHP script

Encode your data string into JSON.

dataString = ??? ; // array?
var jsonString = JSON.stringify(dataString);
$.ajax({
type: "POST",
url: "script.php",
data: {data : jsonString},
cache: false,

success: function(){
alert("OK");
}
});

In your PHP

$data = json_decode(stripslashes($_POST['data']));

// here i would like use foreach:

foreach($data as $d){
echo $d;
}

Note

When you send data via POST, it needs to be as a keyvalue pair.

Thus

data: dataString

is wrong. Instead do:

data: {data:dataString}

How to send an JavaScript array to PHP via jQuery.ajax?

Got it!

To properly access the JavaScrtipt objects in PHP I need to JSON.stringify them when pushing on the array. Then, on PHP, json_decode them to a PHP object, and access their properties with the '->' operator.

The final solution is as follows:

<?php 
include 'ChromePhp.php';

if (isset($_POST['newUsers'])) {

$newUsers = $_POST['newUsers'];

foreach ($newUsers as $user) {
# code...
$usr = json_decode($user);
ChromePhp::log("Nome: " . $usr->nome . " - Idade: " . $usr->idade);
}

} else { ?>

<html>
<body>

<script src="js/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
//var newUsersObj = {};
var newUsers = [];

newUser = {};
newUser['nome'] = 'alvaro';
newUser['idade'] = '34';
newUsers.push(JSON.stringify(newUser));

newUser1 = {};
newUser1['nome'] = 'bia';
newUser1['idade'] = '7';
newUsers.push(JSON.stringify(newUser1));

newUser2 = {};
newUser2['nome'] = 'alice';
newUser2['idade'] = '2';
newUsers.push(JSON.stringify(newUser2));

$.ajax({
url: "testcookie.php",
type: "POST",
data: {
'newUsers[]': newUsers
},
success: function () {

},
error: function () {

}
});

</script>
</body>
</html>
<?php } ?>

Send php array to jquery ajax and make a each loop from that array

try basic for loop with count using length This .. this should help surely.

 function ajax_test()
{
$.ajax({
url: "system/music_list.php",
dataType: 'json',
cache: false,
success: function(response)
{
for (var i = 0; i < response.length; i++)
{
alert(response[i]);
}
}
});
}

how to use AJAX to pass JavaScript array to PHP array

If you want to send a request to PHP through ajax you have to create a PHP script file separately. I think we can't send requests to the same file. and here is the code that I use for ajax requests.

script.php this should be a separate PHP file.

<?php
if( isset($_POST['myData']) )
{
echo json_encode($_POST['myData']);
}
?>

Here is the jquery ajax code.

var str_array = [1,2,3]
$.ajax({
type: "POST",
url: "./script.php",
data: {myData:str_array},
dataType: "json",
success: function (response) {
console.log(response);
},
error:function(data) {
console.log(data.responseText);
},

});

How to pass a PHP Array into jQuery AJAX $.post

you can use json_encode to make a valid javascript object

$("#ButtonCreate").click(function() {
$.post("next.php", <?php echo json_encode($issue); ?>,
function(data, status) {
document.getElementById("ResultBack").innerHTML = data;
});
});

if you would like to send 2 array via ajax, you can create a new array like this:

<?php echo json_encode(array(
'issues' => $issue,
'foo' => $foo,
)); ?>

Cannot send array to php via AJAX?

You defined values as an array and therefore when you stringify it, you get an empty array. Define it as an object like values = {}; and it will work.

You don't use associative arrays in javascript because

If you use named indexes, JavaScript will redefine the array to a
standard object. After that, some array methods and properties will
produce incorrect results.

Thats's why you need to define it as object in the beginning.

values = {};
values['mpsRegnomer'] = $('#mpsRegnomer').val();
values['mpsMarka'] = $('#mpsMarka').val();
values['mpsMarkaOther'] = $('#mpsMarkaOther').val();

Here is a working JSFiddle.
http://jsfiddle.net/pk97fe0b/

jQuery AJAX pass form data and an array to PHP

You need to encode the array, then you can add it to the FormData. You can convert it to JSON.

formData.append('arrGFormId', JSON.stringify(arrGFormId));

Then in PHP you can use json_decode($_POST['arrGFormId']).



Related Topics



Leave a reply



Submit