form verification script

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
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

form verification script

Post by meander »

just as a test to see if i could do it (since im a noob at this) i made a 3 text box form and then i tried to make a script to verify that but it didn't work. i cant find anything wrong.

here is the form...

Code: Select all

<form action="verify.php" method="post" name="frmMain" id="frmMain">
  <p>First Name: 
    <input name="first_name" type="text" id="first_name">
  </p>
  <p> Last Name: 
    <input name="last_name" type="text" id="last_name">

  </p>
  <p>Favorite Number: 
    <input name="fav_num" type="text" id="fav_num">
  </p>
  <p align="center"> 
    <input type="submit" name="Submit" value="Submit"> 
    <input type="reset" name="Submit2" value="Reset">
  </p>
</form>
here is my script:

Code: Select all

<?php
if (empty($_POST['first_name'])) {
	$fn = FALSE;
	$message .= '<p>You forgot to enter your first name!</p>';
} else {
	$fn = $_POST['first_name'];
}

if (empty($_POST['last_name'])) {
	$ln = FALSE;
	$message .= '<p>You forgot to enter your last name!</p>';
} else {
	$ln = $_POST['last_name'];
}

if (empty($_POST['fav_num'])) {
	$n = FALSE;
	$message .= '<p>You forgot to enter your favorite number!</p>';
} else {
	$n = $_POST['fav_num'];
}

if ($fn && $ln && $n) {
	echo '<p>Thank You!</p>';
} else {
	echo $message;
}

?>
and here is what i am getting for output...

Code: Select all

You forgot to enter your first name!

'; &#125; else &#123; $fn = $_POST&#1111;'first_name']; &#125; if (empty($_POST&#1111;'last_name'])) &#123; $ln = FALSE; $message .= '

You forgot to enter your last name!
'; &#125; else &#123; $ln = $_POST&#1111;'last_name']; &#125; if (empty($_POST&#1111;'fav_num'])) &#123; $n = FALSE; $message .= '

You forgot to enter your favorite number!
'; &#125; else &#123; $n = $_POST&#1111;'fav_num']; &#125; if ($fn &amp;&amp; $ln &amp;&amp; $n) &#123; echo '

Thank You!
'; &#125; else &#123; echo $message; &#125; ?&gt;
and that is when all fields are filled out. have any idea?
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Hi,

no idea, I get thank you if all fields are filled. If not all fields are filled I get the according error message.

djot
-
jbr
Forum Newbie
Posts: 2
Joined: Mon Oct 11, 2004 9:26 pm

Post by jbr »

Hi


The problem is this line!

Code: Select all

if ($fn &amp;&amp; $ln &amp;&amp; $n) &#123;



Now there other things that are not wrong, but using better logic you can write the same thing with less code!

Look at at your code!

Each if() block you assign (2) values if the if() returns empty()

example $_POST['first_name'];

on false... (you set these two values)

$fn = FALSE;
$message .= '<p>You forgot to enter your first name';

The $fn is not needed, all you are doing is using more memory when you don't need to!


example....



Code: Select all

<?

$message = '';

if ( empty ( $_POST['first_name'] ) )
{
	$message .= '<p>You forgot to enter your first name!</p>';
}

if ( empty ( $_POST['last_name'] ) )
{
	$message .= '<p>You forgot to enter your last name!</p>';
}

if ( empty ( $_POST['fav_num'] ) )
{
	$message .= '<p>You forgot to enter your favorite number!</p>';
}

if ( !empty ( $message ) )
{
	echo $message;
}
else
{
	echo '<p>Thank You!</p>';
}

?>
See by doing it using simple logic you not only save resources, but you will make your code easier to understand!

Also doing this...

$fn = $_POST['first_name'];

Is not always the best idea, because you are only assigning a value to a $var when that value is already in the $var -> $_POST['first_name'];, you now are using double the memory space to hold (2) variables that contain the same value!


jbr





?>
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post by meander »

ok that makes a lot of sense ill have to try it.

I think that i must have php incorectly set up or something because it gave me this in firefox:

Code: Select all

You forgot to enter your first name!

'; &#125; if ( empty ( $_POST&#1111;'last_name'] ) ) &#123; $message .= '

You forgot to enter your last name!
'; &#125; if ( empty ( $_POST&#1111;'fav_num'] ) ) &#123; $message .= '

You forgot to enter your favorite number!
'; &#125; if ( !empty ( $message ) ) &#123; echo $message; &#125; else &#123; echo '

Thank You!
'; &#125; ?>
and nothing at all in IE

How could i have set up PHP wrong? isnt it just click install and thats it?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

try using " around your message var instead of '
also you can try

Code: Select all

<?php
if(!isset($_POST['fname']))
  $message.="no first name ";
?>
Post Reply