simple form validation..help needed

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
jonleow
-


Joined: 29 Apr 2004
Posts: 48

PostPosted: Sun Apr 09, 2006 6:41 pm    Post subject: simple form validation..help needed Reply with quote

i have a form which i need to validate. Form is as follow:

<form action="processform1.php" method="post">

<table width="75%" border="0">
<tr>
<td width="15%">First Name</td>
<td width="85%"><input type="text" name="firstName" size="50"></td>
</tr>
<tr>
<td>Midldle Name</td>
<td><input name="middleName" type="text" size="50"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input name="lastName" type="text" size="50"></td>
</tr>
</table>

<td><input type="submit" name="Submit Now"></td>
</form>

And this is my validating program:

<?php

if ($firstName == "")
echo "Please enter your firstName";
elseif ($middleName =="")
echo "PLease enter your middleName";
elseif ($lastName == "")
echo "Please neter your lastName";

?>



ANd i got this error while validating the form:
Notice: Undefined variable: firstName in C:\Apache Group\Apache2\htdocs\pg\processform1.php on line 8


Can someone explain to me why is this so? Thank you.
Back to top View user's profile Send private message MSN Messenger
Yami King
-


Joined: 08 Sep 2005
Posts: 120

PostPosted: Sun Apr 09, 2006 7:08 pm    Post subject: Reply with quote

$firstname isn't declared so it returns an error. What you could do is, what I always do:
Code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="75%" border="0">
<tr>
<td width="15%">First Name</td>
<td width="85%"><input type="text" name="firstName" size="50"></td>
</tr>
<tr>
<td>Midldle Name</td>
<td><input name="middleName" type="text" size="50"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input name="lastName" type="text" size="50"></td>
</tr>
</table>
<td><input type="submit" name="submit" value="Submit Now"></td>
</form>

<?php
$submit = (mysql_escape_string($_POST['submit']) == "Submit Now") ? true : false;
$firstName = mysql_escape_string($_POST['firstName']);
$middleName = mysql_escape_string($_POST['middleName']);
$lastName = mysql_escape_string($_POST['lastName']);

if ($submit) {
   if ($firstName == "") {
      echo "Please enter your firstName";
   }
   elseif ($middleName =="") {
      echo "PLease enter your middleName";
   }
   elseif ($lastName == "") {
      echo "Please neter your lastName";
   }
   else {
      # your code here
   }
}
?>


As you can see I wrapped it up in 1 single page.
To make the chances of an injection or something likely smalller I use mysql_escape_string().
I also used brackets for the if-elseif statement which makes the if-elseif statement execute quicker.
The ? and : are also an if-else statement, where ? is the if and : is the else.
Back to top View user's profile Send private message Send e-mail
jonleow
-


Joined: 29 Apr 2004
Posts: 48

PostPosted: Sun Apr 09, 2006 7:17 pm    Post subject: Reply with quote

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">

if the validating coding is residing in another page, what must i modify in the above line of code?

<form action="<?php $_SERVER['processform.php]; ?>" method="post"> ?
Back to top View user's profile Send private message MSN Messenger
Yami King
-


Joined: 08 Sep 2005
Posts: 120

PostPosted: Sun Apr 09, 2006 7:27 pm    Post subject: Reply with quote

I said I put it in one single page, but if you want multiple pages you use this:
Code:
<!-- page with the form -->
<form action="processform1.php" method="post">
<table width="75%" border="0">
<tr>
<td width="15%">First Name</td>
<td width="85%"><input type="text" name="firstName" size="50"></td>
</tr>
<tr>
<td>Midldle Name</td>
<td><input name="middleName" type="text" size="50"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input name="lastName" type="text" size="50"></td>
</tr>
</table>
<td><input type="submit" name="submit" value="Submit Now"></td>
</form>

<!-- processform1.php -->
<?php
$submit = (mysql_escape_string($_POST['submit']) == "Submit Now") ? true : false;
$firstName = mysql_escape_string($_POST['firstName']);
$middleName = mysql_escape_string($_POST['middleName']);
$lastName = mysql_escape_string($_POST['lastName']);

if ($submit) {
   if ($firstName == "") {
      echo "Please enter your firstName";
   }
   elseif ($middleName =="") {
      echo "PLease enter your middleName";
   }
   elseif ($lastName == "") {
      echo "Please neter your lastName";
   }
   else {
      # your code here
   }
}
?>
Back to top View user's profile Send private message Send e-mail
jonleow
-


Joined: 29 Apr 2004
Posts: 48

PostPosted: Sun Apr 09, 2006 7:37 pm    Post subject: Reply with quote

ok it works now..thank you very much!
Back to top View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group