I am a novis at this and I´m trying to make a form work for a Bed and Breakfast rental.
My goal is that on submission if a field is left empty a little box should appear saying “Please fill in your last name” for example.
Ivé tried pasting together different bits of code but nothing works.
Is there anyone who could find it in their heart to help a php-idiot?
This is the code I have now:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8” />
<title>New Reservations</title>
</head>
<body>
<?php
//--------------------------Set these paramaters--------------------------
$subject = ‘New rental request’;
// Your email address. This is where the form information will be sent.
$to = ‘my email adress’;
// Where to redirect after form is processed.
$url = ‘http://www.mysite.com/thanks.html’;
// Makes all fields required. If set to ‘1’ no field can not be empty. If set to ‘0’ any or all fields can be empty.
$req = ‘1’;
// --------------------------Do not edit below this line--------------------------
$text = "Results from form:
";
$space = ’ ';
$line = ‘’;
if ($_POST[‘submitted’]==1) {
//something
}
if ($_POST[‘submitted’]==1) {
$errorMsg = “”; // error messages variable
}
foreach ($_POST as $key => $value)
{
if ($req == ‘1’)
{
if ($value == ‘’)
echo ’ <script type=“text/javascript”>
alert(“‘.$errorMsg.’”);
</script> ';
}
$j = strlen($key);
if ($j >= 40)
{echo “Name of form element $key cannot be longer than 40 characters”;die;}
$j = 40 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ’ ‘;}
$value = str_replace(’
‘, “$line”, $value);
$conc = “{$key}:$space{$value}$line”;
$text .= $conc;
$space = ’ ‘;
}
mail($emailadd, $subject, $text, ‘From: ‘.$emailadd.’’);
echo ‘<META HTTP-EQUIV=Refresh CONTENT="0; URL=’.$url.’">’;
?>
</body>
</html>
Thanks, Rundblomman
Hi,
There is an excellent article on PHP form validation here: http://phpmaster.com/form-validation-with-php/
If you read this, you should be able to accomplish what you want.
By way of a small example, here is a PHP script with two input fields. The field “Last name” is a compulsory field.
Upload the script to your server, name it “validation_example.php” and have a play around with it.
You might also notice the technique for repopulating the field “First name” in the event that a first name is entered, a last name is omitted and the form is submitted.
If you have any further questions, just let me know.
<?php
$first_name = "";
$last_name = "";
//When the submit button is pressed this code will trigger
if ($_POST['process'] == 1) {
$error = "";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
if (empty($last_name)){
$error_message .= "Your last name cannot be blank";
}
if (empty($error_message)) {
//Do some stuff here, then set header to redirect
header("location: http://hibbard.eu");
} else {
$error="on";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form validation example</title>
<style>
.error{color:red;}
input{display:block; margin-bottom:10px;}
</style>
</head>
<body>
<h1>Form Validation Example</h1>
<h2>Please enter your name</h2>
<?php
if ($error == "on"){
echo "<p class=\\"error\\">".$error_message."</p>";
}
?>
<form action="validation_example.php" method="post">
<label for="first_name">First name:</label>
<input name="first_name" type="text" value="<?php echo htmlspecialchars($first_name); ?>" />
<label for="last_name">Last name:</label>
<input name="last_name" type="text" value="<?php echo htmlspecialchars($last_name); ?>" />
<input type="hidden" name="process" value="1" />
<input type="submit" name="Button1" value="Submit" />
</form>
</body>
</html>
Have you considered using HTML5’s required input attribute, apposed to PHP validation?
Although it does not work in IE or Safari, the required attribute will prevent form submission unless the required forms are filled.
<form action="validation_example.php" method="post">
<label for="first_name">First name:</label>
<input name="first_name" type="text" value="<?php echo htmlspecialchars($first_name); ?>" required />
<label for="last_name">Last name:</label>
<input name="last_name" type="text" value="<?php echo htmlspecialchars($last_name); ?>" required />
<input type="hidden" name="process" value="1" />
<input type="submit" name="Button1" value="Submit" />
</form>
If the user attempts to submit the form without filling out the require inputs, a bubble will appear over the ignored input saying “Please fill out this form”.
It’s a quick and easy alternative, but I still suggest using PHP validation as well.