<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
User name:<input type="text" name="user_name"><p>
Email: <input type="text" name="user_email"><p>
Language:
<input type="radio" name="language" value="php">PHP
<input type="radio" name="language" value="python">Python
<p>
Application type:
<select name="type">
<option value="">Select</option>
<option value="desktop">Desktop</option>
<option value="web">Web</option>
<option value="network">Network</option>
</select>
<input type="hidden" name="submit" value="1">
<input type="Submit" value="Go">
</form>
Note the hidden input type. Its value will be used to decide if the user submitted the form or not. This way is safer and easier than testing for input type submit.
<?php
function inputData(){?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
User name:<input type="text" name="user_name"><p>
Email: <input type="text" name="user_email"><p>
Language:
<input type="radio" name="language" value="php">PHP
<input type="radio" name="language" value="python">Python
<p>
Application type:
<select name="type">
<option value="">Select</option>
<option value="desktop">Desktop</option>
<option value="web">Web</option>
<option value="network">Network</option>
</select>
<input type="hidden" name="submit" value="1">
<input type="Submit" value="Go">
</form>
<?php
}
function processData(){
}
function validateData($data){
}
?>
if($_POST["submit"]){
$data = $_POST;
if (validateData($data)){
inputData();
} else {
processData();
}
} else {
inputData();
}
Nothing out of an ordinary here (yet). If variable $_POST[]"submit"], which comes from that hidden field in the form, is true then it will try to validate the input (which is now in $data array). If validation succeeds (next step) it will go to process the data, if not - display the form again.
function validateData($data){
if(trim($data['user_name']) == ""){
$errors[] = "User name cannot be empty";
}
if(!trim(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $data['user_email']))){
$errors[] = "Invalid email format";
}
if(!isset($data['language'])){
$errors[] = "No language was chosen";
}
if($data['type'] == "0"){
$errors[] = "Select type of application";
}
return $errors;
}
if($_POST["submit"]){
$data = $_POST;
if ($errors = validateData($data)){
inputData($errors);
} else {
processData();
}
} else {
inputData();
}
What's happening is that for every error it detects a message is recorded into $errors array. So if there is at least one error, $errors = validateData($data) will return true and go to inputData. If array is empty it will return false and processData instead.
function inputData($errors = ''){
if($errors != ""){
echo "There were problems with your input:<ul>";
foreach($errors as $error){
echo "<li>".$error."</li>";
}
echo "</ul>";
}?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Now you should see why I assign the output of validateData in the if statement: $errors = validateData($data). I pass the error messages as a parameter to inputData function, which is empty by default. If there are any errors they are displayed in UL/LI list. Format them as you wish.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
User name:<input type="text" name="user_name" value="<?php echo $data['user_name']; ?>"><p>
Email: <input type="text" name="user_email" value="<?php echo $data['user_email']; ?>"><p>
Language:
<input type="radio" name="language" value="php" <?php if($data['language']=='php'){ echo 'checked'; }?> >PHP
<input type="radio" name="language" value="python" <?php if($data['language']=='python'){ echo 'checked'; }?> >Python
<p>
Application type:
<select name="type">
<option value="0">Select</option>
<option value="desktop" <?php if($data['type']=='desktop'){ echo 'selected'; }?> >Desktop</option>
<option value="web" <?php if($data['type']=='web'){ echo 'selected'; }?> >Web</option>
<option value="network" <?php if($data['type']=='network'){ echo 'selected'; }?> >Network</option>
</select>
<input type="hidden" name="submit" value="1">
<input type="Submit" value="Go">
</form>
$data has to come from somewhere, so just add it as another parameter in function declaration:
function processData($data){
print_r($data);
}
function notEmpty($field){
if(trim($field) == ""){
return 1;
} else {
return 0;
}
}
function isNumeric(){
}
include('validators.php');
function validateData($data){
if(notEmpty($data['user_name'])){
$errors[] = "User name cannot be empty";
}
if(notEmpty($data['user_name'])){
$errors[] = "User name cannot be empty";
} else {
$sqlCheckUser = "SELECT COUNT(*) FROM users WHERE user_name = '".$data['user_name']."'";
$rsCheckUser = mysqli_query($dblink,$sqlCheckUser);
$user_num = mysqli_fetch_row($rsCheckUser);
$user_num = $user_num[0];
if($user_num > 0){
$errors[] = "User name already taken";
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Except eregi has just been deprecated on php 5.3; You could rewrite your code for preg_match!
Cool article!
Commented:
Open in new window