0

我是 PHP-to-Mysql 的新手,所以我正在使用 mysql_* 其他人已经声明不再支持的函数。但我发现这个地方最适合问我的问题,因为我找不到任何有类似问题的人。

所以基本上,我的问题是如何使下拉列表保持在 IF/ELSE 块内。当它通过另一个 IF/ELSE 块传递一个值(提交)时,它基本上消失了,所以我正在尝试一个代码,我不必在下一个 IF/ELSE 块中重写整个下拉代码。因为我正在尝试根据下拉列表中选择的选项显示一个文本框(它具有自动提交功能)。这可能吗?或者我必须求助于其他库(jQuery、javascript 等)?

如果有帮助,这是代码的一部分:

elseif($_POST['question'] == edit ){
    $res = mysql_query("SELECT questionID FROM questions");

    echo "<form action='' method='post'>
    Edit Question No.
        <select name='question_select' onchange=this.form.submit()>
            <option  value=null selected>--</option>";

            while($row = mysql_fetch_array($res))
            {
                echo "<option value=\"".$row['questionID']."\">".$row['questionID']."</option>";
            }

    echo "</select></form>";
  }

//TEXTBOX TO BE DISPLAYED WHEN A NUMBER IS SELECTED IN EDIT EXISTING
if (isset($_POST['question_select'])){
    $res = mysql_query("SELECT question FROM questions WHERE questionID='{$_POST['question_select']}'");
    $row = mysql_fetch_assoc($res);     

    echo "Editing Question ",$_POST['question_select'],"<br>
    <form action='' method='post'>
        <textarea name='edited_question' rows='4' cols='50'>",$row['question'],"</textarea><br>
        <input type='submit' name='save' value='Save'>
        <input type='hidden' name='question_num' value='{$_POST['question_select']}'>
        <input type='submit' name='cancel' value='Cancel'>
    </form>";
}

//PASSING OF VALUES WHEN SAVE IS PRESSED
if (isset($_POST['save'])){
    $edited_question = trim($_POST['edited_question']);
    mysql_query("UPDATE questions SET question='$edited_question' WHERE questionID='{$_POST['question_num']}'") or die(mysql_error);
    header("Location:admin_questions.php");
}
4

2 回答 2

1

为此使用jquery。它易于使用。对于 depanding 下拉,jquery 代码是:

$(document).ready(function(e) {
  $("#abc").change(function()
    {
     var firstfeild= $("#firstfeild").val();
     $.post("getdata.php",{"firstfeild":firstfeild},function(data)
       {
        $("#abc").html(data);   
       });
   });
});

在 getdata.php 中,只需编写 mysql 查询。

于 2013-03-05T07:52:09.563 回答
0
function renderDropdown($res)
{
    echo "
    <select name='question_select' onchange=this.form.submit()>
        <option  value=null selected>--</option>";
        while($row = mysql_fetch_array($res))
        {
            echo "<option value=\"".$row['questionID']."\">".$row['questionID']."</option>";
        }
    echo "</select>";
}

if(1==1)
{
    echo "<form action='' method='post'>Edit If Question No.";
    renderDropdown($res);
    echo "</form>";
}
else
{
    // $someOtherRes
    echo "<form action='' method='post'>Edit Else Question No.";
    renderDropdown($someOtherRes);
    echo "</form>";
}
于 2013-03-05T08:05:31.220 回答