How to Pass a JavaScript Array via Jquery Post So That All Its Contents Are Accessible via the PHP $_Post Array

How to pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?

If you want to pass a JavaScript object/hash (ie. an associative array in PHP) then you would do:

$.post('/url/to/page', {'key1': 'value', 'key2': 'value'});

If you wanna pass an actual array (ie. an indexed array in PHP) then you can do:

$.post('/url/to/page', {'someKeyName': ['value','value']});

If you want to pass a JavaScript array then you can do:

$.post('/url/to/page', {'someKeyName': variableName});

How to pass JS array via POST AJAX to PHP?

Most likely you get unexpected results because you apply string-related functions trim and mysql_real_escape_string to array $_POST['var_ids']

As long as it is just an array of integers - the only mysql sanitize you need is casting to int:

$ids = array_map('intval', $_POST['var_ids']);
print_r($ids);

Pass Javascript array containing objects using JQuery post

You will end up with object looking like:

var fruit ={
name:'Apple',
color: 'Red'
}

You can simply post that object to php

$.post('path/to/server', fruit, function(resp){
// validate response and do something
});

Default contentType for $.ajax of which $.post is a shorthand method is application/x-www-form-urlencoded so it is exactly the same as sending a form.

jQuery internals will take care of encoding the data object passed as argument to $.ajax or $.post or other shorthand methods

In PHP

$fruitName = $_POST['name'];
$fruitColor = $_POST['color'];

If you wanted to send the whole array you could do:

$.post(url, {fruits: fruits});

Then in php:

 $fruitsArray = $_POST['fruits'];
foreach($fruitsArray as $item){
$name = $item['name'];
}

Jquery post array via ajax

I'm getting a javascript error on this line

new_data = [a,b,c,d,e];

I had to change it to this

new_data = ['a','b','c','d','e']; 

you capitalized the J in jQuery in this line

somedata_assoc = JQuery.param({'choices[]': new_data});

should be this (or just the $ shorthand)

somedata_assoc = jQuery.param({'choices': new_data});

also, i dont think you need the brackets, in most cases they would make it more difficult to retrieve the data on the server

Sending POST parameters between PHP files using AJAX calls

A slightly different version of the above which should help you solve your problem. For ease in debugging it is all one page but you can clearly see the script in operation once you click the button. Simply echoing javascript in the php code and hoping it will execute client side is not going to work - simply echo a message or a JSON object would be better.

<?php

if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();

print_r( $_POST );

exit();
}

?>
<!doctype html>
<html>
<head>
<title>jQuery - ajax experiments</title>
<script src='//code.jquery.com/jquery-latest.js'></script>

</head>
<body>
<a id='link' role='button' href='#'>Test</a>
<script>
$('#link').click(function(e) {
e.preventDefault();
alert('function entered');
$.ajax({
url: location.href,
type: 'POST',
data: {
parameter1: 'test',
parameter2: 'test2'
},
success: function(msg) {
alert( 'wow' + msg );
}

});
alert('function end');
});
</script>
</body>
</html>

As 2 separate pages ( both in same directory otherwise edit page to the ajax target )

<!doctype html>
<html>
<head>
<title>jQuery - ajax experiments</title>
<script src='//code.jquery.com/jquery-latest.js'></script>

</head>
<body>
<a id='link' role='button' href='#'>Test</a>
<br /><br />
<script>
$('#link').click(function(e) {
e.preventDefault();

$.ajax({
url: 'jquery-ajax-target.php',
type: 'POST',
data: {
parameter1: 'test',
parameter2: 'test2'
},
success: function(msg) {
alert( 'wow' + msg );
}
});
});
</script>
</body>
</html>

And the ajax target, jquery-ajax-target.php

<?php

if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();

$parameter1=( isset( $_POST['parameter1'] ) ) ? $_POST['parameter1'] : false;
$parameter2=( isset( $_POST['parameter2'] ) ) ? $_POST['parameter2'] : false;

$_POST['modified']=array(
'p1'=>sprintf('modified %s', strrev( $parameter1 ) ),
'p2'=>sprintf('modified %s', strrev( $parameter2 ) )
);

$json=json_encode( $_POST );
echo $json;

exit();
}

?>


Related Topics



Leave a reply



Submit