How to include a dependent-dropdown list in a form tag?

How to include a dependent-dropdown list in a form tag?

I have a working dependent-dropdown list that works smoothly when it's not placed inside a form tag. As soon as I enclose it in a form tag, it stops working. How can I include this code in a form tag?

I have a working dependent-dropdown list that works smoothly when it's not placed inside a form tag. As soon as I enclose it in a form tag, it stops working. How can I include this code in a form tag?

I have simply tried running the code without using any form tag. It works perfectly fine. To pass the values to the next page I used the form tag and that's when I was getting a blank response(the drop-down list wasn't loading).

index.php

<?php
require_once("dbcontroller.php");
$query ="SELECT * FROM flat";
$results = mysqli_query($conn,$query); 
?>
<html>
<head>
<title></title>

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script> <script> function floor(val) { $.ajax({ type: "POST", url: "get_info.php", data:'floor='+val, success: function(data){ $("#floor").html(data); } }); }

function flatno(val1) { $.ajax({ type: "POST", url: "get_info.php", data:'flatno='+val1, success: function(data){ $("#flatno").html(data); } }); }

</script> </head> <body>

<form method="post" action="a.php"><br>

Select Wing: <select name="wing" onChange="floor(this.value);"> <option value="">---------</option> <?php foreach($results as $row) { ?> <option value="<?php echo $row["wing"]; ?>"><?php echo $row["wing"]; ?>
</option> <?php } ?> </select>

<br><br> Select Floor: <select name="floor" id="floor" onChange="flatno(this.value);"> <option value="">---------</option> </select>

<br><br> Select Flat No: <select name="flatno" id="flatno"> <option value="">---------</option> </select>

<input type="submit">

</form>

</body> </html>

get_info.php

<?php
require_once("dbcontroller.php");

if(!empty($_POST["floor"])) { $query ="SELECT * FROM flat WHERE wing = '".$_POST["floor"]."'"; $results = mysqli_query($conn,$query); ?> <option value="">Select Floor</option> <?php foreach($results as $row) { ?> <option value="<?php echo $row["floor"]; ?>"><?php echo $row["floor"];
?></option> <?php } } ?>

<?php if(!empty($_POST["flatno"])) { $query ="SELECT * FROM flat WHERE floor = '".$_POST["flatno"]."'"; $results = mysqli_query($conn,$query); ?> <option value="">Select Flat no</option> <?php foreach($results as $row) { ?> <option value="<?php echo $row["flatno"]; ?>"><?php echo
$row["flatno"]; ?></option> <?php } } ?>

a.php

<?php

include_once 'dbcontroller.php';

$wing =mysqli_real_escape_string($conn,$_POST['wing']);
$floor =mysqli_real_escape_string($conn,$_POST['floor']);
$flatno=mysqli_real_escape_string($conn,$_POST['flatno']);

echo "<h2>Flat No : " . $flatno . "</h2>"; echo "<h2>Email : " . $floor . "</h2>"; echo "<h2>Email : " . $wing . "</h2>"; ?>

I expected all the 3 dropdown list to be functional, but only the 1st dropdown list loads and the other 2 are blank.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Upload file using JavaScript and PHP

In this tutorial, I show how you can upload a file using JavaScript AJAX and PHP.

How to Include PHP in HTML

Welcome to this article on how to use PHP in HTML! Here, we try to get specific about exactly how PHP and HTML interact, at the level of a specific .php file. In other words, how do you actually include PHP in HTML, and what rules can and can’t you follow in weaving PHP and HTML together?