Look at Paolo's answer to understand the ternary operator.
To do what you are looking for, you can use a session variable.
Put this at the top of the page (because you cannot display anything on the page before starting a session. IE NO ECHO STATEMENTS)
session_start();
Then, when the user submits your form, save the result in this server variable. If this is the first time a user has submitted a form, just save it directly, otherwise run a loop and add any value that is not empty. See what exactly you are looking for:
HTML CODE (testform.html):
<html> <body> <form name="someForm" action="process.php" method="POST"> <input name="items[]" type="text"> <input name="items[]" type="text"> <input name="items[]" type="text"> <input type="submit"> </form> </body> </html>
Code Processing (process.php):
<?php session_start(); if(!$_SESSION['items']) { // If this is the first time the user submitted the form, // set what they put in to the master list which is $_SESSION['items']. $_SESSION['items'] = $_POST['items']; } else { // If the user has submitted items before... // Then we want to replace any fields they changed with the changed value // and leave the blank ones with what they previously gave us. foreach ($_POST['items'] as $key => $value) { if ($value != '') { // So long as the field is not blank $_SESSION['items'][$key] = $value; } } } // Displaying the array. foreach ($_SESSION['items'] as $k => $v) { echo $v,'<br>'; } ?>
Joe Bubna May 21 '09 at 17:31 2009-05-21 17:31
source share