I am currently working on a small signup sheet for a bigger project which will integrate it, and I'm trying to figure out how to integrate a hidden form field(s) into it, to preserve data across submissions. Basically, whenever someone signs up on the form, their name should appear next to the entry, and that entry should not change when the next person submits the form (i.e., it shouldn't become available again). This is what I am working with so far:
Name: <input type="text" name="name"><br>
Select from one of the following:<br>
<fieldgroup>
<?php
$form_contents = "";
if (isset($_POST['food']) && $_POST['food'] == 'pasta') {
$form_contents .= "<input type='hidden'>Pasta - " . $_POST['name'] . "<br>";
} else {
$form_contents .= '<input type="radio" name="food" value="pasta">Pasta</input><br>';
}
if (isset($_POST['food']) && $_POST['food'] == 'salad') {
$form_contents .= "Salad - " . $_POST['name'] . "<br>";
} else {
$form_contents .= '<input type="radio" name="food" value="salad">Salad</input><br>';
}
if (isset($_POST['food']) && $_POST['food'] == 'burgers') {
$form_contents .= "Burgers - " . $_POST['name'] . "<br>";
} else {
$form_contents .= '<input type="radio" name="food" value="burgers">Burgers</input><br>';
}
echo $form_contents;
?>
</fieldgroup>
Currently, this form stores an initial entry, but the next time I submit it, that name is erased and the selection becomes available again. Is there any way to keep that entry so that it is preserved every time the form is submitted? Any help is appreciated!
I think your idea with <input type="hidden" />
works well.
Maybe you can take a look how <input>
works, right here. You don't have to do it like <input></input>
, just give the input a name and a value.
Every submit, you can check if a current variable is already given. When it is, you can set a <input type="hidden" />
with that variable, when it isn't, you can give the user the possibility to enter a name in <input type="text" />
.
In response to your comment, you maybe can try this. I think that it does what you're looking for. Probably, it isn't the most attractive solution, but I think it'll work.
Name: <input type="text" name="name"><br>
Select from one of the following:<br>
<fieldgroup>
<?php
# Set empty form
$_Form = NULL;
# Check submit
if(isset($_POST['food'])) {
# Check pasta
if($_POST['food'] == 'pasta') {
$_Form .= '<input type="hidden" name="foodSelectedPasta" value="Pasta - ' . $_POST['name'] . '">Pasta - ' . $_POST['name'] . '</input><br>';
} else {
# Check already chosen
if(isset($_POST['foodSelectedPasta'])) {
$_Form .= '<input type="hidden" name="foodSelectedPasta" value="' . $_POST['foodSelectedPasta'] . '">' . $_POST['foodSelectedPasta'] . '</input><br>';
} else {
$_Form .= '<input type="radio" name="food" value="pasta">Pasta</input><br>';
}
}
# Check salad
if($_POST['food'] == 'salad') {
$_Form .= '<input type="hidden" name="foodSelectedSalad" value="Salad - ' . $_POST['name'] . '">Salad - ' . $_POST['name'] . '</input><br>';
} else {
# Check already chosen
if(isset($_POST['foodSelectedSalad'])) {
$_Form .= '<input type="hidden" name="foodSelectedSalad" value="' . $_POST['foodSelectedSalad'] . '">' . $_POST['foodSelectedSalad'] . '</input><br>';
} else {
$_Form .= '<input type="radio" name="food" value="salad">Salad</input><br>';
}
}
# Check burgers
if($_POST['food'] == 'burgers') {
$_Form .= '<input type="hidden" name="foodSelectedBurgers" value="Burgers - ' . $_POST['name'] . '">Burgers - ' . $_POST['name'] . '</input><br>';
} else {
# Check already chosen
if(isset($_POST['foodSelectedBurgers'])) {
$_Form .= '<input type="hidden" name="foodSelectedBurgers" value="' . $_POST['foodSelectedBurgers'] . '">' . $_POST['foodSelectedBurgers'] . '</input><br>';
} else {
$_Form .= '<input type="radio" name="food" value="burgers">Burgers</input><br>';
}
}
} else {
$_Form .= '<input type="radio" name="food" value="pasta">Pasta</input><br>';
$_Form .= '<input type="radio" name="food" value="salad">Salad</input><br>';
$_Form .= '<input type="radio" name="food" value="burgers">Burgers</input><br>';
}
# Output form
echo $_Form;
?>
</fieldgroup>
If you want to have all of this on a single page, you can change the name attribute of the hidden elements to be arrays. Then, check if a particular field has been set in the array before deciding whether to echo the hidden element again or echoing out the radio button.
Name: <input type="text" name="name"><br>
Select from one of the following:<br>
<fieldgroup>
<?php
$form_contents = "";
// you only showed the options for food but you can extend this by adding the other fields to this array
$alreadySelected = array(
'food'=>array()
);
if(isset($_POST['selected']))
$alreadySelected = array_merge_recursive($alreadySelected,$_POST['selected']);
if(!empty($_POST['food']) && !empty($_POST['name'])) {
$alreadySelected['food'][$_POST['food']] = $_POST['name'];
}
// do some error checking
else if(!empty($_POST['name']) && empty($_POST['food'])) {
echo "<span style='color:red'>You need to pick a food when you enter your name.</span><br/>";
}
else if(empty($_POST['name']) && !empty($_POST['food'])) {
echo "<span style='color:red'>Please enter your name when you pick a food</span>.<br/>";
}
// use the label as the key and the value of the input as the value
$foods = array(
'Pasta'=>'pasta'
,'Salad'=>'salad'
,'Burgers'=>'burgers'
);
foreach($foods as $label=>$value) {
if(isset($alreadySelected['food'][$value])) {
// use the name of the person who selected the option as the value so that we can remember it for further submissions
$form_contents .= "<input type='hidden' name='selected[food][" . $value . "]' value='" . $alreadySelected['food'][$value] . "'>" . $label . " - " . $alreadySelected['food'][$value] . "<br>";
}
else {
$form_contents .= "<label><input type='radio' name='food' value='" . $value . "'>" . $label . "</label><br>";
}
}
echo $form_contents;
?>
</fieldgroup>