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
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Okay, stop it, both of you. If we cannot get back on the subject of providing assistance then this thread will have served its purpose and will be locked.

akimm, are you checking for if the form was actually sent? Also, try this to see what is being passed to the $_POST array. Logically, the code I posted should work without incident.

Code: Select all

<?php
foreach ($_POST as $key => $value)
{
    echo '<p>The post key is ' . $key . ' and its matching value is ' . $value . '.</p>';
/*    if (empty($value))
    {
        echo '<h2>The form field ' . $key . ' was left blank. Please correct it.</h2>';
    }
*/
}?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Yes

Post by akimm »

Sorry for my biccering.

now, yes the form is definetly sent. I usually post a value into 1 form to make sure its going through.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Everah.

Post by akimm »

Its been a mistake of mine this whole time.

The problem was, I made a file called error.php then included it. However, error.php is seperate from my muk.php which processes the writing of the file to their seperate places.

So please forgive me for this mistake. The code you wrote here, works without error.

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) 
{ 
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks for the help.

Post by akimm »

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

Post by christian_phpbeginner »

akimm wrote:but it allows submissions with no values entered at all. I'm sincerely confused as to how we can solve this.
I am refering to the bolded part in the quoted string. PHP is a server-side scripting language...so, how could you expect PHP to validate your form without submitting the data first ? If what you want is to prevent submit after the validation process is completed, that must be on the client side and use Javascript.

My code and everybody's codes here don't prevent form submissions, but our codes don't process inputs into the database. Our codes here are for validating and not for processing.

The most important thing is:
  • If satisfied conditions are not met, the data should not be INSERTED (PROCESSING) into the database, but you should validate and validate the data until the satisfied conditions are met, then you program PHP to INSERT the satisfied data into the database.
I recommend to use BOTH Javascript and PHP form validation for your solution as quoted from http://www.zend.com/zend/tut/tutorial-m ... c=0&view=1:
To prevent any kind of intrusion, your best companion is always PHP. Even though you have utilized Javascript, never skip checking data with PHP. This is the most reliable checkpoint before processing input.
And if the user turns off the javascript, your PHP code would still be executed.

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

Everah

Post by akimm »

Do you have any suggestions how I can make sure the data that was filled in stays filled in, rather than being eraced when there is an error.
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Re: Everah

Post by christian_phpbeginner »

akimm wrote:Do you have any suggestions how I can make sure the data that was filled in stays filled in, rather than being eraced when there is an error.
Use $_SESSION variables, you should then do like this on muk.php:

Code: Select all

<?php
session_start();
$_SESSION ['name'] = $_POST['name'];
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Set initial vars, then reset them to the passed vars and echo those vars in the form when there is an error. Here is a one-off hint for you...

Code: Select all

<?php
$name = '';
if (isset($_POST['name']))
{
    $name = $_POST['name'];
}
?>

<form>
<input type="text" name="name" value="<?php echo $name; ?>" />
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

I am glad that I could help! :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ok wrote:I am glad that I could help! :D
Who did you help? :wink:
Post Reply