vaidoshia Posted December 1, 2011 Report Share Posted December 1, 2011 Hello everybody, I would like to ask for help, I made a form, it is validated before submitting it, however when I check it on the browser, it shows errors without even submitting a form, please help me to solve this problem. This is the code: <!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>Reservation Form</title> <link href="stylesheet/form.css" rel="stylesheet" type="text/css" /> </head> <?php require_once('includes/connection.php') ?> <body> <?php //Validation //Validating name: // testing if form data has been sent - $_POST['submit'] = submit button name if(isset($_POST['submitted'])){ $errors = array(); } if(!empty($_REQUEST['name']) && (!is_numeric($_POST['name']))) { $name = $_REQUEST['name']; } else { $errors[] = "You forgot to type a name <br />"; } if(!empty($_REQUEST['last_name']) && (!is_numeric($_POST['last_name']))) { $name = $_REQUEST['last_name']; } else { $errors[] = "You forgot to type your last name <br />"; } if(!empty($_REQUEST['phone']) && (!is_numeric($_POST['phone'])) &&(preg_match('/^[0-9+$/i', $phone))) { $name = $_REQUEST['phone']; } else { $errors[] = "You forgot to type your phone <br />"; } if(!empty($_REQUEST['email']) && (is_numeric($_POST['email'])) &&(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-]) ↪*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))) { $name = $_REQUEST['email']; } else { $errors[] = "You forgot to type your email <br />"; } if(isset($_REQUEST['months'])) { $months = $_REQUEST['months']; } else { $errors[] = "You forgot to select a date <br />"; } if(isset($_REQUEST['day'])) { $day = $_REQUEST['day']; } else { $errors[] = "You forgot to select a date <br />"; } if(isset($_REQUEST['years'])) { $years = $_REQUEST['years']; } else { $errors[] = "You forgot to select a date <br />"; } if(isset($_REQUEST['hour'])) { $hour = $_REQUEST['hour']; } else { $errors[] = "You forgot to select hour <br />"; } if(!empty($_REQUEST['guests']) && (is_numeric($_POST['guests']))) { $guests = $_REQUEST['guests']; } else { $errors[] = "You forgot to type number of guests<br />"; } //Validating not obligatory data if(!empty($_REQUEST['comments']) && (!is_numeric($_POST['comments']))) { $comments = $_REQUEST['comments']; } else { $comments = NULL ; } if(empty($errors)){ echo "SUCCESS – thank you for making a reservation<br /><br />"; } else{ echo "<p style=\"color:#F00\">"; foreach($errors as $errormessages){ echo "$errormessages <br /><br />"; } echo "</p>"; } ?> <form name ="Reservation" method ="POST" action = "reservation.php" id="form"> <fieldset> <label>Name</label><input type="text" id="name" name="name" value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>" /><br /> <label>Last name</label><input type="text" id="last_name" name="last_name" value="<?php if(isset($_POST['last_name'])){ echo $_POST['last_name']; } ?>" /><br /> <label>Phone</label><input type="text" id="lastname" name="phone" value="<?php if(isset($_POST['phone'])){ echo $_POST['phone']; } ?>" /><br /> <label>Email</label><input type="text" id="email" name="email" value="<?php if(isset($_POST['email'])){ echo $_POST['email']; } ?>" /><br /> <label>Select Date </label> <?php //make month array $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); //month pull down menu: echo '<select name="months">'; foreach ($months as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } echo '</select>'; //Make the days pull-down menu: echo '<select name ="day">'; for ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\">$day</option>\n"; } echo '</select>'; //Make years pull down menu: echo '<select name="year">'; for($year = 2008; $year<= 2018; $year++) { echo "<option value=\"$year\">$year</option>\n"; } echo '</select>'; ?> <br /> <label>Hour</label> <?php //Make hours pull-down menu: echo '<select name ="hour">'; for ($hour = 10; $hour <= 23; $hour++) { echo "<option value=\"$hour\">$hour</option>\n"; } echo '</select>'; ?> <br /> <label>Guests</label><input type="text" id="guests" name="guests" value="<?php if(isset($_POST['guests'])){ echo $_POST['guests']; } ?>" /><br /> <label>Comments</label><textarea name="comments" id="comments" cols="30" rows="5"><?php if(isset($_POST['comments'])){ echo $_POST['comments']; } ?></textarea><br /> <input class="submit" type="submit" id="submit" name="submit" value="Send reservation" /> </fieldset> </form> <?php mysqli_close($connection); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
sbsmith Posted December 2, 2011 Report Share Posted December 2, 2011 If your going to do it this way then you may need to nest a series of if else statements inside your first if statement (ie where you check if your form is submitted.) if(isset($_POST['submitted'])){ $errors = array(); } I'll try to explain... When you first load page in broswer PHP is first going to check if your form is submitted. Obviously your form will not be submitted when you first load page. So PHP will return false on first conditional check. This means your first if statement will be ignored. PHP will continue to your next if statement... if(!empty($_REQUEST['name']) && (!is_numeric($_POST['name']))) { $name = $_REQUEST['name']; } else { $errors[] = "You forgot to type a name <br />"; } Now your first condition will be false so php will be forced to set your error variable evertime you load page without actually submitting form... else { $errors[] = "You forgot to type a name <br />"; } If you want to do a series of form validation checks then you may need to do nested if else statement when your form is submitted.. (ie inside of your first if statement) if(isset($_POST['submitted'])){ $errors = array(); } if your not member already there is massive amount of great video tutorials and solid information about php and javascript form validation inside members area that addresses all of this in much greater detail. http://www.killersites.com/university/ Quote Link to comment Share on other sites More sharing options...
sbsmith Posted December 2, 2011 Report Share Posted December 2, 2011 OK Here is something to get you started... I saw that in original form your submitting your forms data to to "reservation.php" in my example below i'm submitting form back to itself. But if you want to submit form data to 'reservation.php' you will just probably need to do your form validation within the 'reservation.php' file. PHP <?php $input['name'] = ''; $input['last_name'] = ''; $input['phone'] = ''; $input['email'] = ''; $input['guests'] = ''; $input['comments'] = ''; $error['name'] = ''; $error['last_name'] = ''; $error['phone'] = ''; $error['email'] = ''; $error['months'] = ''; $error['year'] = ''; $error['day'] = ''; $error['hour'] = ''; $error['guests'] = ''; $error['comments'] = ''; $error['alert'] = ''; $success = ''; //Validation //Validating name: // testing if form data has been sent - $_POST['submit'] = submit button name if(isset($_POST['submit'])) { //Process the form validation checks foreach($_POST as $input_name => $input_val ) { $input[$input_name] = htmlentities($input_val, ENT_QUOTES); } if( $input['name'] == '' || $input['last_name'] == '' || $input['phone'] =='' || $input['email'] == '' || $input['guests'] == '' || $input['comments'] == '' || $input['year'] == '' || $input['months'] == '' || $input['day'] == '' || $input['hour'] == '') { foreach($input as $input_name => $input_val ) { //check required form inputs are not empty if($input[$input_name] == '') { // set error msg for required fields. $error[$input_name] = '<div class="error"> This is a Required Field</div>'; } $error['alert'] = 'Please fill all Required fields'; } } // Good place to test your regex at => http://www.gskinner.com/RegExr/ // #^\d{2}(-\d{3}){2}(\d{2})?$# else if( !preg_match('#^\d+$#', $input['phone'] ) ) { $error['phone'] = '<div class="error">Digits Only</div>'; $error['alert'] = 'Phone can only contain digits'; } // validate email address else if (!preg_match('#^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$#', $input['email'])) { $error['email'] = '<div class="error">Invalid Email Address</div>'; $error['alert'] = 'Invalid Email Address'; } else { // form data passes your validation checks do as you wish... $success = "SUCCESS – thank you for making a reservation<br /><br />"; } } if($error['alert'] != ''){echo "<div class='alert'>{$error['alert']}</div>";} if($success != ''){echo "<div class='success'>{$success}</div>";} ?> The HTML Form that you wrote only changed some variables <form name ="Reservation" method="post" action="" id="form"> <fieldset> <label>Name</label><input type="text" id="name" name="name" value="<?php echo $input['name']; ?>" /><br /> <?php echo $error['name']; ?> <label>Last name</label><input type="text" id="last_name" name="last_name" value="<?php echo $input['last_name']; ?>" /><br /> <?php echo $error['last_name']; ?> <label>Phone</label><input type="text" id="lastname" name="phone" value="<?php echo $input['phone']; ?>" /><br /> <?php echo $error['phone']; ?> <label>Email</label><input type="text" id="email" name="email" value="<?php echo $input['email']; ?>" /><br /> <?php echo $error['email']; ?> <label>Select Date </label> <?php //make month array $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); //month pull down menu: echo '<select name="months">'; foreach ($months as $key => $value) { echo "<option value=\"$key\">$value</option>\n"; } echo '</select>'; echo $error['months']; //Make the days pull-down menu: echo '<select name ="day">'; for ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\">$day</option>\n"; } echo '</select>'; echo $error['day']; //Make years pull down menu: echo '<select name="year">'; for($year = 2008; $year<= 2018; $year++) { echo "<option value=\"$year\">$year</option>\n"; } echo '</select>'; echo $error['year']; ?> <br /> <label>Hour</label> <?php //Make hours pull-down menu: echo '<select name ="hour">'; for ($hour = 10; $hour <= 23; $hour++) { echo "<option value=\"$hour\">$hour</option>\n"; } echo '</select>'; echo $error['hour']; ?> <br /> <label>Guests</label><input type="text" id="guests" name="guests" value="<?php echo $input['guests']; ?>" /><br /> <?php echo $error['guests']; ?> <label>Comments</label><textarea name="comments" id="comments" cols="30" rows="5"><?php echo $input['comments'];?></textarea><br /> <?php echo $error['comments']; ?> <input class="submit" type="submit" id="submit" name="submit" value="Send reservation" /> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
vaidoshia Posted December 3, 2011 Author Report Share Posted December 3, 2011 Thank you, it really helped me to understand and change errors :] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.