I am tring to send an array to PHP, PHP however, only outputs "array". I have two files:
forms1.php and forms1.js. The PHP if statement never turns true. I have tried without the if statement too.
Thanks for pointing it out in the comments, Yes I call the function, and the alert returns entire forms1.php code.
old title: Why does my php file not see GET["result"]?
// this title is wrong
forms1.js
var nums = [1,2,3,4];
function postcars() {
$.ajax({
type : 'GET',
url : 'forms1.php',
data : {"result": nums},
success: function(resp)
{
alert(resp);
}
});
}
forms1.php
<?php
if ($_GET["result"] != null){
$filename = $_GET["result"];
echo $filename;
}
?>
3
Answers
What I’d normally do would be to test if a particular parameter is in the request ( using
isset()
or!empty()
) and then callob_clean()
to discard any possible output buffer generated at that stage. Process the request and then terminate the processing completely as it is a different thread. The response then that is set back is only what you want to send. This approach with a GET request would cause a regular page loading where there is a querystring, such asforms1.php?result=banana
, to halt at that stage and display no further content however/.You did not call the function
postcars()
;The JavaScript file should be like this:
nums
is an array, elements inside might be sent as individual values and the parameter name isresult[]
, make it a single string by usingJSON.stringify