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.
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.