I'm trying to post a variable as an object to my PHP file, but it doesn't receive any data. I tried window.alert(u.un)
to test whether the data is being passed from AJAX call, and it works fine and there are no errors in my console. But still I'm not getting data in PHP file, there are no errors either.
This is my AJAX function
function getfulldetails(n)
{
var u={un:n}; window.alert(u.un);
var locationto= "getfull.php";
$.ajax({
type: "POST",
url: locationto,
data: u,
processData: false,
contentType: false,
success: function(response)
{
window.alert(response);
}
});
return false;
}
This is my PHP file
<?php
session_start();
if($_SERVER['REQUEST_METHOD']==='POST')
{
if(isset($_REQUEST["un"]))
{
function validate_data($data)
{
require 'connectcred.php';
$data = trim($data);
$data = stripslashes($data);
$data = strip_tags($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($conn,$data);
return $data;
}
$u=validate_data($_REQUEST["un"]);
echo $u;
}
else
{
echo "something's wrong";
}
}
?>
I'm getting result only from the else
part.
I've used AJAX using the code below many times to get data from Form and it worked like a charm, but its not working when I assign an object myself.
Don't set Content-Type
/ contentType
to false
. You also need to send an actual query string.
For now, this should fix it:
$.post(locationto, u, (response) => {
window.alert(response);
});
Alternatively, use data: "un=" + n
Also, in your PHP, use only $_POST
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.