I have this code
$.ajax({
type: 'POST',
url: 'ajaxfunctions.php',
data: {email: email},
success: function(data)
{
if(data == "true" || data == "false")
{
alert("Response")
}
else
alert("Data: " + data);
}
});
with this PHP-Script
if(isset($_POST['email']))
{
$email = $_POST['email'];
$countEmail = $db->getCountEmail($email);
if($countEmail == 1)
echo "true";
else {
echo "false";
}
}
The problem is, that it never comes in the alert("Response") case. Always in the other. In the alert window I then got my full index.html content.. What am I doing wrong?
#devShuba monitor your Ajax request in Chrome here is a previous related post
Request Monitoring in Chrome
maybe the isset($_POST['email']) is returning false, that's why.
can you do a var_dump(isset($_POST['email'])); and check if it evaluates to true?
if no, then you have to check if the email is correctly posted using your javascript.
Related
I am using AJAX to call a PHP script. I am using conditions to echo the proper error message in my PHP. When I do this, my AJAX and JQUERY do not work properly.
My JQUERY/AJAX:
if (email != 0) {
// Run AJAX email validation and check to see if the email is already taken
$.ajax({
type: "POST",
url: "checkemail.php",
data: dataString,
async: false,
success: function(data) {
var error= false;
if (data == 'invalid') {
var invalid= 1;
}
else if (data == 'taken') {
var taken= 1;
}
if (invalid == 1) {
alert('invalid email');
e.preventDefault();
}
if (taken == 1) {
alert('email taken');
e.preventDefault();
}
}
});
}
My PHP:
<?php
$email = true
if ($email == true) {
echo "taken";
}
?>
But, when I just put:
echo "taken";
The AJAX and JQUERY works exactly how it should and the respective error message pops up. "taken" is being echo'd either way, so I don't get what is going on. What could I be doing wrong?
You're missing your semicolon.
$email = true
needs to be
$email = true;
In your response, you will probably be getting a PHP error - unless your error messages are suppressed.
I'm submitting a form via jQuery.ajax()
Now my PHP script is checking if a specific input field is empty, example:
$is_error = $user->is_error;
if($is_error !=0)
{
echo $is_error;
}
Back to my jQuery.ajax() , I'd like to check if the value of $error was true or not, within the sucess: part of the jQuery.ajax() call.
jQuery.ajax({
type: "POST",
url: "edit.php",
data: jQuery("#idForm").serialize(),
success: function(data)
{
// show response from the php script if there is an error message
// like:
// if(is_error) {show specific error message}
// else {show everything positive message}
}
});
Is it possible to check the PHP variable's value in there? Like if/else ?
Best regards!
if($_POST['name'] == "")
{
$error = 1;
}
else
{
$error = 0;
}
echo $error;
This code will echo the value.
jQuery.ajax({
type: "POST",
url: "edit.php",
data: jQuery("#idForm").serialize(),
success: function(data)
{
// show response from the php script if $error == 0 or $error == 1.
if(data==1)
....
}
});
Then you check what is the returned value.
With your variable data, you can return values from PHP. And after in your scope success you can check.
You have to echo the error so that it can be returned as data.. The ajax only returns what has been created in html..
In instances like this I would use the following:
if($_POST['name'] == "")
{
$error = 1;
}
else
{
$error = 0;
}
echo $error;
jQuery.ajax({
type: "POST",
url: "edit.php",
data: jQuery("#idForm").serialize(),
success: function(response)
{
if(response == 1){
alert('Error');
}else{
alert('No error');
}
}
});
i think you should try the following in php script
echo $error;
and then in jquery.ajax() do
success: function(data){
//data is $error
}
I have a strange error within my $.ajax call here:
//CHECK USERNAME
$('#username').focusout(function() {
var id = $(this).attr('id');
var username = ($(this).val());
$(this).removeClass('hint').removeClass('hint_validated');
if (!$(this).val() || !regexUser($(this).val())){
//INVALID
$('#'+id+'_hint').hide().addClass('hint').show().html(usernameInvalid);
}else{
//VALID -> CHECK USERNAME FROM DB IF CONFIG = TRUE
if (checkUsername == true){
//LOADING FROM DB
$('#'+id+'_hint').hide().addClass('hint_check').show().html(usernameCheck);
$.ajax({
cache: false,
type: 'POST',
url: 'classes/ajax_check.php', //all that does currently is echo "USERNAMEVALID"
data: {username: username},
success: function(response){
if (response == "USERNAMEVALID") {
$('#'+id+'_hint').hide().removeClass('hint_check').addClass('hint_validated').show().html(usernameValid);
}else{
alert("ERROR");
};
},
error: function(){
$('#'+id+'_hint').hide().removeClass('hint_check').addClass('hint').show().html(usernameError);
}
});
}else{
$('#'+id+'_hint').hide().addClass('hint_validated').show().html(usernameValid);
}
}
});
The success function is called but the IF clause throws always FALSE. Why?
If the success function is only alert (response); it actually alerts USERNAMEVALID
I've worked with these functions before but I can't find the error here... Thanks for reading, any help is apreciated.
cheer
PrimuS
Check for empty lines. There is a difference between seeing:
alert("Hi\n");
alert("Hi");
You see the same, but it is not. Try this:
alert(encodeURI("Hi\n"));
alert(encodeURI("Hi"));
Where, it alerts this way:
Hi%0A
Hi
PS: If you are using a good browser other than IE, please use console.log() instead of alert(). So that you can check what is printing and what not!
Fiddle: http://jsfiddle.net/XaT6S/
how to output or alert the mysqli error message during an ajax call?
here's my php code
if(isset($_POST['resumetitle']) || isset($_POST['name']) || isset($_POST['dob']) || isset($_POST['gender']) || isset($_POST['cvid'])){
$result = $db->updatepdetails($_POST['resumetitle'],$_POST['name'],$_POST['dob'],$_POST['gender'],$_POST['cvid']);
if($result){
echo "success!";
} else {
echo "failed! ".$result->error;
}
}
//here's my js code
$.ajax({
type: "POST",
url: "classes/ajax.resumeupdate.php",
data: "resumeid="+cvid+"&resumetitle="+resumetitle+"&name="+name+"&dob="+dob+"&gender="+gender,
success: function(msg){
//window.location = "resumeview.php?cvid="+cvid;
alert(msg);
},
});
after the ajax call, it only pop out the word "failed!" ...i wish to see the mysqli_error too, how's that?
You use $db->error and not $result->error
I am trying to get the data return from a function called by a jquery ajax call. My function is located in a a php file and it looks liket his
valid_user() {
$id = $_POST('id');
if($id == 'hello'){
return true;
}
else{
return false;
}
}
and here is my ajax call
$.ajax({
type: "POST",
url: path + "valid_user",
sucess: function(msg) {
alert("Data returned: " + msg );
}
});
I have tested everthing and the function is wokring ( has been changed for this example) but I can not the return value of the function valid_id(). How do I get this? the variable msg keeps coming back empty. Thanks
From my understanding, there are several issues.
1) the method valid_user() is not been called.
2) The url doesn't look like it is correct either.
3) The "success" keyword is spelt "sucess".
4) You aren't passing any "data".
Here is an example ajax call tailored to what you may want.
$.ajax({
type: "POST",
url: "validateUser.php",
data: "id=49",
success: function(msg){
alert( "true or false: " + msg );
}
});
It looks like you misspelled sucess----but this may not be in your running code. You should check the second parameter of success:
success:function(data, textStatus)
You need to write PHP server-side code that calls the function and writes its return value to the output stream. For example:
<?php echo valid_user(); ?>
This should work - you might want to put better sanitizing on the POST value just in case.
In the PHP file:
$id = isset($_POST['id']) ? trim($_POST['id']) : '';
$return = 'false';
if($id!=''){
valid_user($id);
}
echo $return;
valid_user($id) {
if($id == 'hello'){
$return = 'true';
}
}
jQuery Call:
<script>
id = 'hello';
$.ajax({
type: "POST",
url: "validateUser.php?id="+id,
success: function(msg) {
alert("Data returned: " + msg );
}
});
</script>
Thank you for your help, I figured out the issue, the reason why it was not working was becuase my function valid_id() was returning true or false, and I needed to return echo "true"; and echo "false"; once I did this the msg variable contained the data true or false.