Hello,
I found a tutorial script that fits my need but it doesn’t seem to work. I’m trying to setup a email form that executes without refreshing the whole page but the script doesnt work.
I have a little bit of a javascript weakness and i was wondering if someone can figure this out for me… greatly appreciated.
thanks
<!doctype html>
<html>
<head>
<title>Submit form without refreshing the page</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" </script>
<script>
$(function()
{ $("#send").click(function()
{ $.ajax(
{ type: "post",
url: "send.php",
data: $("#myform").serialize(),
success: function(response)
{ if(response == "done")
{ alert("Form submitted successfully!"); }
else
{ alert("Form submission failed!"); }
},
error:function(response){ alert(response); }
});
});
});
</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="button" id="send" value="Submit" />
</form>
</body>
</html>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];
if(!empty($name) && !empty($msg))
{ //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";
}
?>