数据未显示在选择标记中

时间:2017-03-28 18:46:14

标签: javascript php html sql select

我有一个章节的select标签,我正在从中加载查询结果。但它没有显示在选择标记中,章节名称显示为echo语句。

     <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MCQ Questions</title>

</head>
<body>

<?php
session_start();

$dbh = new PDO('mysql:host=174.75.54;dbname=handbook', 'airman', 'airman'); 

    $type = $_POST['type'];
    $question = $_POST["question"];
    $optionA = $_POST["opt1"];
    $optionB = $_POST["opt2"];
    $optionC = $_POST["opt3"];
    $optionD = $_POST["opt4"];
    $ans = $_POST["ans"];
    $chapter = $_POST["chapter"];
?>

<form method="post" action="mcq.php" enctype="multipart/form-data">


<?php if(isset($_POST['question']))
{
?>

Enter the question: <input name="question" type="text" value = "<?php echo $question?>"</input> <br><br>
<?php
}
else
{
?>
    <p> Enter the question :</p> <input name="question" type="text"></input> <br><br>

<?php
}
?>

Select rank : 

<select name="type" id="type" onchange="this.form.submit()"> 

<?php if(isset($_POST['type']))
{ ?>
<option value="1"  <?php echo($type==1?"selected":"");?>>SSgt</option> 

<option value="2"  <?php echo($type==2?"selected":"");?>>TSgt</option> 
<option value="3" <?php echo($type==3?"selected":"");?>>MSgt</option>
</select>
<?php
}
    else 
    {
    ?>
    <option value="1">SSgt</option> 
<option value="2">TSgt</option> 
<option value="3">MSgt</option> 
</select>
    <?php
    }
    ?>

<br><br>
<?php if(isset($_POST['opt1']))
{ ?>
<p> Enter options :</p>
Enter option A : <input name="opt1" type="text" value = "<?php echo $optionA?>"</input> <br><br>

<?php
}
else{
?>
<p> Enter options :</p>
Enter option A : <input name="opt1" type="text"> <br><br>
<?php   
}
?>
<?php if(isset($_POST['opt2']))
{ ?>

Enter option B : <input name="opt2" value = "<?php echo $optionB?>"</input> <br><br>

<?php
}
else{
?>
Enter option B : <input name="opt2" type="text"> <br><br>
<?php   
}
?>

<?php if(isset($_POST['opt3']))
{ ?>
Enter option C : <input name="opt3" value = "<?php echo $optionC?>"</input> <br><br>

<?php
}
else{
?>

Enter option C : <input name="opt3" type="text"> <br><br>

<?php
}
?>

<?php if(isset($_POST['opt4']))
{ ?>
Enter option D : <input name="opt4" value = "<?php echo $optionD?>"</input> <br><br>
<?php
}
else{
?>

Enter option D : <input name="opt4" type="text"> <br><br>

<?php
}
?>


Select correct answer :

<select name="ans" id="type"> 


<?php if(isset($_POST['ans']))
{
?>
<option value="A"  <?php echo($ans==A?"selected":"");?>>A</option> 

<option value="B"  <?php echo($ans==B?"selected":"");?>>B</option> 

<option value="C" <?php echo($ans==C?"selected":"");?>>C</option>

<option value="D" <?php echo($ans==D?"selected":"");?>>D</option
</select>

<br><br>
<?php
}
else
{
?>

<option value="A">A</option> 
<option value="B">B</option> 
<option value="C">C</option> 
<option value="D">D</option>
</select>
<?php
}
?>
<br><br>

Select Chapter : 
<select name="chapters" id="chapters"> 

<?php 

if(isset($_POST['type']))
{
    $stmt = $dbh->prepare("SELECT * FROM chapters where type = :type"); 
    $stmt->bindParam("type", $type);
$stmt->execute(); 
$results = $stmt->fetchall(PDO::FETCH_ASSOC); 

if(count($results > 0)){ 
foreach($results as $row):?> 
<option value="<?php echo $row['id'];?>"><?php echo $row['title'];?></option> 
<?php 
endforeach; 
}else{?> 

<option value="0">No data found</option> 
<?php 
} 

}

else{

$stmt = $dbh->prepare("SELECT * FROM chapters where type = 1"); 

$stmt->execute(); 
$results = $stmt->fetchall(PDO::FETCH_ASSOC); 

if(count($results > 0)){ 
foreach($results as $row):?> 
<option value="<?php echo $row['id'];?>"><?php echo $row['title'];?></option> 
<?php 
endforeach; 
}else{?> 

<option value="0">No data found</option> 
<?php 
} 
}
?> 

</select> <br><br>

<input type="Submit" value = "Submit" name="b1">

<?php

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

ini_set('display_errors', 1); 
error_reporting(1); 
ini_set('error_reporting', E_ALL); 

$type = $_POST['type'];
$question = $_POST['question'];
$optionA = $_POST['opt1'];
$optionB = $_POST['opt2'];
$optionC = $_POST['opt3'];
$optionD = $_POST['opt4'];
$ans = $_POST['ans'];
$chapter = $_POST['chapters']; 

    $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
$stmt->execute(array($question, $optionA, $optionB, $optionC, $optionD, $ans,$type,$chapter));

if ($dbh->lastInsertId())
{   
    echo 'Question submitted.';

    echo '<a href="mcq.php">Upload another question.</a>';
    session_destroy();
}
else
{
    echo 'Question could not submit.';
}
}
?>

</form>
</body>
</html>

如果未设置type,它会在第一次加载select标签中的数据,但如果设置了类型,则会显示echo语句等章节。

这里有什么问题?请帮忙谢谢。

1 个答案:

答案 0 :(得分:3)

我在php中构建我的选项然后像这样回显整个字符串 在PHP

IF ($row[$i]['abc'] == 'A'){
$a[$i]='<option value="A" selected="selected">A</option>';
$b[$i]='<option value="B" >B</option>';
}
IF ($row[$i]['abc'] == 'B'){
$a[$i]='<option value="A" >A</option>';
$b[$i]='<option value="B" selected="selected" B</option>';
}

在我的HTML中

 <select>
    <php? echo $a[$i]; ?>
    <php? echo $b[$i]; ?>
 </select>

始终工作并将其设置在正确的数据上

练习和学习创建一个名为try1.php的文件

<?php

$1='<option value="1">1</option>';
$2='<option value="2">2</option>';
//name of your selection
if(isset($_POST['1']){
echo 'you selected : '.$_post['1'];

}
?>
<html>
<body>
<select name="1">
<?php echo $1; ?>
<?php echo $2; ?>
</select>
<input type="submit" name="1" value="1">
</body>
</html>

这是我为我妻子制作的文件中的代码,它已经工作了2年,但它已经很老了,并没有获得所选内容的功能

<?php
session_start();
include_once("../staffpage/getstaff.php");
include_once("../staffpage/staffheader.php");
include_once("../cert/dbc.php");
?>
<html>
   <head>
   </head>
      <body>
        <h1>&#20250;&#21592;&#27880;&#20876;</h1>

       <form action="direct to you php file handling your indput.php" method="post">
          <tr>
             <td>
                <table width="60%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                 <tr>
                    <td colspan="3"><strong>&#20250;&#21592;&#27880;&#20876;</strong></td>
                 </tr>
                  <tr>
             <td width="78">&#21345;&#21495;</td>
         <td width="6">:</td>

         <td width="294"><input type="text" name="cno" required/></td>
         </tr>
         <td width="78"> &#25910;&#25454;&#21495;</td>
         <td width="6">:
         </td>

         <td width="294"><input type="text" name="rno" required/></td>

         </td>
         </tr>
         <td width="78"> &#22995;&#21517;</td>
         <td width="6">:</td>
         <td width="294"><input type="text" name="fname" required/></td>
         </tr>


         <td width="78">&#30005;&#35805;</td>
         <td width="6">:</td>
         <td width="294"><input type="text" name="ctel" required/></td>
         </tr>


         <td width="78"> cardname </td>
         <td width="6">:</td>
         <td width="294">
         <select name="cardnameno" >
            <?php
                //my query in this file
                include_once("../sec/cards.php");
                $i=0;
                while($row = mysql_fetch_array($result)) {
                $z2=$row['cardname'];
                $z1=$row['cardnameno'];
                ?>

            <option value="<?php echo $row['cardnameno']; ?>" ><?php echo $row['name']; ?></option>
            <?php
            $i++;
            }
            ?>  
        </select>

        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><input type="submit" /></td>
        </tr>
    </table>
</form></p>
  </div>
</form>
</body>
</html>
</html>

我将此文件包含在另一个文件中但不应阻止其工作