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/
Related
so I have the following code that is calling a CheckEmail2.php. I know what I want to do in CheckEmail2.php but because my Ajax success is not working, I have minded the scope to just return a Json. below is my java script. When I launch my console, I see my string coming through that I am echoing in my CheckEmail2.php. so I know my php file is getting read but why won't my success function hit if my string is getting read? I can't get my success function to get called. It always goes to error.
<script type="text/javascript">
function validateForm(){
var email = document.forms["signupform"]["email"].value;
var result = false;
if (email != ""){
$.ajax({
type: "POST",
url: "/CheckEmail2.php",
data: { "User_Email": email },
dataType: "json",
success: function(resp){
console.log(resp);
if(resp == "Not Found"){
result = true;
}
else
{
result = false;
}
},
error: function(data, status){
console.log(data, status);
}
}); //end Ajax
}
return result;
}
</script>
here is my PHP
<?php
header("Content-type:application/json");
echo json_encode("Not Found");
?>
why is error always getting called?
I am making an AJAX call but its not returning a value in the success handler. This is my AJAX call. I have checked that it's hitting the PHP file correctly
var msj;
$.ajax({
type: "POST",
url: "ajaxFile.php",
data: {
name: name,
status: status,
description: description,
action: 1
},
sucess: function(data){
msj = data;
alert(data);
}
});
alert(msj);
My PHP code is as follow:
if (isset($_POST['action']))
{
if ($_POST['action'] == 1)
{
$obj = new project($_POST['name'], $_POST['active'], $_POST['description']);
$obj = testInput($obj);
$check = validateName($obj->getName());
if ($check == 1)
{
echo $nameError;
}
else
{
print "asdasdasd";
}
}
}
Please help me tracking the mistake.
As far as I can see there's a syntax error in your code. There's sucess instead of success.
You are only providing a "success" callback function. You should also provide an "error" callback so you can debug and see what is wrong. You can also provide a "complete" callback that will be used in both alternatives.
var msj;
$.ajax({
type:"POST",
url:"ajaxFile.php",
data:{name:name,status:status,description:description,action:1},
complete:function(data){
msj=data;
alert(data);
}
});
alert(msj);
In your PHP, you could add
header('Content-Type: application/json');
to make sure JQuery identify well your web service.
I'm trying to check if a website exists with an ajax call, but I'm not sure I am getting it right. On my page I grab a URL on click
$("#go").click(function() {
var url = $("#url").val();
$.ajax({
type: "POST",
url: "/ajax.php",
data: "url="+url,
success: function(){
$("#start").remove();
},
error: function(){
alert("Bad URL");
}
});
});
a=And then check on ajax.php
$url = $_POST['url'];
ini_set("default_socket_timeout","05");
set_time_limit(5);
$f=fopen($url,"r");
$r=fread($f,1000);
fclose($f);
if(strlen($r)>1) {
return true;
} else {
return false;
}
It seems I am getting SUCCESS no matter what... What am I missing?
It seems I am getting SUCCESS no matter what... What am I missing?
This is extremely pretty straightforward.
Because of this reasons:
// You have no idea what server respond is.
// that is you can't parse that respond
success: function(){
$("#start").remove();
}
Which should be
success: function(respond){
//you don't have to return TRUE in your php
//you have to echo this one instead
if ( respond == '1'){
$("#start").remove();
} else {
//handle non-true if you need so
}
}
In php replace this:
if(strlen($r)>1) {
return true;
} else {
return false;
}
to
if(strlen($r)>1) {
print true; //by the way, TRUE is a constant and it equals to == 1 (not ===)
}
Oh yeah, also don't forget to fix this as well:
data: "url="+url,
to data : {"url" : url}
As Nemoden said, you get a success message even if it returns false.
You need to check the data returned and then remove the element.
for example
$("#go").click(function() {
var url = $("#url").val();
$.ajax({
type: "POST",
url: "/ajax.php",
data: "url="+url,
success: function(response){
if (response == 'whatever you are returning') {
$("#start").remove();
}
},
error: function(){
alert("Bad URL");
}
});
});
Success callback is called whenever server-side script returned an answer (there were no connectivity errors or server-side errors). Is this answering your question?
See the difference:
$("#go").click(function() {
var url = $("#url").val(),
ajax_data = {url: url};
$.post({
"/ajax.php?cb=?",
ajax_data,
function(response){
if (response.status) {
// URL exists
}
else {
// URL not exists
}
$("#start").remove();
},
'json'
});
});
php back-end:
printf('%s(%s)', $_GET['cb'], json_encode(array('status' => (bool)$url_exists)));
I'm using jQuery and AJAX to validate my form when someone creates a new user on my website. I'm programming in OOP PHP, together with the jQuery and AJAX.
I'm using this code:
$.ajax({
type: "POST",
url: "includes/classes/handler.php?do=addLogin",
data: dataString,
success: function() {
$('.sideBarNewUserWrap').fadeOut();
}
});
return false;
But how do I return an error message, if the e-mail already exists?
Hope it's info enough, else I'll just add some more.
Thanks in forward :)
* UPDATE *
This is my PHP checking if email exists:
$email_count = mysql_num_rows($check_email);
if($email_count){
return false;
}
* UPDATE *
success: function(data){
if(data.error){
$('.sideBarNewUserWrap').fadeOut();
} else {
$('.sideBarNewUserError-email').fadeIn();
}
Now this looks pretty much as a failure because.
if(data.error) then it's okay?
Shouldn't it be something like:
if(date.error){
//Error message
}
And not the other way around?
Well, If I try to enter an email which already exists, it tells me as it should, but why does this work? In my eyes I'm doing something wrong here?
php:
$result = array('error' => true); // or $result = array('error' => false);
echo json_encode($result);
js:
success: function(response) {
if (response.error) {
// ...
}
}
You can get the response in the function:
$.ajax({
type: "POST",
url: "includes/classes/handler.php?do=addLogin",
data: dataString,
success: function(response) {
if (response == "ok")
{
$('.sideBarNewUserWrap').fadeOut();
}
else
{
// error happend
}
}
});
return false;
You can return string, int in PHP or even XML, JSON, whatever you want to validate on client side
You can return data by using an echo in your handler.php file.
To receive this in jQuery just place a parameter in the success function of the Ajax function.
success: function(returnedValue)
{
// Here you check if returned value e.g. "returnedValue == 1"
}
basically in the handler.php you should verify whether email already exists or not and then send to the client (at least) two different responses (eg. 0: email exists, 1:ok).
In the success callback you can read the response data so you can tell the user the operation status
I have a simple form validation with ajax and jquery. I've done this before but now it seems to be failing over and over.
The title is the error I see in the firebug console. After some google research, seems to be a problem with managing windows but... I'm not trying to do that and I also tried in other browsers with identical results. The form submits...
This is the function:
$("#login-da").submit(function(){
$('#error').hide();
var username = $('#username').val();
var password = $('#password').val();
var query = 'username=' + username + '&pass=' + password;
$.ajax({
type: "POST",
url: "functions/check-user.php",
async: false,
data: query,
cache: false,
success: function(response){
if(response == 0) {
$('#error').slideDown();
var error = 1;
}
else {
var error = 0;
}
}
});
if (error == 1){
return false;
}
});
I've tried to return false directly in the success function with identical results. The error begins to show but the form submits anyways and nothing appear.
I've also noticed through debugging that anything outside the ajax call isn't executed.
Any help would be really appreciated
Try using Number() on the response data, also, check the scope of the error variable on the response funcion:
success: function (response) {
var result = Number(response);
if (result == 0) {
$('#error').slideDown();
error = 1;
} else {
error = 0;
}
}