PHP Newbie

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
tyler durden
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2009 2:20 pm

PHP Newbie

Post 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>
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: PHP Newbie

Post 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.
Last edited by sparrrow on Wed Feb 11, 2009 3:33 pm, edited 1 time in total.
tyler durden
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2009 2:20 pm

Re: PHP Newbie

Post 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
tyler durden
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2009 2:20 pm

Re: PHP Newbie

Post 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.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: PHP Newbie

Post 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
}
tyler durden
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2009 2:20 pm

Re: PHP Newbie

Post by tyler durden »

ill try those when i get home in a few hours. thanks for all the help!
Post Reply