Displaying some data in a html table after $_POST

Hello, I want to display a table with only a part of the info after Submit, if a name is inserted correctly I want to display the name and it’s role, followed by text “Accepted!”
If name is wrongly inputted i don’t insert it, but I display the name, it’s role followed by text “Try Again!”
All in the same table so u can enter several names at the same time and display if accepted or not… I’ve checked some examples but they are slightly different.

Only one $name is displayed, even if you $_POST two.

Here’s my code:


include_once 'pdo.php';

$name = $_POST['name'];
$role = $_POST['role'];
$round = $_POST['round'];
$week = $_POST['week'];
$postdate= date('Y-m-d H:i:s.');

for($i=0;$i<count($name) && ($role) ;$i++)
       if(preg_match("%^[A-Za-z0-9-_]{3,35}$%", $name[$i]))
          $sql_add = 'INSERT INTO worktable (name,role,round,week,time) VALUES (?,?,?,?,?)';
          $stmt_add = $pdo->prepare($sql_add);
          $stmt_add->execute(array($name[$i],$role[$i],$round, $week, $postdate)); 
            $arr = array($name[$i],$role[$i],'Accepted!');
         } else {
            $arr = array($name[$i],$role[$i],'Try again!');

      <link href="bootstrap.min.css">
      <div class="container">
         <div class="row">     
            <div class="col"></div>        
               <div class="col-md-8"><br><br>
                  <table class="table table-striped">
                           <th scope="col">Name</th>
                           <th scope="col">Role</th>
                           <th scope="col">Status</th>
                           <?php foreach($arr as $data): ?>
                           <td><?php echo $data; ?></td>
                           <?php endforeach ?>
            <div class="col"></div> 

How do I correctly build this new array and correctly display it? Any material to read o hint. Thanks.

Your form and form processing code should be on the same page so that the visitor doesn’t need to ‘remember’ which fields contained errors. They can just correct the fields with invalid values and re-submit the form. Since you would be re-populating all the form field values with the previously submitted values, you can simplify the processing by only inserting all the data when there are no validation errors.

Your form processing code should -

  1. Detect if a post method form was submitted.
  2. Trim all the data at once.
  3. Validate all the data, storing user/validation errors in an array using the field name as the array index. Since there are arrays of inputs, you would store arrays of validation errors for each field name.
  4. Validation errors should be specific and helpful, telling the visitor what was wrong with the value, such as being empty, containing characters that are not permitted, or being of a length that is not permitted.
  5. After the end of all the validation logic, if the array holding the errors is empty, use the submitted data.
  6. If the insert query could result in duplicate values, and this is an error for your application, you would catch any execute error in your code, detect if the error number is for a duplicate index, and setup a message in the errors array telling the user what was wrong with the submitted data. For all other error numbers, just re-throw the exception and let php handle it.
  7. After the end of all of the above processing, if there are no errors, execute a redirect to the exact same url of the current page to cause a get request for the page. To display a one-time success message, store a value in a session variable, then test, display, and clear the session variable on the get request for the page. If there are errors, you would continue on the page, display any errors, re-display the form, populating the fields with the submitted form data so that the user can correct any errors.
1 Like

Everything you said makes a lot of sense, I’ll make good use of everything you explained, thanks a lot again.

Sponsor our Newsletter | Privacy Policy | Terms of Service