我目前正在为一个更大的项目开发一个小注册表,它将整合它,我正在试图弄清楚如何将隐藏的表单字段集成到其中,以保存提交的数据。基本上,每当有人在表单上注册时,他们的名字应该出现在条目旁边,并且当下一个人提交表单时该条目不应该改变(即,它不应该再次可用)。这就是我到目前为止所做的工作:
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>
目前,此表单存储初始条目,但下次提交时,该名称将被删除,选择将再次可用。是否有任何方法可以保留该条目,以便每次提交表单时都保留该条目?任何帮助表示赞赏!
答案 0 :(得分:1)
我认为您对<input type="hidden" />
的想法很有效。
也许您可以了解<input>
的工作原理,here。您不必像<input></input>
那样执行此操作,只需为输入提供名称和值。
每次提交,您都可以检查是否已经提供了当前变量。如果是,您可以使用该变量设置<input type="hidden" />
,如果不是,您可以让用户在<input type="text" />
中输入名称。
在回复您的评论时,您可以试试这个。我认为它可以满足您的需求。可能它不是最有吸引力的解决方案,但我认为它会起作用。
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>
答案 1 :(得分:0)
如果要在一个页面上拥有所有这些内容,可以将隐藏元素的name属性更改为数组。然后,在决定是否再次回显隐藏元素或回显单选按钮之前,检查数组中是否已设置特定字段。
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>