I need to pass an array and a string to my php page. But I cannot figure out how to do this. Here is my code:
Javascript:
function processData(myVar){
new Ajax.Request('myPage.php', {
type: 'post',
data: {myCmd: 'ProcessIt', addData: myVar},
onSuccess: function(transport) {
return transport.responseText;
}
});
}
PHP:
<?php
if (empty($_POST)){
// $_POST is always empty. Even though the type is array.
}
As my comment says, $_POST
is always empty. I have tried many ways to get some values out of it, but it is just empty.
Can someone show me what I am doing wrong?
If you want send POST with prototypejs this is the form in ajax
new Ajax.Request('index/index.php', {
method: 'POST',
parameters: {
'options[]': JSON.stringify({"array1" : "option1", "array2" : "option2"}),
'option': "test"
},
onSuccess: function(transport){
console.log(transport);
}
});
I hope this help you
index.php
if(empty($_POST)){
echo "BAD";
}else{
echo json_encode($_POST);
}
If it's a html form POST you can do this.
<input type="text" name="nameOfInput[]">
The []
means it POSTS as an array.