如何在处理不同的If / Else块时保留下拉列表?

时间:2013-03-05 07:39:51

标签: php html mysql

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

所以基本上,我的问题是如何使下拉列表停留在IF / ELSE块内。当它通过另一个IF / ELSE块传递一个值(submit)时,它基本上消失了,所以我正在尝试一个代码,我不必在下一个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");
}

2 个答案:

答案 0 :(得分:1)

使用jquery for it。它简单易用。 对于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查询。

答案 1 :(得分: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>";
}