Making a select option tag retrieve data from database and display data from another table

Hey I have a restaurant and a food table, food table(F_ID) has as a foreign key the restorant id (R_ID). I have used this code, the selected option tag is generated with the names of the restaurants but when I click on one restaurant I get this error: Notice : Undefined index: restorant in C:\xampp\htdocs\robinfood\filter.php on line 41

<?php
include "connection.php";
$conn = Connect();
?>

<!DOCTYPE html>
<html>
<head>
	<title>Kategorizimi</title>
</head>
<body>

<form method="post" action="filter.php">
<?php
$sql3 = "SELECT * FROM restaurants";
$result=mysqli_query($conn,$sql3) or die("nuk ka restorant");

$opt = "<select id='restorant' name='restorant'>";

while($row = mysqli_fetch_assoc($result)){
	//$opt .= "<option value='$row[R_ID]'>$row[name]</option>"; 
	$id=$row['R_ID'];
	$name=$row['name'];
	$opt .= '<option value="'.$id .'">' .$name . '</option>';
}


$opt .="</select>";
?>
<input type="submit" name="submit" value="Kliko"/>
</form>

 	<?php echo $opt; ?> 


<?php
//grumbullimi i te dhenave 
if(isset($_POST['submit'])){
	
	$select_term = $_POST['restorant'];
	echo $select_term;
	$sql = "SELECT * FROM food WHERE F_ID = '$select_term'";
	$result= mysqli_query($conn,$sql) or die ("nuk ka ushqim");

	$count=mysqli_num_rows($result);
	if($count==0){
		$output = "There was no search resutlts!";
	}else{
		while($row = mysqli_fetch_assoc($result)){
				$name = $row['name'];
				$description=$row['description'];
				$f_id = $row['F_ID'];

				$output .= "<div> ".$name."</div>";
			}
			echo $output;
		}
	}

?>

</body>
</html>

Hi there @kristindoni1. Welcome to the forums.

[off-topic]
When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.
[/off-topic]

Edit: I don’t think we need the images either :slight_smile:

okay thank you for that :slight_smile:

1 Like

Here

<input type="submit" name="submit" value="Kliko"/>
</form>

 	<?php echo $opt; ?> 

your <select> tag is after the form is closed, so it’s not sent to your processing script. Move it to before you close the form.

1 Like

I think i fixed it :smiley: thank you, even though the foods are still not appearing