PHP-How to update values in a combobox after clicking the insert button?

Asked

Viewed 167 times

0

I’m new to Law and I study alone... I need to make sure that after the user records a course by choosing the combobox and clicking Enroll, the recording is done in the Enroll table in the database. Okay, so far it’s working. The problem is that as the user is still logged in, the combobox does not give a refresh, that is, it can select the same course 2 X, and this cannot happen... PLEASE, how do I update the values of the combobox right after clicking the button Enroll and without it interfering with the insertion of the data in the table? Somebody help me!!!! thank you since already!!

<?php  //this is panel.php
session_start();
include('userauthentication.php');
include ('header.php');
include ('connection.php');
?>
<?php

$result_courses=$conn->query("SELECT * FROM courses");

$color1="whitesmoke";
$color2="teal";
$color= $color1;

?>

<html><head>
 <link rel="stylesheet" href="school.css"/>
<title>Course enrollment</title>

</head>
<body>
<div id="student">
<h3><p> Haven't you enroll in a course yet?</p><h3>        
<p>Choose your options:</p>
<form name="courses" method="post" action="enroll.php">
<select name="courses" > 
<?php
while($rows= $result_courses->fetch_assoc())
{
	$color == $color1 ? $color= $color2 : $color= $color1;
	
	$idcourse=$rows['idcourse'];
	$title= $rows['title'];
	
	echo "<option value='$idcourse' style='background:$color;'>$title</option>";
	
}

?>
<input  type="submit" name="submit" value="Enroll">
</form>
</div>

<div id="footer">
<?php  include ('footer.php');?>
   
 </div>
</body>
</html>

/////////////here enroll.php///////
<?php
session_start();
include('userauthentication.php');
include('connection.php');

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


    /*.. do the query ... */
 $idstudent=$_SESSION['idstudent'];
 $idcourse=$_POST['courses'];
	
	 $query= "INSERT INTO enroll VALUES(null,'".$idstudent."','".$idcourse."')";
	 
	 $result=mysqli_query($conn,$query);
      
      if(!$result)
	  {
		 echo "INSERT failed<br><br>";
	  
	 header('Location:panel.php');
	  }
	
}

?>

  • you can use a javascript function to do this : document.getElementsByName('enroll').addEventListener("submit", window.location.reload());

  • When it comes to handling without refresh on the page you can use AJAX with JS which will help you well.

  • Thank you very much!!!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.