PHP form not validating form field properly - php

I'm learning some more PHP and after PHP itself to work, I can't seem to get it to validate any form field correctly. My goal is to check if the firstname field is empty, and if it is, it will give a message in a red color. The message in the red works, but only because the echo script is being called by the form submission, not because it has detected any empty field, because when I made an else statement to say "wassup" if its not empty, I got the same message for when the field is empty. Also, is there a way to check off multiple input fields at once like you could with JavaScript? For example if input1 == '' || input2 == '' and so on. Here is my HTML:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="welcome.php" method="post">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text">
Middle name <input name="middlename" type="text">
Surname <input name="lastname" type="text">
Age <input name="age" type="number">
Date of birth <input name="dob" type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input name="country" type="text"> State <input type="text">
City <input name="city" type="text">
Street number <input name="streetno" type="number">
Street name <input name="streetname" type="text"> <br><br>
Suburb <input name="suburb" type="text"> Postcode <input name="postcode" type="number">
If none of these apply to your accommodations, enter a typed location here <input type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text">
First time job seeker <input type="checkbox" name="check1" value="ftjb">
I'm a student <input type="checkbox" name="check2" value="ias">
Previous &/or most recent acedemic title <input name="school" type="text">
First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq">
I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival <input name="reason" type="text">
Date of arrival <input name="arrival" type="date">
Amount of stay expectancy
<input type="checkbox" name="check3">Temporary
<input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input name='signiture' type="text">
</fieldset>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Here is my PHP code:
<?php
$firstname = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$dob = $_POST['dob'];
$country = $_POST['country'];
$city = $_POST['city'];
$suburb = $_POST['suburb'];
$postcode = $_POST['postcode'];
$streetno = $_POST['streetno'];
$streetname = $_POST['streetname'];
$suburb = $_POST['suburb'];
$job = $_POST['job'];
$school = $_POST['school'];
$reason = $_POST['reason'];
$arrival = $_POST['arrival'];
$signiture = $_POST['signiture'];
if (isset($_POST['submit'])) {
if (empty($_POST[$firstname])) {
echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
echo "wassaup";
}
}
?>

In your if statement, you need to do this:
if (empty($_POST['name'])) { //Or replace $_POST['name'] with $firstname
echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
echo "wassaup";
}

You have a wrong syntax if (empty($_POST[$firstname])). You should use ' ' single paranthesis in firstname and remove this sign $.

change this if (empty($_POST[$name])) { to if (empty($_POST['firstname'])) { and check again. your syntax is wrong that's why it is not working.

Change in your code
if (!isset($_POST['firstname'] || empty($_POST['firstname'])) {
echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
echo "wassaup";
}

First you can create error array where you will put all yours errors
$error = array();
then when you check for multiple fields
if (empty($_POST['firstname'])) $error[] = "First name can't be empty";
if (empty($_POST['lastname'])) $error[] = "Last name can't be empty";
// and so on
after you do all of this make statement to check if error array is empty, if not display errors
if (empty($error)) {
// do something
} else {
// error exists you want to display all errors
foreach ($error as $value) {
echo '<ul><li>'.$value.'</li></ul>';
}
}

in the html form, you have to add the attribute required in html tag and without using the PHP processing
First name <input name="name" type="text" required="required">
and if the user doesn't enter his First name, he will get the error message (that the first name is required)

Related

PHP isset($_POST["Submit1"])) is not working

So I am trying to run this code and at the if (isset($_POST["Submit1"])) statement for isset, the form will not give the error message whenever I leave my form empty. It is supposed to give the $nameErr whenever the form is left empty, but it is not giving that error message. Whenever I delete the if (isset($_POST["Submit1"])) statement, it will run the else statement fine.
if (isset($_POST["Submit1"]))
{
if (isset($_POST['name']))
{
$name = sanitizeString($_POST['name']);
} else {
$nameErr = "* Your name must consist of letters and whitespace.";
}
}
Here is the submit button code.
<input type="submit" name="Submit1" value="Calculate">
As you can see, the names are the same so I don't think that is the problem.
Here is the rest of my code if you wish to take a look.
<!DOCTYPE html>
<html lang="en">
<head>
<title>GPA Improvement Calculator</title>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<h1>GPA Improvement Calculator</h1>
<p><span class="error">All form fields must be completed for the GPA calculator to function.</span></p>
<?php
function sanitizeString($var)
{
$var = stripslashes($var);
$var = strip_tags($var);
$var = htmlentities($var);
return $var;
}
$name = "";
$nameErr = "";
if (isset($_POST["Submit1"]))
{
if (isset($_POST['name']))
{
$name = sanitizeString($_POST['name']);
} else {
$nameErr = "* Your name must consist of letters and whitespace.";
}
}
?>
<form method="post" action="improveGPA.php">
Name: <input type="text" size="35" name="name" value="<?php echo $name; ?>">
<span class="error"><?php echo $nameErr; ?></span>
<br><br>
E-mail: <input type="text" size="35" name="email" value="">
<span class="error"></span>
<br><br>
<input type="checkbox" name="agree" >
I agree to the terms and conditions of this website.
<span class="error"></span>
<br><br>
Current GPA: <input type="text" size="4" name="currentGPA" value="">
<span class="error"></span>
<br><br>
Current Total Credits: <input type="text" size="3" name="currentCredits" value="">
<span class="error"></span>
<br><br>
I am taking <input type="text" size="3" name="newCredits" value="">
<span class="error"></span> credits this semester.
If I want to raise my GPA
<input type="text" size="4" name="GPAincrease" value="">
<span class="error"></span> points,
I need a <span style="font-weight: bold;">????</span> GPA on my courses this semester.
<br><br>
<input type="submit" name="Submit1" value="Calculate">
</form>
</body>
</html>
It will only start messing up if I have that one if statement in and just won't show that error message at all, when the form is empty or filled in.

PHP form doesn't validate, only gives blank page on submit

I decided to do a little test by testing to see if the form would detect an empty input field, and it didn't work, I don't know what the problem is and I don't want to write the rest of the project if this one small thing doesn't work so here's the code, I've looked over it and I don't think I've missed anything.
Here's the HTML:
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="POST" action="myform.php">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text">
Middle name <input name="middlename" type="text">
Surname <input name="lastname" type="text">
Age <input name="age" type="number">
Date of birth <input name="dob" type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input type="text"> State <input type="text">
City <input type="text">
Street number <input type="number">
Street name <input type="text"> <br><br>
Suburb <input type="text"> Postcode <input type="number">
If none of these apply to your accommodations, enter a typed location here <input type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text">
First time job seeker <input type="checkbox" name="check1" value="ftjb">
I'm a student <input type="checkbox" name="check2" value="ias">
Previous &/or most recent acedemic title <input name="school" type="text">
First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq">
I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival of all parties <input name="reason" type="text">
Date of arrival <input name="arrival" type="date">
Amount of stay expectancy
<input type="checkbox" name="check3">Temporary
<input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input type="text">
</fieldset>
<input type="submit" value="Submit"><button type="reset">Reset</button>
</form>
</body>
</html>
And here's what I've done in PHP so far for the form:
<?php
$nameInvalid = "";
$middleInvalid = "";
$surnameInvalid = "";
$ageInvalid = "";
$dobInvalid = "";
$countryInvalid = "";
$cityInvalid = "";
$strtInvalid = "";
$strnameInvalid = "";
$suburbInvalid = "";
$postcodeInvalid = "";
$jobInvalid = "";
$ftjsInvalid = "";
$iasInvalid = "";
$schoolInvalid = "";
$check1Invalid = "";
$check2Invalid = "";
$checl3Invalid = "";
$check4Invalid = "";
$reasonInvalid = "";
$arrivalInvalid = "";
if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameInvalid = "Name is required";
}
}
?>
Please add name="submit" attribute into the button.
<input type="submit" value="Submit" name="submit">
And also please add echo into the validation to print validation message like below.
if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
echo $nameInvalid = "Name is required";
}
}
Use this
<input type="submit" name="submit" value="Submit">
HTML
You need to use HTML5 validation to prevent the form from being sent if it doesn't conform to your rules.
From the official documentation:
The simplest HTML5 validation feature to use is the required attribute — if you want to make an input mandatory, you can mark the element using this attribute. When this attribute is set, the form won't submit (and will display an error message) when the input is empty (the input will also be considered invalid).
In your case, try adding the required tag to the fields you want to test, for instance the name field:
First name <input name="name" type="text" required>
To make it a little bit more fancy, add some CSS:
input:invalid {
border: 2px dashed red;
}
input:valid {
border: 2px solid black;
}
PHP
To validate PHP responses, all you need to do is the following:
if (!$_REQUEST["name"]) {
//If the name field is empty
die("Name is missing");
//Replace this with whatever logic fits your code
}
We're using $_REQUEST, which means both $_POST and $_GET, but you can stick to $_POST if you'd like. We're using if(!$val) to validate, it's the easiest way, but has caveats (see below), you can also use if(empty($val)) but for strings, the first is fine.
Get rid of the list of variables you define as empty in the beginning of your script. Instead look at setting an array if a value that shouldn't be empty, is empty. For instance:
# Repeat this structure for each form field you want
if (!$_REQUEST["name"]) {
//If the name field is empty
$empty_fields[] = "name";
//Fill the $empty_fields array with all the fields that are missing
}
# At the end, cycle thru the missing fields and tell the user
if(!empty($empty_fields)){
die("The following fields are missing: ".implode(", ", $empty_fields));
}
Caveat
Using a if(!$val) is a shortcut, and won't work if you allow values like " " (space) or "0" (zero), but works fine if you're expecting string values.
I checked code and its working properly. Just give name to submit button and echo validation message in php page.
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form method="POST" action="test1.php">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text">
Middle name <input name="middlename" type="text"> Surname <input name="lastname" type="text"> Age <input name="age" type="number"> Date of birth <input name="dob" type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input type="text"> State <input type="text"> City <input type="text">
Street number <input type="number"> Street name <input type="text"> <br><br>
Suburb <input type="text"> Postcode <input type="number"> If none of these apply to your accommodations, enter a typed location here <input type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text"> First time job seeker <input type="checkbox" name="check1" value="ftjb"> I'm a student <input type="checkbox" name="check2" value="ias"> Previous &/or most recent acedemic title <input name="school" type="text"> First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq"> I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival of all parties <input name="reason" type="text"> Date of arrival <input name="arrival" type="date"> Amount of stay expectancy <input type="checkbox" name="check3">Temporary <input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input type="text">
</fieldset>
<input type="submit" name="submit" value="Submit">
<button type="reset">Reset</button>
</form>
</body>
</html>
Display echo message in php page.
<?php
$nameInvalid = "";
$middleInvalid = "";
$surnameInvalid = "";
$ageInvalid = "";
$dobInvalid = "";
$countryInvalid = "";
$cityInvalid = "";
$strtInvalid = "";
$strnameInvalid = "";
$suburbInvalid = "";
$postcodeInvalid = "";
$jobInvalid = "";
$ftjsInvalid = "";
$iasInvalid = "";
$schoolInvalid = "";
$check1Invalid = "";
$check2Invalid = "";
$checl3Invalid = "";
$check4Invalid = "";
$reasonInvalid = "";
$arrivalInvalid = "";
if (isset($_POST['submit']))
{
if (empty($_POST["name"]))
{
echo $nameInvalid = "Name is required";
}
}
?>
Its done.

Concatenation email recipient address

This is a form that I get the user to enter data including their mobile number and mobile carrier. I am using concatenation of the mobile number and carrier to send a message to their phone. This works but I am having a problem with the concatenation for the email recipient. If I use a straight email address ("myemail#gmail.com"), it will deliver the content. It will not work using the concatenation of $phone and $carrier ($YourEmailAddress). I have tried several different methods but nothing is working. I have tried using "&" and "+". I need assistance in figuring out why the concatenation of the phone and carrier strings is not working. I am new to this site so I am not sure if I correctly posted this.
This is my php file:
<?php
if( count($_POST) )
{
$YourEmailSubject = "Form Submission From the Blog";
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$comment = stripslashes($_POST['comment']);
$phone = ($_POST['phone']);
$selectOption = $_POST['carrier'];
$content = "$name\r\n$email\r\n$comment\r\n$selectOption\r\n";
$YourEmailAddress = $phone."#".$selectOption;
mail($YourEmailAddress,$YourEmailSubject,$content,"From: ABC Company");
header("Location:" . (isset($_POST['redirect']) ? $_POST['redirect'] : '/') );
exit;
}
?>
This is my form:
<form method="post" action="/simplecontact.php">
<input type="hidden" name="redirect" value="//www.google.com">
<p>
Name:<br>
<input type="text" name="name" style="width:200px;"></td>
</p>
<p>
Email:<br>
<input type="text" name="email" style="width:200px;"></td>
</p>
<p>
Comment:<br>
<textarea name="comment" style="width:200px; height:100px"></textarea></td>
</p>
<p>Phone Number:<br>
<input type="text" id="number" name="number" /></td>
</p>
Carrier: <br>
<select id="carrier" name="carrier">
<option value="tmomail.net">T-mobile</option>
<option value="vmobl.com">Virgin Mobile</option>
<option value="cingularme.com">Cingular</option>
<option value="messaging.sprintpcs.com">Sprint</option>
<option value="txt.att.net">AT&T</option>
<option value="vtext.com">Verizon</option>
<option value="messaging.nextel.com">Nextel</option>
<option value="email.uscc.net">US Cellular</option>
<option value="sms.mycricket.com">Cricket</option>
<option value="mymetropcs.com">Metro PCS</option>
<option value="myboostmobile.com">Boost Mobile</option>
</select>
<p>
<input type="submit" style="width:200px;" value="Submit Form"></td>
</p>
</form>
In your form the phone input name is "number", in the php code you are trying to get the phone number like the input name was "phone"
Just change the input name to "phone"
<input type="text" id="number" name="phone" /></td>

header, session, php validation

I'm having a big problem with my validation form. Basically I created a form that will, once submitted, redirect to another page. To do this I used header("location: aaaaa.php") and apparently it works. The problem is that by doing this the validation doesn't work anymore, in fact if I don't enter any data and press submit I'll be redirected to the second page without getting the errors. If I delete the header the validation works again.
Moreover I have a big problem with the session method. I tried different way of using it to transfer the data to the second page when pressed the button submit, but it doesn't work and no one until now was able to help me. In the code that I'm gonna put below I deleted the session method and I was hoping that you would help me with that.
London Flight Agency
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name =($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["surname"])) {
$surnameErr = "Surname is required";
} else {
$surname =($_POST["surname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email =($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["telephone"])) {
$telephoneErr = "Number is required";
} else {
$telephone =($_POST["telephone"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9\_]{7,20}/",$telephone)) {
$telephoneErr = "Enter correct telephone number";
}
}
if (empty($_POST["date"])) {
$dateErr = "Date is required";
} else {
$date =($_POST["date"]);
// check if name only contains letters and whitespace
if (!preg_match("~^\\d{1,2}[/.-]\\d{2}[/.-]\\d{4}$~",$date)) {
$dateErr = "Enter correct date of birth";
}
}
if (empty($_POST["luggage"])) {
$luggageErr = "Choose one of the options";
} else {
$luggage =($_POST["luggage"]);
}
$weight = $_POST['weight'];
$height = $_POST['height'];
if(isset($_POST['submit']))
{
$total = ($weight+$height)/10;
}
header("Location: thankyou.php");
}
?>
<h2 id="title">London Flight Agency</h2><!-- This tag is used to define HTML heading (h1 to h6), this particular one defines the most important heading -->
<form id="form" method="post" name="myForm" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" onsubmit="return validateForm(this);"><!-- The <form> tag is used to create an HTML form for user input -->
<h4 class="subtitle"><strong>Personal Details</strong></h4>
<fieldset>
Enter here all your details (all of them are compulsory.)
<br />
<br />
<select name="Title" id="title" value="<?php echo $title;?>" onblur="validateSelect(name)">
<option value="empty">Title</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
</select><br /><br />
<span class="validateError" id="titleError" style="display: none;">You must select a title!</span>
<span id="error8"><?php echo $titleErr;?></span>
<table
<tr>
<td><label for="firstname">First Name:</label></td>
<td><input type="text" name="name" value="<?php echo $name;?>" id="name" onblur="validateName(name)"></td>
<span id="nameError" style="display: none;"></span>
<span id="error1"><?php echo $nameErr;?></span>
</tr>
<tr>
<td><label for="surname">Surname:</label></td>
<td><input type="text" name="surname" value="<?php echo $surname;?>" id="name" onblur="validateSurname(surname)"></td>
<span id="surnameError" style="display: none;"></span>
<span id="error2"><?php echo $surnameErr;?></span>
</tr>
<tr>
<td><label for="email">Email address:</label></td>
<td><input type="text" name="email" value="<?php echo $email;?>" id="email" onblur="validateEmail(email)"></td>
<span id="emailError" style="display: none;"></span>
<span id="error3"><?php echo $emailErr;?></span>
</tr>
<tr>
<td><label for="telephone">Telephone No:</label></td>
<td><input type="text" name="telephone" value="<?php echo $telephone;?>" id="telephone" onblur="validateTelephone(telephone)"></td>
<span id="telephoneError" style="display: none;"></span>
<span id="error4"><?php echo $telephoneErr;?></span>
</tr>
<tr>
<td><label for="date">Date of birth:</label></td>
<td><input type="text" name="date" value="<?php echo $date;?>" id="date" onblur="validateDate(date)"></td>
<span id="dateError" style="display: none;"></span>
<span id="error5"><?php echo $dateErr;?></span>
</tr>
</table>
</fieldset>
<h4 class="subtitle"><strong>Flight Details</strong></h4>
<fieldset>
<p>Hand luggage:</p><br />
<input type="radio" name="luggage" <?php if (isset($luggage) && $luggage=="Yes") echo "checked";?>value="Yes" id = "myRadio" required onblur="myFunction()">Yes
<input type="radio" name="luggage" <?php if (isset($luggage) && $luggage=="No") echo "checked";?>value="No" id = "myRadio" required onblur="myFunction()">No
<span id="luggageError" style="display: none;"></span>
<span id="error6"><?php echo $luggageErr;?></span>
<br /><br />
<label for="extrabag">Include free extra bag</label>
<input type="checkbox" name="extra" id="check" value="bag">
<br />
<br />
<select name="option" id="card" onblur="validatePayment(card)">
<option value="empty">Payment Card</option>
<option value="Visa">Visa Debit Card</option>
<option value="MasterCard">MasterCard</option>
<option value="PayPal">PayPal</option>
<option value="Maestro">Mestro</option>
<option value="Visa Electron">Visa Electron</option>
</select><br />
<span class="validateError" id="cardError" style="display: none;">You must select your payment card!</span>
</fieldset>
<h4 class="subtitle"><strong>Luggage Details</strong></h4>
<fieldset>
<p>Enter weight and height of your luggage.</p>
Your Weight(kg): <input type="text" name="weight" size="7"><br />
Your Height(cm): <input type="text" name="height" size="7"><br />
<span id="error7"><?php echo $measureErr;?></span>
<input type="button" value="Tot. price" onClick="totalPrice()"><br /><br />
Total Price: <input type="text" name="total" value="<?php echo $total?>" size="10"><br /><br />
<input type="reset" value="Reset">
<input type="submit" value="Submit" name="submit">
</fieldset>
</form>
</body>
It doesn't show the first part of the code for I don't know which reason. I'll post it below:
London Flight Agency
The problem Is that you are checking if the method is post then redirect to the new page.
You must check if the your entries are valid then redirect.
For Example You can make an array of data and use array_push to push new data whenever there is something invalid and then check like this :
if( count( $array ) == 0 )
header('Location:YOUR_NEW_PAGE.php');
This is one of the simplest solutions you can implement.
Good luck

Can't get php contact form to work

I have a webpage that has two contact forms and the second one is working fine but I can't seem to get the first one to work. I am fairly new to php. Thanks in advance for the help. Here's the html:
<h3>Message Us</h3>
<form action="?" method="POST" onsubmit="return saveScrollPositions(this);">
<input type="hidden" name="scrollx" id="scrollx" value="0" />
<input type="hidden" name="scrolly" id="scrolly" value="0" />
<div id="msgbox1">
<label for="name2">Name:</label>
<input type="text" id="name2" name="name2" placeholder=" Required" />
<label for="email2">Email:</label>
<input type="text" id="email2" name="email2" placeholder=" Required" />
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" placeholder="" />
<label for= "topic">Topic:</label>
<select id="topic" name="topic">
<option value="general">General</option>
<option value="stud">Property Price Enquiry</option>
<option value="sale">Bull Sale Enquiry</option>
</select>
</div><!--- msg box 1 -->
<div id="msgbox2">
<textarea id="message" name="message" rows="7" colums="25" placeholder="Your Message?"></textarea>
<p id="feedback2"><?php echo $feedback2; ?></p>
<input type="submit" value="Send Message" />
</div><!--- msg box 2 -->
</form><!--- end form -->
</div><!--- end message box -->
And here's the php:
<?php
$to2 = '418#hotmail.com'; $subject2 = 'MPG Website Enquiry';
$name2 = $_POST ['name2']; $email2 = $_POST ['email2']; $phone = $_POST ['phone']; $topic = $_POST ['topic']; $message = $_POST ['message'];
$body2 = <<<EMAIL
This is a message from $name2 Topic: $topic
Message: $message
From: $name2 Email: $email2 Phone: $phone
EMAIL;
$header2 = ' From: $email2';
if($_POST['submit']=='Send Message'){
if($name2 == '' || $email2 == '' || $message == ''){
$feedback2 = '*Please fill out all the fields';
}else {
mail($to2, $subject2, $body2, $header2);
$feedback2 = 'Thanks for the message. <br /> We will contact you soon!';
} } ?>
For the saveScrollPositions I use the following php and script:
<?php
$scrollx = 0;
$scrolly = 0;
if(!empty($_REQUEST['scrollx'])) {
$scrollx = $_REQUEST['scrollx'];
}
if(!empty($_REQUEST['scrolly'])) {
$scrolly = $_REQUEST['scrolly'];
}
?>
<script type="text/javascript">
window.scrollTo(<?php echo "$scrollx" ?>, <?php echo "$scrolly" ?>);
</script>
You have a mistake in your HTML part i.e.
<input type="submit" value="Send Message" />
add attribute name also in this input type like this-
<input type="submit" value="Send Message" name="submit" />
one thing more to correct in your php script is write-
$header2 = "From: ".$email2; instead of $header2 = ' From: $email2';
now try it.
What do you return in "saveScrollPositions" function? if you return false the form submit wont fire.

Categories