I have an HTML form, and linked with jQuery and PHP. The jQuery check whether the fields or empty or not, if they are not empty, it should send the data to the PHP file. But its not working.
$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var message = $("#message").val();
$("#returnmessage").empty(); // To empty previous error/success message.
// Checking for blank fields.
if (name == '') {
alert(" Please Fill your name");
} else if (message == '') {
alert("Please Fill message");
} else {
$.post("xxx.php", { // To php file.
name: name,
message: message
}, function(data) {
$("#returnmessage").append(data);
if (data == "We will contact you soon.") {
$("#form")[0].reset(); // To reset form fields on success.
}
});
}
});
});
<form name="form" method="post" action="">
<h4>FEED-BACK FORM</h4>
<p id="returnmessage"></p>
<label>Name: </label>
<input type="text" id="name" /><br>
<label>Message: </label>
<input type="text" id="message" /><br>
<input type="button" id="submit" value="Submit"/>
</form>
THE PHP CODE IS:
if(!isset($_POST['submit']))
{
//Need to submit the form.
exit;
}
$name = $_POST['name'];
$message = $_POST['message'];
$email_body ="Dear sir,n".
"Name: $namen".
"Message: $messagen";
//Email sending to
$email_from = 'xxx@xxx.xxx';
$email_subject = "my-form";
$to = "xxx@xxxx";
$headers = "From: $email_from rn";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
Aucun commentaire:
Enregistrer un commentaire