I am trying to verify some form fields using javascript but I cant seem to get it to work. Its just a simple html form with a few inputs.. name, phone, message, etc.
Contact.html
<form action="sent.php" method="post" id="myform" enctype="multipart/form-data">
<div id="myform"></div>
<fieldset class="addingenq">
<label>Your Name</label>
<input name="custName" type="text" size="40" >
<label>Phone Number *</label>
<input name="custPhone" type="text" size="40" >
<label>Email Address <em>(Optional)</em></label>
<input name="custEmail" type="text" size="40" >
<label>Enquiry type</label>
<input name="subjectType" type="text" size="40" >
<label>Details about Enquiry</label>
<textarea name="messageType" cols="68" rows="5" class="msg" type="text"></textarea>
<div class="submitbutton">
<input type="submit" name="submit" value="Submit Message" >
</div>
</fieldset>
</form>
Send.php
I attempted to get it return back to contact.html but it just showed a blank sent.php although I did receive the email. Is there a way to make $From something like the custName instead of 'custEmail', as 'custEmail' will not be a requirement on this form.
<?php
if($_POST){
$custName = $_POST ['custName'];
$custPhone = $_POST ['custPhone'];
$custEmail = $_POST ['custEmail'];
$subjectType = $_POST ['subjectType'];
$messageType = $_POST ['messageType'];
}
$subject = "$subjectType";
$message = "
Name: $custName
Phone Number: $custPhone
Enquiry type: $subjectType
Message: $messageType
";
$from = "From: $custEmail
";
//put your email address here
mail("myemail@mail.com", $subject, $message, $from);
exit;
?>
Use HTML5
required
attr to verify the form fields
<form action="sent.php" method="post" id="myform" enctype="multipart/form-data">
<div id="myform"></div>
<fieldset class="addingenq">
<label>Your Name</label>
<input name="custName" id="name" required type="text" size="40" >
<label>Phone Number *</label>
<input name="custPhone" id="no" required type="text" size="40" >
<label>Email Address <em>(Optional)</em></label>
<input name="custEmail" type="text" size="40" >
<label>Enquiry type</label>
<input name="subjectType" id="type" type="text" size="40" >
<label>Details about Enquiry</label>
<textarea name="messageType" id="enq" required cols="68" rows="5" class="msg" type="text"></textarea>
<div class="submitbutton">
<input type="submit" name="submit" value="Submit Message" >
</div>
</fieldset>
</form>
JQuery
submitButton.onclick = function Validate(){
var name = $("#name").val();
var no = $("#no").val();
var enquiry = $("#enquiry").val();
var message = $("#ErrorMessage").val();
if(name == '' || no == '' || enquiry == ''){
message.empty().append ("Complete the registration!!!");
}
else{
message.empty().append ("Registration Completed");
}
};
Change <form action="sent.php" method="post" id="myform" enctype="multipart/form-data">
to
<form action="sent.php" method="post" id="myform">
and use if ($_SERVER['REQUEST_METHOD'] == 'POST') {
instead of just
if($_POST){
then see if you are getting anything at all by using
print_r($_POST)
It's possible that you are getting an error, but the page isn't displaying it because displaying of errors is turned off, in that case, include
error_reporting(E_ALL);
ini_set('display_errors', 1);
on top of your send.php page