Jump to content

[SOLVED] php form validation


Nandini

Recommended Posts

Hi i have a registration form. There is two categories. One is peer, contains three text fileds named as peer[] like '<input type="text" name="peer[]">' etc like this 3 fields. Another one is user, this is also contain three text fields named as user[] like '<input type="text" name="user[]">' etc like this 3 fields. I want to insert either peer values or user values or both. But dont accept if atleast one of them not entered. So i want to validate the form. Because of the text fields names as peer[] like array validations are not done. Here is my code. pls check it and give advice pls.

<?
extract($_POST);
$error_colour = "red";
$lang_peeruser = "Enter peer or user details\n";

if ((empty($_POST['peer'])) || (empty ($_POST['user'])))
        {
        $error = "1";
        $info_error .= "".$lang_peeruser . "";
        }

        if ($error == "1")
        {
        $info_notice = "<span style=\"color: " . $error_colour . "; font-weight: bold;\">" . $lang_error . "</span><br>";

        if (empty ($submit))
                {
                $info_error = "";
                $info_notice = $lang_notice;
                }
?>

<form method="post" action="">
<table border="1" align="center">
<tr align="left" valign="top">
      <td colspan="2"><?php echo "<font color=red>".$info_notice.$info_error. "</font>" ?></td>
    </tr>
<tr><td colspan="4" align="center">Outgoing</td></tr>
<tr><td valign="top">Peer:</td>
<td><input type="text" name="peer[]" /><br />
<input type="text" name="peer[]" /><br />
<input type="text" name="peer[]" />
</td></tr>
<tr><td colspan="4" align="center">Incoming</td></tr>
<tr><td valign="top">User:</td>
<td><input type="text" name="user[]" /><br />
<input type="text" name="user[]" /><br />
<input type="text" name="user[]" />
</td></tr>
<tr><td colspan="2" align="center">
<input type="submit" name="submit" value="submit" />
</td></tr>
</table>
</form>
}
else {
/////////////database here
if(isset($_POST['peer']))
{
   $value = $_POST['peer'];
   $n        = count($peer);
   $i        = 0;
   while ($i < $n)
   {
          echo $peer[$i]." \r\n"; //////print entered details
      $i++;
   }
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/
Share on other sites

If i get the idea right, you want to accept the input only if all 3 peers or all 3 users are entered. You can always give the text inputs different names (peer1, peer2, peer3), but for your approach, you can use count() to check how many elements the array has. You know it will have 3 elements so:

 

<?php
$peers = array_values(array_filter($_POST['peers'])); //two array functions to remove empty elements. use: $peers = array_filter($_POST['peers']); if you want to preserve keys
$users = array_values(array_filter($_POST['users']));
if(count($peers) == 3 or count($users) == 3){ //validate only if one of the arrays (peers or users) has 3 elements
    //the form validates correctly
    //the rest of your code
}
?>

Hi GuiltyGear. I tried ur code. I got following error.

 

Warning: array_filter() [function.array-filter]: The first argument should be an array in /var/www/admin/trunks/sample.php on line 15

 

Warning: array_values() [function.array-values]: The argument should be an array in /var/www/admin/trunks/sample.php on line 15

 

Warning: array_filter() [function.array-filter]: The first argument should be an array in /var/www/admin/trunks/sample.php on line 16

 

Warning: array_values() [function.array-values]: The argument should be an array in /var/www/admin/trunks/sample.php on line 16

You need to check first if the submit button is clicked, which means that the form itself has been submitted. Just add an if() which contains all your form validation code:

 

<?php
if(isset($_POST['user'])){
   //if this and that
   //if this and that
}
//your html forms and stuff
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.