Jump to content

Recommended Posts

Hi everyone, Im relatively new to PHP.

 

Im required to create a database to store all the courses taken by a student and his/her grades. i have created the database and able to store and display all the data stored in the database.

 

I have a problem here:

 

Im required to create a drop down menu for each row of data displayed in a table so that user can delete the data entry by simply clicking the "cancel" button from the drop down menu. I have created the code as shown below, the problem is I can only delete the first row of data and not able to delete the other rows.

 

Need helps and advices from experts!  ;D

 

<html>
<body style="background-color:#E0FFFF;">


<script language="javascript" >
<!-- hide
function submitRequest(val) {
document.forms[0].submit();
}

// end hide -->
</script>

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_course", $con);

$result = mysql_query("SELECT * FROM Courses ORDER BY Year, Sem, CourseCode");

echo "<table border='1' cellpadding='2' cellspacing='0'>
<tr>
<th>CourseCode</th>
<th>CourseName</th>
<th>Year</th>
<th>Sem</th>
<th>Grade</th>
<th>Option</th>
</tr>";

?>

<?php

while($row = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['CourseCode'] . "</td>";
  echo "<td>" . $row['CourseName'] . "</td>";
  echo "<td>" . $row['Year'] . "</td>";
  echo "<td>" . $row['Sem'] . "</td>";
  echo "<td>" . $row['Grade'] . "</td>";
  echo "<td>";

?>
<?php
?>

         <form name="form1" action="submitDelete.php" method="post">
  	 <select name="cancel" onchange="submitRequest(this.value);">
  	 <option value=> </option>
 <option value="<?php echo $row['CourseName'];?>">Cancel</option>
         </select>
         </form>

<?php
  echo "</td>";
  echo "</tr>";
  }

echo "</table>";

mysql_close($con);
?>

</body>
</html>

 

and the code for submitDelete.php is shown here:

 

<html>
<body style="background-color:#E0FFFF;">

<?php

header('Location: http://localhost/Database/viewcourses2.php');

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_course", $con);

mysql_query("DELETE FROM Courses WHERE CourseName='$_POST[cancel]'");

mysql_close($con);

?>]
</body>
</html>

 

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/241114-need-help-onchange/
Share on other sites

Take a look at this line:

document.forms[0].submit();

What that's doing is basically submitting the first form in the document, hence [0], the first index. The first form also belongs to the first record.

 

If all you want to do is submit the form then what you could do instead is change the OnChange to something like:

onchange="this.form.submit();"

Link to comment
https://forums.phpfreaks.com/topic/241114-need-help-onchange/#findComment-1238817
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.