Hii, i am new here. I have an assignment and now i am confused so is here anyone who can helpp me. I have created the code lyk-

{<?php include 'header.php' ?>
<?php include 'connection.php'; ?>

<form name="f1" method="post" >
<b>COURSE:</b>
<select name="select" id="course">
<option value= "MCA" selected > MCA </option>
<option value= "MBA" > MBA </option>
<option value= "B.TECH"  > B.TECH</option>
</select>


<b>SEMESTER:</b>

  <select name="sem" >
  <option value="">select</option>

  <option value="1" >I</option>
 <option value="2" >II</option>
 <option value="3" >III</option>
 <option value="4" >IV</option>
 <option value="5" >V</option>
 <option value="6" >VI</option>
</select>
<input type="submit" value="Click Here!!"/> </br>
</form>
<?php
if(isset($_POST['sem']))
{
$ans=$_POST['sem'];

if($ans=="1")
{   
$result=mysql_query("select Name from subject_information where sub_code like 'CA- 1%'"); ?>
<form name="f2" method="post" action="">
Subject :<select name="sub" multiple="multiple">
<?php
while($row=mysql_fetch_array($result))
{

echo "<option value='".$row['Name']."'>".$row['Name']."</option>";
}
?>
</select>
<?php
}
elseif($ans=="2")
{   
$result=mysql_query("select Name from subject_information where sub_code like 'CA- 2%'"); ?>
<form name="f3" method="post" action="">
Subject :<select name="sub" multiple="multiple">
<?php
while($row=mysql_fetch_array($result))
{

echo "<option value='".$row['Name']."'>".$row['Name']."</option>";
}?>
</select>
<?php
}

elseif($ans=="3")
{   
$result=mysql_query("select Name from subject_information where sub_code like 'CA- 3%'"); ?>
<form name="f4" method="post" action="">
Subject :<select name="sub" multiple="multiple">
<?php
while($row=mysql_fetch_array($result))
{
echo "<option value='".$row['Name']."'>".$row['Name']."</option>";
}?>
</select>
<?php
}
elseif($ans=="4")
{   
$result=mysql_query("select Name from subject_information where sub_code like 'CA- 4%'"); ?>
<form name="f5" method="post" action="">
Subject :<select name="sub" multiple="multiple">
<?php
while($row=mysql_fetch_array($result))
{
echo "<option value='".$row['Name']."'>".$row['Name']."</option>";
}?>
</select>
<?php
}
elseif($ans=="5")
{   
$result=mysql_query("select Name from subject_information where sub_code like 'CA- 5%'"); ?>
<form name="f6" method="post" action="">
Subject :<select name="sub" multiple="multiple">
<?php
while($row=mysql_fetch_array($result))
{

echo "<option value='".$row['Name']."'>".$row['Name']."</option>";
}?>
</select>
<?php
}
elseif($ans=="6")
{   
$result=mysql_query("select Name from subject_information where sub_code like 'CA- 6%'"); ?>
<form name="f7" method="post" action="">
Subject :<select name="sub" multiple="multiple">
<?php
while($row=mysql_fetch_array($result))
{
echo "<option value='".$row['Name']."'>".$row['Name']."</option>";

}?>
</select>
<?php
}
}
?>
<br>
<br>
<br>
<form name="a1"   method="post" action="generate.php">
<input type="submit" value="GENERATE" style="font-size:18px;background-color:black;color:white" size="5" onClick="window.location.href='http://localhost/vishii/format.php' ">

</form>

  <?php include 'footer.php' ?>



-
ko ko commented: confuse on what ? -2

Dani AI

Generated

Short summary: wants the subject choices from a multi-select list to arrive on another page (generate.php). The code in the thread creates several small/nested forms and prints the subject <select> separately, so the browser never sends those values to the final submit. As asked, the goal is to post the selected values; and as pointed out, database code should avoid deprecated mysql_* calls.

Correct approach (key points)

  • Keep every input that must be submitted inside a single form whose action is the processing page.
  • Use an array name for a multi-select: name="sub[]". PHP will receive an array at $_POST['sub'].
  • Escape output (e.g., htmlspecialchars) to avoid XSS. Use mysqli or PDO for any DB work.

Example (minimal)

<form method="post" action="generate.php">
  <select name="course">
    <option value="MCA">MCA</option>
    <option value="MBA">MBA</option>
  </select>

  <select name="sem">
    <option value="1">I</option>
    <option value="2">II</option>
  </select>

  <select name="sub[]" multiple size="6">
    <option value="CA101">Intro to CA</option>
    <option value="CA102">Data Structures</option>
  </select>

  <input type="submit" value="Generate">
</form>

Processing on generate.php (safe, simple)

<?php
if (!empty($_POST['sub'])) {
  $clean = array_map(function($v){ return htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); }, $_POST['sub']);
  echo "<ul>";
  foreach ($clean as $s) { echo "<li>$s</li>"; }
  echo "</ul>";
} else {
  echo "No subjects selected.";
}
?>

Troubleshooting checklist

  • Ensure the <select name="sub[]"> and the submit button are in the same form tag. Nested forms are invalid.
  • Use print_r($_POST) while debugging to see what was received.
  • If subjects are populated from the DB, generate the <option> elements inside the same form (or use AJAX to populate the select without breaking the single-form rule).
  • Use prepared statements (mysqli/PDO) for DB queries and always escape output when rendering HTML.

Recommended Answers

All 3 Replies

Hey there! What are you trying to do with your program? We can only help you if we know what you want to do.

I wanna dispaly all the values after selecting the values from list box.

Member Avatar for Member #120589

This is extremely difficult to follow for a number of reasons:

1) No code indenting - this makes it almost impossible to follow
2) Mixing html and php - keep them separate

Also you're using deprecated mysql - please switch to mysqli or PDO. You may get more responses if you make it easier for contributors to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.