Jump to content

Form Validation


ToddAtWSU

Recommended Posts

I am having problems figuring out how to do a form validation using HTML and PHP. I create 2 radio buttons, 4 text boxes for name, email, password, and validation string. I then create some other stuff that isn't mandatory to fill out followed by a push button of type="submit". When the user presses the Submit button, I want to notify them if they forget to fill out the name, email, or password, or if they forget to choose one of the radio buttons or if they type in an incorrect validation string. I do not know how to go about doing this if it is part of PHP or HTML. Here is what my code looks like so far.

 

<html>
   <head>
      <title>COC Pick Submission</title>
   </head>

   <?php
/*      $team[0] = "8 Arkansas";
      $team[1] = "4 Florida";
      $team[2] = "Central Michigan";
      $team[3] = "Ohio";
      $team[4] = "Southern Miss";
      $team[5] = "Houston";
      $team[6] = "16 Wake Forest";
      $team[7] = "23 Georgia Tech";
      $team[8] = "13 Rutgers";
      $team[9] = "15 West Virginia";
      $team[10] = "19 Nebraska";
      $team[11] = "8 Oklahoma";
      $team[12] = "Oregon State";
      $team[13] = "24 Hawaii";
      $team[14] = "Louisiana Tech";
      $team[15] = "New Mexico State";*/

      $team;
      function getTeams( )
      {
         global $team;
         $fp = fopen( 'teams.txt', 'r' );
         if( $fp )
         {
            for( $i = 0 ; !feof( $fp ) ; $i++ )
            {
               $team[$i] = fgets( $fp, 128 );
              // echo $team[$i];
            }
         }
      }
   ?>

<!-- You can ignore the rest of this code unless you want to change fonts or colors -->

   <body bgcolor="#000000" text="#FFFFFF" link="#FF9900" vlink="#CC9900">

      <?php
         getTeams( );
      ?>

      <font face="Arial">
         <form action="picksSubmitted.php" method="post">

            <font color="#FFFF00" face="Arial">
               <h3 align="center"> Choose Conference and E-mail addresses </h3>
            </font>

            <p align="center">
               <input type="radio" name="conference" value="East"> East
               <input type="radio" name="conference" value="West"> West

            </p>
            <table border="0" align="center">
               <tr>
                  <td align="right">Your Name</td>
                  <td><input type="text" name="userName"></td>
               </tr>
               <tr>
                  <td align="right">Your E-mail</td>
                  <td><input type="text" name="userEmail"></td>
               </tr>
               <tr>
                  <td align="right">Keyword</td>
                  <td><input type="text" name="keyword"</td>
               </tr>
               <tr>
                  <td align="right">Verifier Phase</td>
                  <td><input type="text" name="phrase"</td>
               </tr>
            </table>

            <br>

            <table border="0" align="center">
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[0];
                     ?>
                  </td>
                  <td><input type="text" name="visitorOne" size=2 /></td>
                  <td><input type="text" name="homeOne" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[1];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[2];
                     ?>
                  </td>
                  <td><input type="text" name="visitorTwo" size=2 /></td>
                  <td><input type="text" name="homeTwo" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[3];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[4];
                     ?>
                  </td>
                  <td><input type="text" name="visitorThree" size=2 /></td>
                  <td><input type="text" name="homeThree" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[5];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[6];
                     ?>
                  </td>
                  <td><input type="text" name="visitorFour" size=2 /></td>
                  <td><input type="text" name="homeFour" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[7];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[8];
                     ?>
                  </td>
                  <td><input type="text" name="visitorFive" size=2 /></td>
                  <td><input type="text" name="homeFive" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[9];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[10];
                     ?>
                  </td>
                  <td><input type="text" name="visitorSix" size=2 /></td>
                  <td><input type="text" name="homeSix" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[11];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[12];
                     ?>
                  </td>
                  <td><input type="text" name="visitorSeven" size=2 /></td>
                  <td><input type="text" name="homeSeven" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[13];
                     ?>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     <?php
                        global $team;
                        echo $team[14];
                     ?>
                  </td>
                  <td><input type="text" name="visitorEight" size=2 /></td>
                  <td><input type="text" name="homeEight" size=2 /></td>
                  <td>
                     <?php
                        global $team;
                        echo $team[15];
                     ?>
                  </td>
               </tr>
            </table>

            <br>

            <p align="center">
               <input type="submit" value="Submit" />
            </p>
         </form>
      </font>
   </body>
</html>

 

Any advice would be great and if you see any ways to improve my PHP code (like how to tell how many members are inside an array), that would be great too. I have done many programs in C/C++ and a few in Java but this is my first real test at using PHP/HTML. Thanks for all your help!

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/
Share on other sites

Then how would I forward to picksSubmitted.php if everything was OK or how would I go back to my main page if something was wrong or empty? I understand what you mean with the check form page but if something is incorrect I don't want to switch pages, but rather keep them on the main page and pop up a message at them but if everything is good to go then forward them to the picksSubmitted.php page. Thanks again for all your help.

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199282
Share on other sites

By using header("Location: place.php")

 

You cant output anything before using the function. So make sure it's at the top of your page.

 

 

<?php
if($blah == ""){
echo $blah " is blank, please enter information to continue";
}
elseif($blah2 == ""){
echo $blah2 " is blank";
}
else{
header("Location: complete.php");
}
?>

 

Man, i'm so tired. Sorry i think that code is fine, i need a rest havn't slept in a LONG time.

 

errm you can do the checking on the same page as the forms by changing the action to the page name and doing an if statement:

 

<?php
if(isset($_POST['conference'])){
//FORM CHECKING
HEADER("LOCATION: COMPLETE.PHP");
}
?>

 

Sorry about the mess, tried to give you a little on where to start. The above can go directly on the form page, when it detects the form has been posted (on submit) it will check the information, if it's ok, goto complete.php. otherwise echo whats wrong.

 

Just take bits from the two and make it neat.

 

Apologies, if you are still in need of help, PM me and i'll sort it in a couple of hours.

 

Snooble

 

(Will still post just in case)... zzzzzzzz

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199297
Share on other sites

I am having problems figuring out how to go about this. If I create a separate file like formCheck.php and if something is wrong I can return the user back to the previous page and if its OK I can send them to the next page. But my question is I want to pop up a message box telling the user what is wrong. How can I pop up a message box to indicate what is wrong and still be able to go back to the previous page if something is wrong. Thanks!

 

Edit: Should I maybe use something other than

<input type="submit" value="Submit" />

? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199448
Share on other sites

lol, you need to decide on what you want. If you want a pop up box like javascript?

 

Type:

javascript:alert("shoob-ba-doob-a-doo-wa");

into your url.

 

If you want to open a page with width and height restraints google it.

 

if you want to send the user to another page displaying the wrong fields.

 

You can do all the above, I suggest just a mix of the last two, as javascript alerts i find childish.

 

if you read my last post you'll find out how to create the validation on the form page itself. Then you can just echo simple commands before/after the text boxes. Par example:

 

<?php
if(isset($_POST['conference'])){
//FORM VALIDATION
}else{
?>

INSERT FORM HERE

<?php
}
?>

 

Where it says "Form validation" enter the if statements. if($_POST['VARIABLE'] = ""){ echo "VARIABLE not entered";}

 

simple!?

 

Snooble

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199490
Share on other sites

I finally understand what you meant by doing the validation and I know how to re-populate the fields that were filled out but I have one more problem and then I think I will be great to go. My last question (hopefully) is when I reload the page after hitting Submit and everything is validated and I tell the header to switch the to the success page, how do I access the variables that were stored in $_POST before. I notice that when I reload the form page and I am successful and I send out the header to go to the next page, all of the $_POST variables have been lost. Thanks for your help in this yet again!

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199516
Share on other sites

you can send the error back to the entry form page at the top if you want...

 

basically, write your entry form page, with code to check if there are $_POST entries...

 

like, <input type='text' 'name'='name' value='$_POST['name']'>

 

if the $_POST['name'] is empty then so will your text box on your form.  If it isn't then it means that it was sent back from the script that the form is submitting to.

 

I am new at this, but if I understand correctly this shoudl work fine.

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199519
Share on other sites

Here is where I am getting confused. At the beginning of my FORM page (pickSubmission.php) I start it like this:

 

<?php
   if( isset( $_POST["conference"] ) && 
       $_POST["userName"] != "" && 
       $_POST["userEmail"] != "" &&
       $_POST["keyword"] != "" && 
       $_POST["phrase"] == "ilovefootball" )
   {
      header( "Location: picksSubmitted.php" );
   }
   else
   {
?>

<html>
   <head>
      <title>COC Pick Submission</title>
   </head>

   <?php
      $team;
      $totalGames;
      function getTeams( )
      {
         global $team;
         $fp = fopen( 'teams.txt', 'r' );
         if( $fp )
         {
            for( $i = 0 ; !feof( $fp ) ; $i++ )
            {
               $team[$i] = fgets( $fp, 128 );
              // echo $team[$i];
            }
         }
         fclose( $fp );
      }
   ?>

<!-- You can ignore the rest of this code unless you want to change fonts or colors -->

   <body bgcolor="#000000" text="#FFFFFF" link="#FF9900" vlink="#CC9900">

      <?php
         getTeams( );
      ?>

      <font face="Arial">
         <form action="pickSubmission.php" method="post">
         ...
         ...

 

This is saying if 3 text fields are empty, the radio buttons do not have one checked and the fourth text field does not match that phrase then hit the else-statement which re-creates the form. The problem is if everything validates and I hit the header( ) call, picksSubmitted.php does not know any of the $_POST values because I believe they do not stay in existence after the page reloads when the Submit button is hit. Does this make sense? Am I correct in my thinking as to why picksSubmitted.php knows nothing of the $_POST variables? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199520
Share on other sites

I had the same problem so I thought outside the box and made a custom mail script that validates the inputs retruns any errors then redirects them back to the page with all their original input still there and then tells them the error. Here is the code if you like:

 

<?php

 

unset($colors);

 

                $option = $_GET['option'];

        $yourname = $_POST['yourname'];

$youremail = $_POST['youremail'];

$yoursubject = $_POST['yoursubject'];

$yourmessage = $_POST['yourmessage'];

$bannedips = "";

$from = "$youremail";

 

 

  // checks proper syntax

  if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $youremail))

  {

$color0 = "hazel";

$colors = "1";

  }

 

 

if($yourname == NULL)

{

$color1 = "blue";

$colors = "1";

}

 

if($youremail == NULL)

{

$color2 = "red";

$colors = "1";

}

 

if($yoursubject == NULL)

{

$color3 = "green";

$colors = "1";

}

 

if($yourmessage == NULL)

{

$color4 = "black";

$colors = "1";

}

if($colors == 1) {$colors = "$color0 $color1 $color2 $color3 $color4";}

if(!isset($option)) {

echo "     

      <form method='POST' action='contact.php?option=boogity&form=1'>

  <div align='center'>

    <table border='0' cellpadding='5' cellspacing='5'>

      <tr>

        <td width='50%' align='right' valign='top'>Your Name:</td>

        <td width='50%'>

        <input type='text' name='yourname' size='40' tabindex='1'></td>

      </tr>

      <tr>

        <td width='50%' align='right' valign='top'>Your E-Mail:</td>

        <td width='50%'>

        <input type='text' name='youremail' size='40' tabindex='2'></td>

      </tr>

      <tr>

        <td width='50%' align='right' valign='top'>Your Subject:</td>

        <td width='50%'><input type='text' name='yoursubject' size='40' tabindex='3'></td>

      </tr>

      <tr>

        <td colspan='2' align='center' valign='top'>Your Message:</td>

  </tr>

  <tr>

        <td colspan='2' align='center'>

        <textarea rows='15' name='yourmessage' cols='44' tabindex='4'></textarea>

        <input type='hidden' name='option' value='sendemail'>

        </td>

      </tr>

      <tr>

        <td colspan='2' align='center'>

        <input type='submit' value='Send Mail' name='Send' tabindex='5'><input type='reset' value='Reset' name='reset'>

      </td></tr>

    </table>

  </div>

</form>";

 

}

 

 

$webmastersemail = "$youremailhere";

 

if($option == "boogity")

{

$ip = $_SERVER["REMOTE_ADDR"];

 

$pass = "yes";

 

$exbannedips = explode(",", $bannedips);

 

for($i=0; $i<count($exbannedips); $i++)

{

if($exbannedips[$i] == $ip)

{

echo("<p align='center'>You are banned from this Contact Form if you feel there has been a mistake contact the website administrator, click <a href='?option=viewform'>here</a> to return.</p>");

$pass = "no";

}

}

 

 

if(isset($colors))

{

 

echo "  <div align='center'>

          <h4 align='center'>Oops, something's missing, try again!</h4> ";

 

if($color1 == blue) {

 

echo "<font color='red'><b>You forgot to tell us your name!</b></font><br />";

 

}

if($color2 == red) {

echo "<font color='red'><b>You forgot to tell us your email address!</b></font><br />";

 

}

if($color3 == green) {

echo "<font color='red'><b>You forgot to tell us your subject!</b></font><br />";

 

}

if($color4 == black) {

echo "<font color='red'><b>You forgot to tell us your message!</b></font><br />";

 

}

if($color0 == hazel) {

echo "<font color='red'><b>Your email address appears invalid</b></font><br />";

 

}

echo " 

      <form method='POST' action='contact.php?option=boogity'>

 

    <table border='0' cellpadding='5' cellspacing='5'>

      <tr>

        <td width='50%' align='right' valign='top'>Your Name:</td>

        <td width='50%'>

        <input type='text' name='yourname' size='40' tabindex='1' value='$yourname'></td>

      </tr>

      <tr>

        <td width='50%' align='right' valign='top'>Your E-Mail:</td>

        <td width='50%'>

        <input type='text' name='youremail' size='40' tabindex='2' value='$youremail'></td>

      </tr>

      <tr>

        <td width='50%' align='right' valign='top'>Your Subject:</td>

        <td width='50%'><input type='text' name='yoursubject' size='40' tabindex='3' value='$yoursubject'></td>

      </tr>

      <tr>

        <td colspan='2' align='center' valign='top'>Your Message:</td>

  </tr>

  <tr>

        <td colspan='2' align='center'>

        <textarea rows='15' name='yourmessage' cols='44' tabindex='4'>$yourmessage</textarea>

        <input type='hidden' name='option' value='sendemail'>

        </td>

      </tr>

      <tr>

        <td colspan='2' align='center'>

        <input type='submit' value='Send Mail' name='Send' tabindex='5'><input type='reset' value='Reset' name='reset'>

      </td></tr>

    </table>

  </div>

</form>";

}

if(!isset($colors)) 

{

 

$yourmessage = nl2br($yourmessage);

 

$yourmessage = $yourmessage ;

 

mail("$webmastersemail", "$yoursubject", "This persons email is: $youremail\n\rThis persons name is: $yourname\n\rThis persons IP address is: $ip\n\r\n\rThe message is: \n\r\n\r$yourmessage", "From: $from");

echo "

    <h4 align='center'>Thank you for contacting us</h4>     

  <div align='center'>

    <table border='0' cellpadding='5' cellspacing='5'>

      <tr>

        <td width='50%' align='right' valign='top'>Your Name:</td>

        <td width='50%'>$yourname</td>       

      </tr>

      <tr>

        <td width='50%' align='right' valign='top'>Your E-Mail:</td>

        <td width='50%'>$youremail</td>

      </tr>

      <tr>

        <td width='50%' align='right' valign='top'>Your Subject:</td>

        <td width='50%'>$yoursubject</td>

      </tr>

      <tr>

        <td colspan='2' align='center' valign='top'>Your Message:</td>

  </tr>

  <tr>

        <td colspan='2' align='center'>$yourmessage</td>

      </tr>

    </table>

  </div>";

}

 

}

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/41139-form-validation/#findComment-199652
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.