Jump to content

[$.ajax] Help success: function


JonnySnip3r

Recommended Posts

Hey guys im new to jquery, anyways im testing out some stuff and i have been making a form that submits without refresh. Anyways so far so good but i seem to have a problem i can manage to gather all of the information from the text fields echo them out in an alert to ensure i have everything and i can pass it through to another page even add it to a database. But how do i return a message to let the user know it was successful?

 

If my php script returns true;

 

how do i catch this? here is what i have::

 

$(document).ready(function(){

$("#submit").click(function(){
	var name = $('input[name=fullname]');
	var email = $('input[name=email]');
	var subject = $('input[name=subject]');
	var comment = $('textarea[name=comment]');
	var captcha = $('input[name=captcha]');

	if(name.val()==''){
		name.addClass('error');
		return false;
	}else{
		name.removeClass('error');
	}
	if(email.val()==''){
		email.addClass('error');
		return false;
	}else{
		name.removeClass('error');
	}
	if(subject.val()==''){
		subject.addClass('error');
		return false;
	}else{
		subject.removeClass('error');
	}
	if(comment.val()==''){
		comment.addClass('error');
		return false;
	}else{
		comment.removeClass('error');
	}
	if(captcha.val()==''){
		captcha.addClass('error');
		return false;
	}else{
		captcha.removeClass('error');
	}

	var data = 'name=' + name.val() + '&email=' + email.val() + '&subject=' + subject.val() + '&comment=' + comment.val() + '&captcha=' + captcha.val();

	$("#submit").fadeOut("fast");
	$("#loading").show();

	$.ajax({
		url: "process.php",
		type: "POST",
		data: data,
		cache: false,

		success: function(html){
			if(html==1){
				$("#loading").hide();
				alert("Sent!");
			}else{
				$("#loading").hide();
				alert("Failed!");
			}
		}
	});
	return false;
});
});

 

I found online the (html) but how else could this be done it seems that when my script returns true it will alert me saying failed even when though everything was successful.

 

Hope someone can help thanks!

Link to comment
https://forums.phpfreaks.com/topic/217805-ajax-help-success-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.