Jump to content

submit a form without refresh


learningPHP1

Recommended Posts

Hello,

 

I'm working on a small form script which submits the form without refreshing the whole page. its a email form which basically calles sendemail.php and returns either "done" or "failed". NOt yet scripted to send, just testing.

 

I been at it for 3 days with no success and wondering if someone can help me fix the problem.

Any help you can provide would be greatly appreciated...

Thanks

<!doctype html>
<html>
	<head>
		<title>Submit form without refreshing the page</title>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" </script>
		<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
		<script>
			$(function() 
			{	$("#send").click(function(e) 
				{	e.preventDefault();
					$.ajax(
					{	type: "POST",
						url: "sendemail.php", 
						data: $("#myform").serialize(),
						success: function(response) 
						{	if(response == "done")
							{ alert("Form submitted successfully!"); }
							else
							{ alert("Form submission failed!");	     }
						}						
					}); 
				});
			}); 
		</script> 
	</head>
	<body>
		<form id="myform"  >
			Name: <br /><input type="text" name="name" id="name" /><br /> 
			Email: <br /><input type="text" name="email" id="email" /> <br />
			Message: <br /><textarea name="msg" id="msg"></textarea> <br />
			<input type="submit" id="send" class="send" value="Submit" /> 
		</form>
	</body>
</html>

small php script

<?php
$name = $_POST['name'];
$message = $_POST['msg'];

if(!empty($name) && !empty($message))
{	//Do your MySQL or whatever you wanna do with received data
	//Do not forget to echo "done" when action was completed successfully.
	echo "done"; 
}
else
{	echo "fail";	}
?>
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.