I am having a hard time getting my form to work.
Can anyone take a look at this and tell me why nothing happens when I try it in a live environment? I managed to get the form to submit using "action" in the form tag whilst ditching the javascript altogether, with the PHP returning both Error and success depending on whether I filled out all fields and the email was successfully received.
However I want to use the javascript method so that I can prevent a page reload and use alert messages as this form will be used inside a packaged mobile app.
Any help is much appreciated.
<form method="post" name="form1" id="form1">
<label for="textfield">Text Field:</label>
<input name="FirstName" type="text" id="FirstName">
<label for="textfield2">Text Field:</label>
<input name="LastName" type="text" id="LastName">
<label for="textfield3">Text Field:</label>
<input name="Email" t ype="text"id="Email">
<label for="textfield4">Text Field:</label>
<input name="MessageText" type="text" id="MessageText">
<input type="button" onclick="submit()" value="submit contact"/>
</form>
<script>
$(document).ready(function(){
event.preventDefault();
$("form").submit(function(){
$.post('http://************/requestform.php', {
FirstName: $('#FirstName_input').val(),
LastName: $('#LastName_input').val(),
Email: $('#Email_input').val(),
MessageText: $('#MessageText_input').val()
}, function (html) {
var response=html;
if (response=="success") {
alert("Message sent!");
} else {
alert("Sorry please fill all fields!");
return false;
}
});
});
});
and the PHP part
<?php
$FirstName=$_POST["FirstName"];
$LastName=$_POST["LastName"];
$Email=$_POST["Email"];
$MessageText=$_POST["MessageText"];
$Headers = "From:" . $Email;
if(
$FirstName=="" ||
$LastName=="" ||
$Email=="" ||
$MessageText==""
) {
echo "Error";
} else {
mail("*******@gmail.com","mobile app message",$MessageText, $Headers);
echo "success";
}
?>
onclick="submit()"$.postshould work. If full refresh is what you want then why don't you do a regular submit with<input type="submit"/>