Data not showing in select tag

I have a dedicated chapter tag, I am loading the result from a query in it. But without showing it in the selected tag, the section titles are displayed as echo expressions.

     <!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>

      

It loads data into the select tag the first time if no type is set, but if that type is set, it displays chapters like echo expressions.

What's wrong here? Please help thank you.

+3


source to share


1 answer


I create my parameters in php and then echo a whole line like this in 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>';
}

      

in my html

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

      

always works and sets the correct data



for practice and learning create a file named 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>

      

this is the code from a file i made for my wife and it worked for 2 years but it is very old and has no function to get what is selected

<?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>

      

i includes this file in another, but shouldn't stop it from running

+3


source







All Articles