操作下拉列表的默认值

时间:2015-08-13 05:38:48

标签: php html html-select

我有一个下拉输入选择&#34;评估测试类型&#34;根据选择,某些数据会在其下方显示一个提交按钮。现在我添加到:&#34;评估测试类型&#34;默认值为<option selected='selected'></option>但是我想阻止在选择此选项并单击submit1时出现提交按钮

$options = '';
$filter=mysql_query("select afnumber from employees WHERE Status='Employed'");
while($row = mysql_fetch_array($filter)) {
    $options .="<option >" . $row['afnumber'] . "</option>";
}
$menu="<form id='filter' name='filter' method='post' action=''>
AFNumber : <select name='SelectAF' id='filter' style='color:grey;'>" . $options . "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Evaluation Test Type : <select name='Type' id='type' style='color:grey;'><option selected='selected'></option><option value='loyalty'>Loyalty</option><option value='performance'>Performance</option></select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type='submit' name='submit1' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
</form>
<br>
";
 echo $menu;

if(isset($_POST['submit1']))

{   
$type = $_POST['Type'];

$mysqli = new mysqli("localhost", "root", "Js", "jr");
/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

 if ( $result = $mysqli->query( "SELECT questiontext FROM questioninfo WHERE type='$type'" ) ) {


        $html=array();

        $html[]="
        <form action='' method='post' id='quiz'>
            <ol>";

        $counter=1;

        while( $row = $result->fetch_array() ) {


            $question=$row['questiontext'];
            $answerA=1;
            $answerB=2;
            $answerC=3;
            $answerD=4;
            $answerE=5;

            $html[]="
             <br/>
                <h3>Question {$counter}:&nbsp; {$question}</h3>

                <li>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-$counter-answersA' value='1' />
                    <label for='question-{$counter}-answers-A'> {$answerA} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersB' value='2' />
                    <label for='question-{$counter}-answers-B'> {$answerB} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersC' value='3' />
                    <label for='question-{$counter}-answers-C'> {$answerC} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersD' value='4' />
                    <label for='question-{$counter}-answers-D'> {$answerD} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersE' value='5' />
                    <label for='question-{$counter}-answers-E'> {$answerE} </label>

                </li>";

            $counter++;

        }

        $html[]="
            </ol>
        <input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
        <input type='hidden' name='type' value='{$type}' />
        </form>";

        echo implode( PHP_EOL, $html );



    $result->close();

 }
}

if( isset( $_POST['submit'] ) ){ 

    $mysqli = new mysqli("localhost", "root", "Js", "jr");
    if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();}

if ($result = $mysqli->query("SELECT * FROM questioninfo WHERE Type='performance'")) {

    $row_cnt = $result->num_rows;
    $result->close();
}
if ($result = $mysqli->query("SELECT * FROM questioninfo WHERE Type='loyalty'")) {

    $row_cnt1 = $result->num_rows;
    $result->close();
} 

$numQuestions=$row_cnt;
$numQuestions1=$row_cnt1; 
    $type = $_POST['type']; 
if($type == 'performance')
{
for( $counter=1; $counter <= $numQuestions; $counter++ ){
$type = $_POST['type']; 
$answer = $_POST['question-'.$counter.'-answers']; 
$sql="insert into `question` (`Type`,`Value`) values ('".$type."','".$answer."')"; 
$mysqli->query($sql);
} 
}
    else if($type == 'loyalty')
    {
for( $counter=1; $counter <= $numQuestions1; $counter++ ){
$type = $_POST['type']; 
$answer = $_POST['question-'.$counter.'-answers']; 
$sql="insert into `question` (`Type`,`Value`) values ('".$type."','".$answer."')"; 
$mysqli->query($sql);
} 

}
    else
    {
    }

}

6 个答案:

答案 0 :(得分:9)

如果您只是想阻止用户选择空白选项,只需使用disabled属性即可。然后对select元素使用required属性,以防止它们使用空白的“评估测试类型”值进行提交。不要忘记在空白选项中添加value='',以使所需属性的工作方式为here

Evaluation Test Type : 
<select name='Type' id='type' style='color:grey;' required>
    <option value='' selected disabled></option>
    <option value='loyalty'>Loyalty</option>
    <option value='performance'>Performance</option>
</select>

答案 1 :(得分:0)

利用while循环。您正在使用while循环意味着将减少代码编号,它将自动调整下拉列表。 如果数据库中有5个列表,那么它将自动调整为5。 谢谢

答案 2 :(得分:0)

这是否必须在PHP中完成?看起来你在按下submit1后重新加载页面,这不是一个非常用户友好的方法。

通常,解决这样的接口问题的最佳方法是jQuery,使用ajax请求根据需要查询数据库。这让你可以使用jQuery直接评估和操作DOM,这对PHP来说并不好。

基本模式:单击submit1时,检查下拉列表的值。使用Ajax使用该值查询数据库,以便您可以填充并显示第二个下拉列表。如果第一个下拉列表的值是默认值,请保持隐藏第二个提交按钮。如果不是,请显示第二个提交按钮。

这样的事情:

$('#submit1').click(function() {
    selectValue = $('#EvaluationTestType').val();
    $.post('path-to-php-script.php',{testType:selectValue},function(data) {
        //get new data from database, build second dropdown

        //show second dropdown
        $('#secondDropdown').show();
        //conditionally show second submit button
        if(selectValue != 'defaultValue') {
            $('#submit2').show();
        }
});

答案 3 :(得分:0)

您可以检查$ type值并仅在提交按钮不为空的情况下取消提交: -

$html[]="
    </ol>
".if($type!=""){"<input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>"}.
<input type='hidden' name='type' value='{$type}' />
</form>";

另一件事,我没有看到在那里使用条件的必要性: -

if ($result = $mysqli->query("SELECT * FROM questioninfo WHERE Type='performance'")) {

    $row_cnt = $result->num_rows;....

你可以这样做:

$result = $mysqli->query("SELECT * FROM questioninfo WHERE Type='$type'");

没有条件。

答案 4 :(得分:0)

空缺会有什么意义?我相信你有理由。我可能只是检查它是否为空(默认值)。如果它不为空,则渲染提交,否则不要。

答案 5 :(得分:0)

这个答案与cbugs基本相同,但更容易阅读/理解。请注意,第三个$ html []赋值现在取决于type(if ($type) { ....)的值。

$options = '';
$filter=mysql_query("select afnumber from employees WHERE Status='Employed'");
while($row = mysql_fetch_array($filter)) {
    $options .="<option >" . $row['afnumber'] . "</option>";
}
$menu="<form id='filter' name='filter' method='post' action=''>
AFNumber : <select name='SelectAF' id='filter' style='color:grey;'>" . $options . "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Evaluation Test Type : <select name='Type' id='type' style='color:grey;'><option selected='selected'></option><option value='loyalty'>Loyalty</option><option value='performance'>Performance</option></select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type='submit' name='submit1' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
</form>
<br>
";
 echo $menu;

if(isset($_POST['submit1']))

{   
$type = $_POST['Type'];

$mysqli = new mysqli("localhost", "root", "Js", "jr");
/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

 if ( $result = $mysqli->query( "SELECT questiontext FROM questioninfo WHERE type='$type'" ) ) {


        $html=array();

        $html[]="
        <form action='' method='post' id='quiz'>
            <ol>";

        $counter=1;

        while( $row = $result->fetch_array() ) {


            $question=$row['questiontext'];
            $answerA=1;
            $answerB=2;
            $answerC=3;
            $answerD=4;
            $answerE=5;

            $html[]="
             <br/>
                <h3>Question {$counter}:&nbsp; {$question}</h3>

                <li>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-$counter-answersA' value='1' />
                    <label for='question-{$counter}-answers-A'> {$answerA} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersB' value='2' />
                    <label for='question-{$counter}-answers-B'> {$answerB} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersC' value='3' />
                    <label for='question-{$counter}-answers-C'> {$answerC} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersD' value='4' />
                    <label for='question-{$counter}-answers-D'> {$answerD} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersE' value='5' />
                    <label for='question-{$counter}-answers-E'> {$answerE} </label>

                </li>";

            $counter++;

        }


        if ($type)
        {

            $html[]="
             </ol>
             <input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
             <input type='hidden' name='type' value='{$type}' />
             </form>";

        } else {

            $html[]="
            </ol>
            <input type='hidden' name='type' value='{$type}' />
            </form>";

        }
        echo implode( PHP_EOL, $html );



    $result->close();

 }
}

if( isset( $_POST['submit'] ) ){ 

    $mysqli = new mysqli("localhost", "root", "Js", "jr");
    if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();}

if ($result = $mysqli->query("SELECT * FROM questioninfo WHERE Type='performance'")) {

    $row_cnt = $result->num_rows;
    $result->close();
}
if ($result = $mysqli->query("SELECT * FROM questioninfo WHERE Type='loyalty'")) {

    $row_cnt1 = $result->num_rows;
    $result->close();
} 

$numQuestions=$row_cnt;
$numQuestions1=$row_cnt1; 
    $type = $_POST['type']; 
if($type == 'performance')
{
for( $counter=1; $counter <= $numQuestions; $counter++ ){
$type = $_POST['type']; 
$answer = $_POST['question-'.$counter.'-answers']; 
$sql="insert into `question` (`Type`,`Value`) values ('".$type."','".$answer."')"; 
$mysqli->query($sql);
} 
}
    else if($type == 'loyalty')
    {
for( $counter=1; $counter <= $numQuestions1; $counter++ ){
$type = $_POST['type']; 
$answer = $_POST['question-'.$counter.'-answers']; 
$sql="insert into `question` (`Type`,`Value`) values ('".$type."','".$answer."')"; 
$mysqli->query($sql);
} 

}
    else
    {
    }

}

因此,更改的代码部分是:

    if ($type)
    {

        $html[]="
         </ol>
         <input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
         <input type='hidden' name='type' value='{$type}' />
         </form>";

    } else {

        $html[]="
        </ol>
        <input type='hidden' name='type' value='{$type}' />
        </form>";

    }
相关问题