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 = "mail@example.com";
 
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="you@email.com" /><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.

Edited by mdmartiny
Link to comment
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.