954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help! (Onchange)

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

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

chaychie
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

try this code

<html>
    <body style="background-color:#E0FFFF;">
     
     
    
     
    <?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>";
    ?>
    <td><a href="submitDelete.php?code=<?php echo $row['CourseCode'];?>">Cancel</a></td> 
    <?php
    
    echo "</tr>";
    }
     
    echo "</table>";
     
    mysql_close($con);
    ?>
     
    </body>
    </html>


and submitDelete.php is

<html>
    <body style="background-color:#E0FFFF;">
     
    <?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 CourseCode='".$_GET['code']."'");
     
    mysql_close($con);
     header('Location: http://localhost/Database/viewcourses2.php');
    ?>
    </body>
    </html>
Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

Hi Karthik,

Is it possible to do it in a dropdown menu instead of href/link?

Thanks!

chaychie
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Yes its possible but what is the need of dropdown with only one option?

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

Hi,

Ya, actually it's only a part of my project. Later i will have to add in more options into the dropdown menu in order to have more functions such as delete, edit and update functions.

Thanks!

chaychie
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

You can do the edit and delete option with href too. This is simple and better than your idea.

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

Hmm.. It's a requirement for me to do it in a drop down menu instead of href. Hope that you can help! =)

chaychie
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

<?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 "CourseCode
CourseName
Year
Sem
Grade
Option
";

?>

<?php
$i = 0;
while($row = mysql_fetch_array($result))
{
echo "";
echo "" . $row['CourseCode'] . "";
echo "" . $row['CourseName'] . "";
echo "" . $row['Year'] . "";
echo "" . $row['Sem'] . "";
echo "" . $row['Grade'] . "";
echo "";

?>
<?php
?>


Cancel

<?php
echo "";
echo "";
$i++;
}

echo "";

mysql_close($con);
?>

reyborn
Junior Poster in Training
58 posts since Jan 2006
Reputation Points: 11
Solved Threads: 9
 

I have solved it! Thanks ya!

chaychie
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: