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
https://forums.phpfreaks.com/topic/285994-submit-a-form-without-refresh/
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.