Page 1 of 1
Basic VALIDATION
Posted: Thu Oct 05, 2006 8:36 pm
by tetsuO2
I coded below. it works but i' d like to show message like "You have to fill every fields".
how can i code for this?
Code: Select all
<html>
<body>
<form action="nextpage.php" method="POST">
Name:<input type='text' name='name'><br>
Address:<input type='text' name='address'><br>
<input type='submit' name='go'>
</form>
</body>
</html>
Code: Select all
<?php
if(($_POST["name"]==null)||($_POST["address"]==null))
{
header('Location:thispage.php');
}
else
{
echo "Thanks!!.your name is".$_POST["name"];
echo $_POST["address"];
}
?>
hopefully i'd like to make like this link...
http://www.studyinvancouver.com/weigman ... /login.php
Thanks in adavence!
Posted: Thu Oct 05, 2006 9:18 pm
by waradmin
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:
Code: Select all
<script language="Javascript">
alert ("Please fill in ALL fields!")
</script>
Posted: Thu Oct 05, 2006 9:33 pm
by bob_the _builder
Hi,
Personally I would flag javascript as a means of field checking ..
Here is some food for thought:
Code: Select all
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 />
Address: <input type="text" name="address" value="'.$_POST['address'].'">'.$error1.'<br />
<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>';
}
hth
Posted: Fri Oct 06, 2006 1:08 am
by tetsuO2
Thank you so much. i understad that.
but, what i' d like to do is to dispay "error messages" like this link.
http://www.gorentcanada.com/mapcom/logi ... newaccount
but he used javascript. but i' d like to know how to do that by PHP!!
actually i tried like this.
Code: Select all
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.
Thanks a lot.
Posted: Fri Oct 06, 2006 1:42 am
by Christopher
You can use Javascript, especially if you want to display nice messages. But you will still need to validate on the server side for security reasons.
Posted: Fri Oct 06, 2006 3:23 am
by bob_the _builder
Hi,
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:
Code: Select all
function ShowForm() {
if(empty($_POST['name'])) {
$error = '<b><font color="red">Error!</font></b> Please enter your name!<br />';
$image = '<img src="../attention.png" />';
}
if(empty($_POST['address'])) {
$error1 = '<b><font color="red">Error!</font></b> Please enter your email address!<br />';
$image1 = '<img src="../attention.png" />';
}
$form = '
<form method="post" action="'.$_SERVER['PHP_SELF'].'" />
Name: <input type="text" name="name" value="'.$_POST['name'].'" /> '.$image.'<br />'.$error.'<br />
Address: <input type="text" name="address" value="'.$_POST['address'].'"> '.$image1.'<br />'.$error1.'<br />
<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>';
}
Good luck
Posted: Fri Oct 06, 2006 4:39 am
by RobertGonzalez
Code: Select all
<?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
}
?>