Jump to content

Help with form submission and AJAX


mdmartiny

Recommended Posts

I am trying to add a contact form to a page. i can't get the script to do anything. When I click on the submit button all it does is sit there no error messages or anything.

If the fields are not filled in then there is supposed to be an error message. If everything is ok then there is supposed to be a success message. 

This is the Jquery code that I am using

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
	<script language="javascript" type="text/javascript" >

	$(function(){
		$("#ContactForm").submit(function(){
			$("#submitf").value='Please wait...';
			
			$.post("/contact.php?send=comments", $("#ContactForm").serialize(),
			function(data){
				if(data.frm_check == 'error'){ 
						$("#message_post").html("<div class='errorMessage'>ERROR: " + data.msg + "!</div>"); 
						document.ContactForm.submitf.value='Resend >>';
						document.ContactForm.submitf.disabled=false;
				} else {
					$("#message_post").html("<div class='successMessage'>Your message has been sent successfully!</div>"); 
					$("#submitf").value='Send >>';
					}
			}, "json");
			
			return false;
			
		});
	});

	</script>

 

 

Here is the contact page that I am also using

<?php
$adminemail = "[email protected]";
 
if ($_GET['send'] == 'comments'){
 
 $_uname = $_POST['name'];
 $_uemail = $_POST['email'];
 $_comments = stripslashes($_POST['message']);
 
 $email_check = '';
 $return_arr = array();
 
	if($_uname=="" || $_uemail=="" || $_comments==""){
	 	$return_arr["frm_check"] = 'error';
	 	$return_arr["msg"] = "Please fill in all required fields!";
	 } else if(filter_var($_uemail, FILTER_VALIDATE_EMAIL)) {
	 
		 $to = $adminemail;
		 $from = $_uemail;
		 $subject = "New Contact Us Message: " .$_title;
		 $message = $_comments;
		 
		 $headers = "MIME-Version: 1.0\r\n";
		 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		 $headers .= "Content-Transfer-Encoding: 7bit\r\n";
		 $headers .= "From: " . $from . "\r\n";
		 
		@mail($to, $subject, $message, $headers);
	 
	 } else {
	 	$return_arr["frm_check"] = 'error';
	 	$return_arr["msg"] = "Please enter a valid email address!";
	}
 
	echo json_encode($return_arr);
}
 
?> 

This is my form 

<form action='' method='post' name='ContactForm' id='ContactForm' >
                            <fieldset class="info_fieldset">
                                    <input class="textbox" type="text" name="name" value="" placeholder="Name" /><br />
                                    <input class="textbox" type="text" name="email" value="" placeholder="[email protected]" /><br />
                                    <textarea class="textbox2" name="comments" rows="5" cols="25" placeholder="You can say Hi !"></textarea>
                                    <input type='submit' value='Submit' class='myinputbtn' name='submitf' id="submitf">
                                <div id='message_post'></div>
                            </fieldset>
                        </form>

I am trying to learn how to use Jquery so I am not sure what I have going on here or if I am even doing it right.

Link to comment
https://forums.phpfreaks.com/topic/277125-help-with-form-submission-and-ajax/
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.