the id needs to be unique on the page: you should never have more than one element with any given id. You might use the class attribute or a custom data attribute (like data-id) instead.
to be clear, you want the values from each form submission to be displayed, inside the div.wrap, along with all the previous submission values?
UNTESTED (sorry, not at my server
I'll check later but it should work fine)
PHP Code:
<?php
session_start();
// reset
if(!empty($_POST['reset'])){
if(!empty($_SESSION['counter'])){ unset($_SESSSION['counter']); }
if(!empty($_SESSION['formsubmission'])){ unset $_SESSION['formsubmission']; }
}
// is there a submission? process it
// (this assumes that you _only_ want to process it if _all_ values are submitted
// (e.g., _no_ empty fields allowed
elseif(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['phone'])){
// is the counter set?
$_SESSION['counter'] = empty($_SESSION['counter'])? 1: $_SESSION['counter'];
// escape each input (otherwise you have a security problem)
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
$phone= htmlentities($_POST['phone']);
// save info to session
// (we will make the html output later)
$_SESSION['formsubmission'][$_SESSION['counter']] = array('name'=>$name,'phone'=>$phone,'email'=>$email);
// increment the counter
$_SESSION['counter']++;
}
// markup for the form
$output = '
<form method="post" action="index.php">
<div id="name">Name:<input type="text" name="name" size="10" /></div>
<div id="email">Armor:<input type="text" name="email" size="10" /></div>
<div id="phone">Strength:<input type="text" name="phone" size="10" /></div>
<input type="submit" name="submitted" value="Submit"/>
<input type="submit" name="reset" value="reset" />
</form>';
// make html output if any submissions are saved to session
if(!empty($_SESSION['formsubmission']){
foreach($_SESSION['formsubmission'] as $counter => $values){
$html[] = '
<div class="wrap">
<div data-count="'.$counter.'" data-val="name">Name: '.$values['name'].'</div>
<div data-count="'.$counter.'" data-val="phone">Phone: '.$values['phone'].'</div>
<div data-count="'.$counter.'" data-val="email">Email: '.$values['email'].'</div>
</div>';
}
// here, we put any results at the end of the form
$output .= implode("\n",$html);
}
// print it
print $output;
?>
Bookmarks