操作下拉列表的默认值
Manipulating the default value of a drop down list
我有一个下拉输入选择 "Evaluation Test Type",它根据选择显示某些数据,其下方有一个提交按钮。现在我添加到:"Evaluation Test Type" 默认值 <option selected='selected'></option>
但是如果选择了此选项并单击了提交 1,我想阻止提交按钮出现
$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>
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>
<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}: {$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
{
}
}
利用 while 循环。您正在使用 while 循环意味着会减少代码数量,它会自动调整下拉列表。
如果数据库中有 5 个列表,那么它会自动调整为 5。
谢谢
是否必须在 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();
}
});
如果您只是想阻止用户选择空白选项,只需使用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>
您可以检查 $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'");
不需要条件
空默认值有什么意义?我相信你有理由。如果它为空(默认),我可能只是检查该值。如果不为空,则渲染提交,否则不渲染。
这个答案和cbugs基本一样,但是更容易read/understand。请注意,第三个 $html[] 赋值现在取决于类型 (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>
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>
<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}: {$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>";
}
我有一个下拉输入选择 "Evaluation Test Type",它根据选择显示某些数据,其下方有一个提交按钮。现在我添加到:"Evaluation Test Type" 默认值 <option selected='selected'></option>
但是如果选择了此选项并单击了提交 1,我想阻止提交按钮出现
$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>
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>
<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}: {$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
{
}
}
利用 while 循环。您正在使用 while 循环意味着会减少代码数量,它会自动调整下拉列表。 如果数据库中有 5 个列表,那么它会自动调整为 5。 谢谢
是否必须在 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();
}
});
如果您只是想阻止用户选择空白选项,只需使用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>
您可以检查 $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'");
不需要条件
空默认值有什么意义?我相信你有理由。如果它为空(默认),我可能只是检查该值。如果不为空,则渲染提交,否则不渲染。
这个答案和cbugs基本一样,但是更容易read/understand。请注意,第三个 $html[] 赋值现在取决于类型 (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>
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>
<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}: {$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>";
}