Page 1 of 1

PHP Newbie

Posted: Wed Feb 11, 2009 2:27 pm
by tyler durden
Alright im very new to php and I was wondering how to make sure people fill out a form before they submit it. If they were to forget to fill something out i would like it let them know what they forgot and try again. I'm doing this off of two files, my index and a process.php file. I will put what code i have below, i believe i have everything except for the code that would check to see if it is filled out. Any help is appreciated

This is the process.php file

Code: Select all

<?php
    // Connecting, selecting database
    $linkage = mysql_connect('mrsour.fatcowmysql.com', 'username', 'password')
       or die('Could not connect: ' . mysql_error());
    mysql_select_db('bardok') or die('Could not select database');
 
    // VALIDATION
    
    // Insert Data into database
    $insert = 'insert into members (username, password, age) values("'.$_POST['username'].'", "'.$_POST['password'].'", '.$_POST['age'].');';
    if(mysql_query($insert)) echo 'Thank You For Voting';
    else echo 'Database issue?  '.mysql_error();
    
?>
 
This is the index file

Code: Select all

<html>
    <head>
        <title>Enter Ninja</title>
    </head>
    <body>
        
        <font color="red">
        <?php
            if(empty($_GET['err']))  echo '<br><br>';
            else{
                if($_GET['err']=='user') echo 'Please Enter Your Name';
                if($_GET['err']=='pw') echo 'Please Enter Your Favorite Movie'; 
            }
        ?>
        </font>
        Vote for your favorite movie<br>
<br>
        <form action="process.php" method="post">
            Name: 
              <input type="text" name="fname" size="10" maxlength="12">
            Favorite Movie: 
            <input type="password" name="movie" size="10" maxlength="50">
            <input type="submit" name="submit" value="Vote">
        </form>
    </body>
</html>

Re: PHP Newbie

Posted: Wed Feb 11, 2009 3:13 pm
by sparrrow

Code: Select all

if (strlen(trim($_POST['fname'])) == 0) {
  header("Location:index.php?err=user");
}
 
if (strlen(trim($_POST['movie'])) == 0) {
  header("Location:index.php?err=pw");
}
That would be the easiest way to plug into what you already have written. I would recommend using the same form field/variable names across the board for simplicity purposes.

Personally, I would array all of the form elements, and then parse through the array. Makes it easier to make changes in the future too.

Code: Select all

<input name="userform[fname]">
<input name="userform[password]">

Code: Select all

if (isset($_POST['userform'])) {
  foreach ($_POST['userform'] as $key -> $value) {
    if (strlen(trim($value)) == 0) {
      header("Location:index.php?err=$key");
    }
  }
}
edit: Corrected slight error. Don't use single quotes in key name of form field name.

Re: PHP Newbie

Posted: Wed Feb 11, 2009 3:18 pm
by tyler durden
Thanks so much for the quick reply, im going to try what you gave me, ill be sure to let you know. thanks again

Re: PHP Newbie

Posted: Wed Feb 11, 2009 4:31 pm
by tyler durden
thanks a ton, that worked like a charm, the only thing is it still saves it to the database even though the message telling them that they forgot a text field does come up. any knowledge on how to stop it from saving the data if its not completed.

Re: PHP Newbie

Posted: Wed Feb 11, 2009 4:49 pm
by sparrrow
That's strange. Modifying header location should kick the user over instantly and not process any more code (I think?).....unless you have something displaying on the page before the call (will throw "headers already sent" error). Try adding ob_start(); to the first line of the processing page.

If that doesn't work, you could try

Code: Select all

if (strlen(trim($_POST['fname'])) == 0) {
  header("Location:index.php?err=user");
  exit;
}
Or

Code: Select all

if (strlen(trim($_POST['fname'])) == 0) {
  header("Location:index.php?err=user");
  $fail=TRUE;
}
 
if (!$fail) {
  //DB INSERT CODE
}

Re: PHP Newbie

Posted: Wed Feb 11, 2009 5:11 pm
by tyler durden
ill try those when i get home in a few hours. thanks for all the help!