I'm trying to return a simple echo from my ajax request, and I've done it before, but cant work out why its not working this time!
I've stripped it down to the bare minimals and its still not working.
Javascript
function getUserColours(){
var username = $('#username').val();
$.ajax({
url: 'assets/processes/loginUserColour.php', // URL of php command
type: 'POST', //TYPE
data: {'username': username}, //Variables in JSON FORMAT
success: function(data) { //SUCCESSFUL REQUEST FUNCTION
console.log(data);
}
}); // end ajax call
}
PHP
<?php
if(isset($_POST['submit'])){
echo "true";
}
?>
But whenever the response is complete it just returns a completely blank string?
The url is definitely correct and working and my username variable is not blank, it does equal what the user types.
What is going wrong?
NOTE : I am using this exact same method already on the same application and it works, just not on this page?
You're looking for a POST value that doesn't exist in your AJAX data object. When jQuery sends AJAX requests it only sends what you've added to the data object as POST parameters.
You can either change your data object and add a "submit" property with some value or you can change your PHP to the following and it should work:
<?php
if (isset($_POST['username'])){
echo "true";
}
?>
Try to echo username. You pass the username in a username variable. Try this:
<?php if(isset($_POST['username'])){ echo "true"; } ?>
You have not submit
in your request parameters. You have two options to fix it:
First option to add submit
to your request in Javascript
function getUserColours(){
var username = $('#username').val();
$.ajax({
url: 'assets/processes/loginUserColour.php',
type: 'POST', //TYPE
data: {'username': username, 'submit': true}, //adding submit
success: function(data) {
console.log(data);
}
});
}
OR fix it from php side:
<?php
if(isset($_POST['username'])){
echo "true";
}
?>
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.