提交(POST)后,请保持选择以php生成的形式

| 我目前正在使用php从数据库中选择内容填充表单。用户在选择样式表单中选择选项并提交,这会在使用第二个提交按钮完成交互之前更新表单下方选择的摘要。 我的问题是,每次用户使用第一个提交时,以前在那里的选择都不会保留。他们必须再次经历整个表格。 有没有办法保持这些选择不诉诸php if语句?有很多选择,因此对于每个使用php都会很痛苦。此外,表单是通过POST提交的。 表格样本:
<?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>&nbsp&nbsp&nbsp&nbsp\";echo $_POST[\'product_color\']; ?>
</div>
    
已邀请:
将变量回显到表单元素的“ 4”标签中。如果您发布了所有代码,我相信我可以为您提供帮助。 更新 嗯,所以它们是下拉列表,您需要记住选择了什么?抱歉,昨天我匆忙阅读了您的帖子,并认为这是一种带有文本输入的表格。 我自己也做了类似的事情,但是没有尝试您的代码,让我看看是否可以帮忙。 基本上,您需要在下拉列表中将一个值设置为“ 5” 当我必须这样做时,我将下拉列表中的值放在这样的数组中:
$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\");
}
我知道您已经收到了很多新代码,但是如果您可以将db中的选择值放入数组中,那么其他所有内容都应该很好地放在了一起。 我唯一要添加的是,在开始时我们称为
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, \"\", \"\"); 
    }
希望对您有所帮助!     
我假设您将用户的选择存储在单独的表中。如果是这种情况,则需要添加一些逻辑来确定是否应显示表单值或已存储的内容。
<?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>&nbsp&nbsp&nbsp&nbsp\";echo $_POST[\'product_color\']; ?>
</div>
因此,在将用户的选择存储在数据库中之后,您可以将其重定向到URL中带有新config_id的页面,以加载保存的值。如果您没有将选定的值存储在表中,则可以对Cookie /会话执行类似的操作。     

要回复问题请先登录注册