Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.