PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Well do an if, like you did with ||'s for each field. And if thats true, have it echo "Please fill in all fields", or heck even do a Javascript alert window like:
function ShowForm() {
if(empty($_POST['name'])) { $error = '*'; }
if(empty($_POST['address'])) { $error1 = '*'; }
$form = '
Fields marked * are required to submit this form:<br /><br />
<form method="post" action="'.$_SERVER['PHP_SELF'].'">
Name: <input type="text" name="name" value="'.$_POST['name'].'">'.$error.'<br />
echo "You have to fill in name form";
//i coded like this to try to display 'error message'. but of cos this code does not work coz
//when i open this page, the error meaages are already appear coz $_POST['address'] and $_POST["name"]
// are empty when i open this page.
//so i need to put like DEFAULT=" "; but i dont know how to code for default=empty......
Address: <input type="text" name="address" value="'.$_POST['address'].'">'.$error1.'<br />
echo "you have to fill in address.";
<input type="submit" value="Submit" name="go">
</form>';
return($form);
}
if( (empty($_POST["name"])) || (empty($_POST["address"])) ) {
echo ShowForm();
}else{
echo 'Your name is: <b>'.$_POST['name'].'</b><br />';
echo 'Your address is: <b>'.$_POST['address'].'</b>';
}
As i said, i'd like to dispay 'error meaasge...'
so i seem to need to code like DEFAULT=" " .
If somebody knows about that, please teach me how to code.
Im thinking there is no javascript in that error checking .. even if there is im sure you can do some thing similar with the following code and some css to create the pretty border around the error messages.
The code can be writen much better, but I will leave that up to you .. im just giving you a base to work off:
<?php
$is_error = true;
$error_message = '';
$name = ( isset($_POST['name']) ) ? $_POST['name'] : '';
$address = ( isset($_POST['address']) ) ? $_POST['address'] : '';
if (isset($POST['some_form_field_to_check_against']))
{
/*
The form was posted, lets do some checking
In reality, you should be checking the data as well
*/
if (empty($name) || empty($address))
{
if (empty($name))
{
$error_message .= '<p><span style="color: #ff0000; font-weight: bold;">You must complete the name field!</span></p>';
}
if (empty($address))
{
$error_message .= '<p><span style="color: #ff0000; font-weight: bold;">You must complete the name field!</span></p>';
}
}
if (empty($error_message))
{
$is_error = false;
}
}
if (!$is_error)
{
// Proceed with processing as things are OK
}
else
{
// Show the form with the var $error_message just before it
}
?>