php – 操作一个下拉列表的默认值

前端之家收集整理的这篇文章主要介绍了php – 操作一个下拉列表的默认值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个下拉输入选择“评估测试类型”,根据选择某些数据出现在它下面的提交按钮.现在我添加到:“评估测试类型”默认值为< option selected ='selected'>< / option>但是,如果选择了该选项并单击了submit1,我想阻止提交按钮出现
  1. $options = '';
  2. $filter=MysqL_query("select afnumber from employees WHERE Status='Employed'");
  3. while($row = MysqL_fetch_array($filter)) {
  4. $options .="<option >" . $row['afnumber'] . "</option>";
  5. }
  6. $menu="<form id='filter' name='filter' method='post' action=''>
  7. AFNumber : <select name='SelectAF' id='filter' style='color:grey;'>" . $options . "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  8. 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;
  9. <input type='submit' name='submit1' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
  10. </form>
  11. <br>
  12. ";
  13. echo $menu;
  14.  
  15. if(isset($_POST['submit1']))
  16.  
  17. {
  18. $type = $_POST['Type'];
  19.  
  20. $MysqLi = new MysqLi("localhost","root","Js","jr");
  21. /* check connection */
  22. if ($MysqLi->connect_errno) {
  23. printf("Connect Failed: %s\n",$MysqLi->connect_error);
  24. exit();
  25. }
  26.  
  27. if ( $result = $MysqLi->query( "SELECT questiontext FROM questioninfo WHERE type='$type'" ) ) {
  28.  
  29.  
  30. $html=array();
  31.  
  32. $html[]="
  33. <form action='' method='post' id='quiz'>
  34. <ol>";
  35.  
  36. $counter=1;
  37.  
  38. while( $row = $result->fetch_array() ) {
  39.  
  40.  
  41. $question=$row['questiontext'];
  42. $answerA=1;
  43. $answerB=2;
  44. $answerC=3;
  45. $answerD=4;
  46. $answerE=5;
  47.  
  48. $html[]="
  49. <br/>
  50. <h3>Question {$counter}:&nbsp; {$question}</h3>
  51.  
  52. <li>
  53. <br/>
  54. <input type='radio' name='question-{$counter}-answers' id='question-$counter-answersA' value='1' />
  55. <label for='question-{$counter}-answers-A'> {$answerA} </label>
  56. <br/>
  57. <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersB' value='2' />
  58. <label for='question-{$counter}-answers-B'> {$answerB} </label>
  59. <br/>
  60. <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersC' value='3' />
  61. <label for='question-{$counter}-answers-C'> {$answerC} </label>
  62. <br/>
  63. <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersD' value='4' />
  64. <label for='question-{$counter}-answers-D'> {$answerD} </label>
  65. <br/>
  66. <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersE' value='5' />
  67. <label for='question-{$counter}-answers-E'> {$answerE} </label>
  68.  
  69. </li>";
  70.  
  71. $counter++;
  72.  
  73. }
  74.  
  75. $html[]="
  76. </ol>
  77. <input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
  78. <input type='hidden' name='type' value='{$type}' />
  79. </form>";
  80.  
  81. echo implode( PHP_EOL,$html );
  82.  
  83.  
  84.  
  85. $result->close();
  86.  
  87. }
  88. }
  89.  
  90. if( isset( $_POST['submit'] ) ){
  91.  
  92. $MysqLi = new MysqLi("localhost","jr");
  93. if ($MysqLi->connect_errno) {
  94. printf("Connect Failed: %s\n",$MysqLi->connect_error);
  95. exit();}
  96.  
  97. if ($result = $MysqLi->query("SELECT * FROM questioninfo WHERE Type='performance'")) {
  98.  
  99. $row_cnt = $result->num_rows;
  100. $result->close();
  101. }
  102. if ($result = $MysqLi->query("SELECT * FROM questioninfo WHERE Type='loyalty'")) {
  103.  
  104. $row_cnt1 = $result->num_rows;
  105. $result->close();
  106. }
  107.  
  108. $numQuestions=$row_cnt;
  109. $numQuestions1=$row_cnt1;
  110. $type = $_POST['type'];
  111. if($type == 'performance')
  112. {
  113. for( $counter=1; $counter <= $numQuestions; $counter++ ){
  114. $type = $_POST['type'];
  115. $answer = $_POST['question-'.$counter.'-answers'];
  116. $sql="insert into `question` (`Type`,`Value`) values ('".$type."','".$answer."')";
  117. $MysqLi->query($sql);
  118. }
  119. }
  120. else if($type == 'loyalty')
  121. {
  122. for( $counter=1; $counter <= $numQuestions1; $counter++ ){
  123. $type = $_POST['type'];
  124. $answer = $_POST['question-'.$counter.'-answers'];
  125. $sql="insert into `question` (`Type`,'".$answer."')";
  126. $MysqLi->query($sql);
  127. }
  128.  
  129. }
  130. else
  131. {
  132. }
  133.  
  134. }
如果您只想阻止用户选择空白选项,只需使用 disabled属性即可.然后为选择元素使用 required属性,以防止它们以空白的“评估测试类型”值提交.不要忘记在所需属性的空白选项上添加value =“’,以响应 here.
  1. Evaluation Test Type :
  2. <select name='Type' id='type' style='color:grey;' required>
  3. <option value='' selected disabled></option>
  4. <option value='loyalty'>Loyalty</option>
  5. <option value='performance'>Performance</option>
  6. </select>

猜你在找的PHP相关文章