[英]Keep selections in php generated form after submit (POST)
我目前正在使用 php 用數據庫中的選擇填充表單。 用戶在 select 樣式表單中選擇選項並提交此表單,這會在使用第二個提交按鈕完成交互之前更新表單下方的選擇摘要。
我的問題是,每次用戶使用第一次提交時,以前的選擇不會堅持。 他們必須再次通過整個表格 go。
有沒有辦法在不訴諸 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>    ";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/會話執行類似的操作。
將變量回顯到表單元素的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
function ,我將以下參數傳遞給它:
// 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");
}
我知道這是向您拋出的很多新代碼,但是如果您可以從數據庫中獲取 select 值到數組中,那么其他所有內容都應該很好地到位。
我唯一要補充的是,在我們調用doSelect
的開頭,我會將其放在if
語句中,因為您不想將尚未設置的內容設置為 selected :
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, "", "");
}
我希望這會有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.