Page 1 of 1

Check for required fields in forms.

Posted: Thu Feb 07, 2008 8:41 pm
by Skoalbasher
New here, tried to find this in search, and maybe I just don't know what to search for...

I'm creating a user sign up page (index.html), and i've written some code to check if required fields have been filled in.
When the user clicks "Submit" it sends them to another page (handler.php), and that does the check for the passwords and the required fields.

I would like for "Submit" to take them back to the same page, and highlight the fields that need to be filled in, if something is missing. If they filled everything in, take them to the next page (handler.php).


If anyone could provide a link to some scripting like this, that'd be great! :D

Thanks

Re: Check for required fields in forms.

Posted: Thu Feb 07, 2008 9:29 pm
by jeffrydell
Here's what I do ... best solution ever, probably not - but it works for me when I need to 'pound it out'.

#1 I always call my FORM (html page) from a php script. You'll see why later. In this case you can just have a script called index.php with:

Code: Select all

 
<?php
session_start();
require('indexform.html');
?>
 
We'll get to a little tweak in your indexform.html (along with WHY we're doing this.)

#2 Note session_start(); at the top of each script. I almost always put that IMMEDIATELY after <?php ... second nature for me.

#3 for the form handler ...

Code: Select all

 
<?php
... Do whatever you typically do to validate the data ...
 
// This is how I typically check something
if (strlen($_POST['firstname'] < 1)
{
$_SESSION['err_msg'] = "Put the error message here.";
}
else
{
... do whatever you typically with the data (personally, I store it to an array in $_SESSION)...
}
 
... do the same for each field ...
 
... at the bottom of the script ...
 
if (isset($_SESSION['err_msg'])
{
header('Location: index.php');  // Send 'em back to fix errors
}
else
{
// Let them go to whatever they should do with good data
header('Location: (whatever is next.php)'); 
}
?>
 
So, you have checked for good data and sent them along if all is well.
You've sent them back to the original script if you didn't like the answers they provided.
Now, to show the user the errors of their ways.

#4 The tweak to your html form:

Code: Select all

 
<html>
<head>
<title>Whatever</title>
<meta>Stuff</meta>
</head>
<body>
<? if(isset($_SESSION['err_msg'])){echo $_SESSION['err_msg'] . "<br />";} ?>
... go on with the rest of your form exactly as it was ...
</body>
</html>
 
Now your error message shows up at the top of the form. Maybe in bold red letters.
Seems to work well for me. You can get fancier depending on your level of sophistication ... I have all the $_POST data in the $_SESSION array, so I usually make the form refer to the $_SESSION for default values.

Hope this helps ... got more questions? Fire away. The gang here is really friendly & helpful!

Re: Check for required fields in forms.

Posted: Thu Feb 07, 2008 10:36 pm
by Skoalbasher
I just got finished reading, and that's something I should have thought of from the start.

Just one thing though...
Say this page is "index.php" and my form is posting to "handle.php". When the form is submited, should I have it post to index.php or the new page handle.php. If they mess up, i'd like it to send them back to index.php and still have all the form data there, so they only need to fill in what they missed. If they did what they are told, then send them to the next page. That's the part i'm having trouble with the most. But your post did help out, gotta learn to use the $_SESSION variable.

Edit: I understand how you are doing this, just again, not sure how to put all the data in session so you can use it from page to page.

Code: Select all

 
----------------- I see this calls index.php into this page which is (handle.php)
session_start();
require('index.php');
------------------ but when i do i get this error
 
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at handle.php:6) in handle.php on line 35
 
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at handle.php:6) in handle.php on line 35
 
I keep figuring stuff out on my own, lol. I didn't have the above code ^^ posted above the <head> tag. Works good now so far! Still need to figure out how to use the $_SESSION as an array to pass the code back into index.php.

This is the code I was using to check for empty boxes.

Code: Select all

 
$username = htmlspecialchars($_POST['username']);  // User's requested username, this is unique in MySQL
$password = htmlspecialchars($_POST['password']);  // User's password.
$confirm = htmlspecialchars($_POST['confirm']);    // User's confirm password entry
$email = htmlspecialchars($_POST['email']);
$firstname = htmlspecialchars($_POST['firstname']);
$middle = htmlspecialchars($_POST['middle']);
$lastname = htmlspecialchars($_POST['lastname']);
$sex = htmlspecialchars($_POST['sex']);
$address = htmlspecialchars($_POST['address']);
$apartment = htmlspecialchars($_POST['apartment']);
$state = htmlspecialchars($_POST['state']);
$zipcode = htmlspecialchars($_POST['zipcode']);
$phone1 = htmlspecialchars($_POST['phone1']);
$phonetype1 = htmlspecialchars($_POST['phonetype1']);
$phone2 = htmlspecialchars($_POST['phone2']);
$phonetype2 = htmlspecialchars($_POST['phonetype2']);
$xbox = $_POST['xbox'];
$xbox360 = $_POST['xbox360'];
$ps2 = $_POST['ps2'];
$ps3 = $_POST['ps3'];
$pc = $_POST['pc'];
$mmorpg = $_POST['mmorpg'];
$rpg = $_POST['rpg'];
$fps = $_POST['fps'];
$rts = $_POST['rts'];
$sim = $_POST['sim'];
$sports = $_POST['sports'];
 
// Birthday variables
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
 
// create birthday variable for SQL input.
$birthday = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
 
// echo $password."<br><br>";
// echo $username.": Login Test<br><br>";   // Makes sure $_POST function works.
// echo "Birthday in MySQL format: ".$birthday."<br><br>";
 
//check to see if the user entered a password
if($password)
{
        // check for matching passwords
    if($password == $confirm)
    {
        $passmatch = true;
    }
    else
    {
        echo "Passwords did not match!<br>Please press \"Back\" on your browser and re-enter your password.";
        $passmatch = false;
    }
} else 
{
    echo "You must enter a password<br>Please press \"Back\" on your browser and enter a password.";  
    $passmatch = false;
}
 
 
// Check to see if user filled in all boxes.
if($username AND $passmatch AND $email AND((INT)$_POST['year']) AND ((INT)$_POST['month']) AND ((INT)$_POST['day']) AND $firstname AND $lastname AND $address AND $state AND $zipcode AND $phone1  AND $phonetype1)
{
 
if($passmatch)
 
// more stuff down here that doesn't matter
Is there a way to do this quicker? I programmed a lot during high school and college, but now I've got something I need to do and need to brush up on my skills. I understand classes and functions, but don't really know how to use them in php. Something I can learn on PHP.net, sometimes it's hard to navigate.

I need to look around and find some good tutorials on arrays, because this would help me tremendously here when inputing into MySQL. I don't want to have to type all those variables one by one.

I appreciate your quick reply, and it's nice to find a good dev forum for php.

Re: Check for required fields in forms.

Posted: Fri Feb 08, 2008 9:42 am
by Skoalbasher
Your examples helped more than I thought. All the questions I had in my second post were answered by your post, just had to dig a little deeper than I was late last night. Thanks again for the help

Re: Check for required fields in forms.

Posted: Fri Feb 08, 2008 11:54 am
by jeffrydell
Skoalbasher wrote:Your examples helped more than I thought. All the questions I had in my second post were answered by your post, just had to dig a little deeper than I was late last night. Thanks again for the help
Glad it did ... I was just about to reply, but it looks like you saved my tired old hands a few keystrokes.

Come back with other snags, this is a great place to work things out!

Jeff

Re: Check for required fields in forms.

Posted: Fri Feb 08, 2008 12:41 pm
by Skoalbasher
jeffrydell wrote:
Skoalbasher wrote:Your examples helped more than I thought. All the questions I had in my second post were answered by your post, just had to dig a little deeper than I was late last night. Thanks again for the help
Glad it did ... I was just about to reply, but it looks like you saved my tired old hands a few keystrokes.

Come back with other snags, this is a great place to work things out!

Jeff
Yeah, pretty much got it figured out. I'm going to have to do a lot of typing to create the error messages, with a ton of if statements.

Do you know how to make all the fields an array so I can just do a loop to check them? That may not work if i'm trying to creat an error message, so that may not be needed.

I still can't find where to put an array into $_SESSION, but I'm happy to learn that the post data will stay when index is called back by handle.php.

Re: Check for required fields in forms.

Posted: Fri Feb 08, 2008 1:15 pm
by jeffrydell
All the fields ARE in an array ($_POST), but I think you want to put them in a 'permanent' array.

It just so happens you can learn about that at:

http://apptools.com/phptools/multipage.php

As for putting an array in $_SESSION, easier than one might think:

Code: Select all

 
$_SESSION['User'] = array();
 
Now you can store & access first name and last name like this:

Code: Select all

 
$_SESSION['User']['firstname'] = "";
$_SESSION['User']['lastname'] = "";
 
Let's say you want to store info about the user's pet in an array:

Code: Select all

 
$_SESSION['Pet'] = array();
$_SESSION['Pet']['type'] = "dog";
$_SESSION['Pet']['name'] = "Spot";
 
Got it? Good! Now go forth and rock!

Jeff

Re: Check for required fields in forms.

Posted: Fri Feb 08, 2008 2:36 pm
by Skoalbasher
You are the man my friend. The man. If you ever have a questions about a Genie garage door opener, i'm your man.

Re: Check for required fields in forms.

Posted: Fri Feb 08, 2008 9:45 pm
by jeffrydell
Thanks! Pay it forward.