我目前使用php从数据库中选择填充表单。用户在选择样式表单中选择选项并提交,这会在使用第二个提交按钮完成交互之前更新表单下面的选择摘要。
我的问题是,每次用户使用第一次提交时,之前的选择都不会坚持。他们得把整个表格再看一遍。
是否有办法保持这些选择,而不诉诸php if语句?有大量的选项,所以它将是一个痛苦的使用php为每一个。此外,表单是通过POST提交的。
示例from form:
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'COLOR' ORDER BY cat")
or die(mysql_error());
echo "<div id='color'><select id='color' name='product_color'>";
while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";}
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>
我尝试使用这个js片段来重新填充选择,但它似乎不能正常工作…
<script type="text/javascript">document.getElementById('color').value = "<?php echo $_GET['proudct_cpu'];?>";</script>
这似乎不起作用。除了php if语句,还有什么建议吗?
谢谢!
编辑:这基本上是我正在使用的表单设置,尽管我已经大大缩短了它,因为实际实现相当长。
// Make a MySQL Connection
<?php mysql_connect("localhost", "kp_dbl", "mastermaster") or die(mysql_error());
mysql_select_db("kp_db") or die(mysql_error());
?>
<br />
<form action="build22.php" method="post">
<input type="hidden" name="data" value="1" />
<br />
<br />
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat")
or die(mysql_error());
echo "<div id='color'><select id='color' name='product_color'>";
while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";}
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>
<input type="submit" value="Update Configuration">
</form>
上述表单中的选择在提交后得到回显,为用户提供如下更新:
<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
<h1>Current Configuration:</h1>
<?php echo "<strong>Color:</strong>    ";echo $_POST['product_color']; ?>
</div>
我假设您将用户的选择存储在一个单独的表中。如果是这种情况,您需要添加一些逻辑来确定是否应该显示表单值或已经存储的内容。
<?php
// form was not submitted and a config id was passed to the page
if (true === empty($_POST) && true === isset($_GET['config_id']))
{
// make sure to properly sanitize the user-input!
$rs = mysql_query("select * from saved_configuration where config_id={$_GET['config_id']}"); // make sure to properly sanitize the user-input!
$_POST = mysql_fetch_array($rs,MYSQL_ASSOC); // assuming a single row for simplicity. Storing in _POST for easy display later
}
?>
<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
<h1>Current Configuration:</h1>
<?php echo "<strong>Color:</strong>    ";echo $_POST['product_color']; ?>
</div>
因此,在将用户的选择存储在数据库中之后,您可以将它们重定向到URL中具有新config_id的页面以加载保存的值。如果没有将选中的值存储在表中,可以对cookie/sessions执行类似的操作。
将变量返回到表单元素的value
标记中。如果你把你所有的代码贴出来,我相信我能帮到你。
啊,所以他们是下拉列表,你需要记住选择了什么?抱歉,我昨天匆忙看了你的帖子,以为那是一个有文本输入的表单。
我自己也做了类似的事情,但是没有尝试你的代码,让我看看我能不能帮上忙。
基本上你需要做的就是在下拉菜单中设置一个值为selected="selected"
当我必须这样做时,我将下拉列表值放在一个数组中,如下所示:
$options = array( "stack", "overflow", "some", "random", "words");
// then you will take your GET variable:
$key = array_search($_GET['variablename'], $options);
// so this is saying find the index in the array of the value I just told you
// then you can set the value of the dropdown to this index of the array:
$selectedoption = $options[$key];
这是它可能令人困惑的地方,因为我的代码是不同的,所以如果你想使用它,你可能需要重构一个位
我有一个doSelect
函数,我向它传递以下参数:
// what we are passing is: name of select, size, the array of values to use and the
// value we want to use as the default selected value
doSelect("select_name", 1, $options, $selectedoption, "");
// these are the two functions I have:
// this one just processes each value in the array as a select option which is either
// the selected value or just a 'normal' select value
FUNCTION doOptions($options, $selected)
{
foreach ($options as $option)
{
if ($option == $selected)
echo ("<option title='"$title'" id='"$value'" selected>$option</option>'n");
else
echo ("<option title='"$title'" id='"$value'">$option</option>'n");
}
}
// this is the function that controls everything - it takes your parameters and calls
// the above function
FUNCTION doSelect($name, $size, $options, $selected, $extra)
{
echo("<select class='"'" id='"$name'" name='"$name'" size='"$size'" $extra>'n");
doOptions($options, $selected);
echo("</select>'n");
}
我知道这是一堆新代码,但如果你能把你的选择值从数据库到数组,那么其他的一切都应该很好地到位。
我唯一要补充的是,在开始我们调用doSelect
的地方,我会把它放在if
语句中,因为您不想将未设置的东西设置为已选:
if (isset($_GET['variable']))
{
$key = array_search($_GET['variablename'], $options);
$selectedoption = $options[$key];
doSelect("select_name", 1, $options, $selectedoption, "");
}
else
{
doSelect("select_name", 1, $options, "", "");
}
我希望这对你有帮助!