...

View Full Version : server side validation if else syntax help



calebandchels
11-19-2010, 07:56 PM
Im still a beginner with php and using a book to learn. I'm sure its some kind of syntax error but any help would be great. I am trying to do basic server side validation and when the variable $output_form is true I want it to display the form. The form isn't showing when I first load the page but I used

if(isset($_POST['submit'] to try and correct this. Please help


if(isset($_POST['submit'])) {

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$user_name = $_POST['user_name'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$email = $_POST['email'];
$output_form = false;

if(!empty($first_name) && !empty($last_name) && !empty($user_name) && !empty($password) && !empty($repassword) && !empty($email)){

}else $output_form = true; //end if

if($output_form == true){
?>

<form method = "post"
action = "<?php echo $_SERVER['PHP_SELF']; ?>"
name = "sign_up_form"
onSubmit = "return validate_signup ();">
<label for = "first_name">First Name:</label>
<input type = "text"
name = "first_name"
id = "first_name" /> <br />
<label for = "last_name">Last Name:</label>
<input type = "text"
name = "last_name"
id = "last_name" /> <br />
<label for = "user_name">User Name:</label>
<input type = "text"
name = "user_name"
id = "user_name" /> <br />
<label for = "password">Password:</label>
<input type = "text"
name = "password"
id = "password" /> <br />
<label for = "repassword">Retype Password:</label>
<input type = "text"
name = "repassword"
id = "repassword" /> <br />
<label for = "email">E-Mail:</label>
<input type = "text"
name = "email"
id = "email" /> <br />
<button type = "submit"
name = "submit">Submit</button>
</form>
<?php
}}
?>

MattF
11-19-2010, 08:34 PM
Validate and sanitise your input.



<?php

if(isset($_POST['form_sent']))
{

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$user_name = $_POST['user_name'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$email = $_POST['email'];
$output_form = false;

if(!empty($first_name) && !empty($last_name) && !empty($user_name) && !empty($password) && !empty($repassword) && !empty($email))
{
[processing code here]
}
}
else
{
?>
<form method = "post"
action = "<?php echo $_SERVER['PHP_SELF']; ?>"
name = "sign_up_form"
onSubmit = "return validate_signup ();">
<input type="hidden" name="form_sent"/>
<label for = "first_name">First Name:</label>
<input type = "text"
name = "first_name"
id = "first_name" /> <br />
<label for = "last_name">Last Name:</label>
<input type = "text"
name = "last_name"
id = "last_name" /> <br />
<label for = "user_name">User Name:</label>
<input type = "text"
name = "user_name"
id = "user_name" /> <br />
<label for = "password">Password:</label>
<input type = "text"
name = "password"
id = "password" /> <br />
<label for = "repassword">Retype Password:</label>
<input type = "text"
name = "repassword"
id = "repassword" /> <br />
<label for = "email">E-Mail:</label>
<input type = "text"
name = "email"
id = "email" /> <br />
<button type = "submit"
name = "submit">Submit</button>
</form>
<?php
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum