Basic VALIDATION

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!

Moderator: General Moderators

Post Reply
tetsuO2
Forum Newbie
Posts: 16
Joined: Thu Aug 10, 2006 6:33 am

Basic VALIDATION

Post 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!
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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>
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post 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
tetsuO2
Forum Newbie
Posts: 16
Joined: Thu Aug 10, 2006 6:33 am

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
}
?>
Post Reply