PHP form validation

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

User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

Everah wrote: How about a little simpler version...

Code: Select all

<?php
foreach ($_POST as $key => $value)
{
    if (empty($value))
    {
        echo '<h2>The form field ' . $key . ' was left blank. Please correct it.</h2>';
    }
}
?>
Or something along these lines.
Hi Everah !...what do you mean with "simpler version" ? It's much better version ! Thanks for making the code simpler. This often happens to me, I don't know why....some friends told me that I am a complicated man, maybe that's the problem. I really need to learn a lot how to think in simpler way !':oops:' Or maybe it's a great idea too when some experienced programmers publish a book containing complicated code which could be made simpler, much like Programmer Puzzle Book !...lol...

Thanks again, I should learn much from people like you.

Cheers,
Chris
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

And I learn from all of you

Post by akimm »

Thanks for the ideas, I'm going to try everah's code and see if that will be enough. Thanks again for the input.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Everahs code

Post by akimm »

Everahs code still allows submission, when no values are present, I am honestly lost as to why it wouldn't work, it reads very logically, and should work. But its not.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Try JavaScript solution (one similar to mine)
viewtopic.php?p=308918#308918
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

As for Christian's code, it too has the same flaws, it should be working too, but it allows submissions with no values entered at all. I'm sincerely confused as to how we can solve this.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

But like they said

Post by akimm »

if java is off on the browser then its all for nothing.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

that's javascript... not java... there is a difference.

Show us what you tried... those should be working fine.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Yes I know. I was looking at that error as you patronized me.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

I tried:

Post by akimm »

Code: Select all

<?php
   session_start(); 
    
   $postCollections = array ( 'Name'=>$_POST['name'], 
                             'Email'=>$_POST['email'], 
                             'Title'=>$_POST['title'], 
                             'Article'=>$_POST['article'] 
); 
    
   foreach($postCollections as $key => $value) { 
           if (empty($value)) { 
              switch ($key) { 
               case "Name": 
                 echo "Please put your name".'<br />'; 
                 break; 
               case "Title": 
                 echo "Please post your title".'<br />'; 
                 break; 
               case "Article": 
                 echo "Please post an article".'<br />'; 
                 break;    
              } //end switch statement 
           } else { 
                echo $key.': '.$value.'<br />'; 
           }//end if-else 

   }  //end foreach 
    
?>
Everahs code:

Code: Select all

<?php 
foreach ($_POST as $key => $value) 
{ 
    if (empty($value)) 
    { 
        echo '<h2>The form field ' . $key . ' was left blank. Please correct it.</h2>'; 
    } 
} 
?>
Another of Everah's

Code: Select all

<?php 
$name = ''; 
$title = ''; 
$article = ''; 
$validation = 0; 
$submit = "<input type=submit value=submit your article name=submit>"; 

if (isset($_POST['name'])) 
{ 
    $name = $_POST['name']; 
    // Depending on what you expect name to be you could regex here 
} 

/* 
    Handle the other form fields the same way 
    validating as needed for the expected content 
*/ 

if (empty($name)) 
{ 
    echo "Please put your name"; 
} 
else 
{ 
    $validation++; 
} 

if (empty($title)) 
{ 
    echo "Please post your title"; 
} 
else 
{ 
    $validation++; 
} 

if (empty($article)) 
{ 
    echo "Please post an article"; 
} 
else 
{ 
    $validation++; 
} 

if ($validation == 3) 
{ 
    echo $submit; 
} 
?>
And the code I posted at first, i've tried.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

man... I was pointing that out to HELP you... I think I'm done helping you... you are too whiney.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

muk.php

Code: Select all

<?php

$submit = 0;
foreach ($_POST as $key => $value)
{
    if (empty($value))
    {
        echo '<h2>The form field ' . $key . ' was left blank. Please correct it.</h2>';
        $submit++;
    }
} 
if($submit > 0)
{
?>
<form method="POST" action="muk.php">
<p><b>Date</b><input type="text" value="<b><?php echo date('F j, Y, g:i a');?></b>" name="date" size="30">
<p><b>Your name</b>
<input type='text' name='name' size='30'></p>

<p><b>Your email</b>
<input type='text' name='email' size='30'></p>

<p><b>article title</b>
<input type='text' name='title' size='30'></p>

<p><b>article</b>
<TEXTAREA NAME='article' COLS='120' ROWS='20' WRAP='virtual'></TEXTAREA></P>
<p><b><input type="submit" value="submit your article" name="submit">
</b></p>
<?php
}else{
?>
Form submitted!
<?php
}
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok.

Post by akimm »

Well you're too picky, help is provided when you want, so feel free to walk away, sorry i'm so whiney, but to avoid that consider all sides of something before pointing a finger. That will avoid some aungish.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK... next time I will consider all sides of trying to help you out. :roll:
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

'ok'

Post by akimm »

Thanks for the assistance, but still this is not working, I know JAVASCRIPT is what I need to use, but Everah warned me against that, and do trust his opinion more than my sense as far as programmin is concerned.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok Ninja

Post by akimm »

Thanks for your cynacism, further proving my point 8O
Post Reply