Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Form Validation - foreach

All the submitted fields are available in $_POST array. So we can iterate over this array to check if the value of required fields are exist or not.The code is as follows:

<?php
$post = $_POST;
if(count($post) > 0) {

    foreach($post as $key => $value) {

        if(empty($post[$key])) {
        $message =  $key . " is required!";
        break;
        }
    }

}
?>

I want to this action:

For example when the username field is empty the message is printed input1 is required!. input1 is same the name of the username field.

I want to echo username is required but without changing name of the username field.

For example, a code like the following, but does not work and I do not know where and how!

if($key == 'input1'){
    $key = 'username';
}
else
if($key == 'input2'){
    $key = 'password';
}

Form and inputs elements in the following code:

<html>
<head>
<style>
.tableheader {
    background-color: #CCC;
    color:white;
    font-weight:bold;

}
.tablerow {
    background-color: #f9f9f9;
    color: #333;
}
.message {
    color: #FF0000;
    font-weight: bold;
    text-align: center;
    width: 100%;
    padding: 10;
}

</style>
</head>
<body dir="rtl">
<div align="center" class="message"><?php if(isset($message)) echo $message; ?></div>
<form name="registrationform" method="post" action="" style="direction: ltr">

<table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
<tr class="tableheader">
<td align="center" colspan="2">Registration Form</td>
</tr>
<tr class="tablerow">
<td align="right">Username</td>
<td><input type="text" name="input1" value="<?php if(isset($_POST['input1'])) echo $_POST['input1']; ?>"></td>
</tr>
<tr class="tablerow">
<td align="right">Password</td>
<td><input type="password" name="input2" value="<?php if(isset($_POST['input2'])) echo $_POST['input2']; ?>"></td>
</tr>


<tr class="tableheader">
<td align="center" colspan="2"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</body></html>
like image 304
Mohammad Avatar asked Aug 09 '26 21:08

Mohammad


2 Answers

If understood correctly... you need another array where you'd keep a "map" of your input fields' names and their actual "human" names. Something like:

$fields_map = array(
  'input1' => 'Username',
  'input2' => 'Password',
  'whatever' => 'something'
)

..then, when you want to output the message to user, then you'd do something like:

if(empty($post[$key])) {
   $message =  $fields_map[$key] . " is required!";
} 
like image 171
Oliver Maksimovic Avatar answered Aug 11 '26 09:08

Oliver Maksimovic


You have to use a switch statement or find a way to map the actual names you want to display, e.g username to the form names, e.g input1.

An example of such mapping is presented below:

$map = array('input1'=>'username', 'input2'=>'surname', 'input3'=>'othernames');
$post = $_POST;
if(count($post) > 0) {

   foreach($post as $key => $value) {

      if(empty($post[$key])) {
         $message =  $map[$key] . " is required!";
         break;
      }
   }
}
like image 40
NaijaProgrammer Avatar answered Aug 11 '26 11:08

NaijaProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!