Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating the default value of a drop down list

I have a dropdown input selection "Evaluation Test Type" that based on the selection certain data appear with beneath it a submit button. Now i added to : "Evaluation Test Type" a default value of <option selected='selected'></option> however i want to prevent the submit button from appearing if this option was chosen and submit1 was clicked

$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
    {
    }

}
like image 909
dan Avatar asked Aug 13 '15 05:08

dan


People also ask

How do I change the default value in drop down?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.


Video Answer


1 Answers

If you just want to prevent users from choosing the blank option, just use disabled attribute on it. Then use required attribute for the select element to prevent them from submitting with a blank "Evaluation Test Type" value. Don't forget to add value='' on the blank option for the required attribute to work as answered 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>
like image 135
Chester M Avatar answered Sep 17 '22 15:09

Chester M