I'm trying to populate a text field and a text area depending on the choice of the second dropdown list using an SQL table. Somehow I got it all wrong. Here's my code right now:
AJAX/HTML
<script>
function getPackage(val) {
$.ajax({
type: "POST",
url: "get_package.php",
data:'serviceid='+val,
success: function(data){
$("#package-list").html(data);
}
});
}
function getPackageDesc(val) {
$.ajax({
type: "POST",
url: "get_packagedesc.php",
data:'packageid='+val,
success: function(data){
$("#price").html(data);
}
});
}
function selectService(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
I feel like there is already something wrong with function getPackageDesc. I don't know if it's in this function or in the php file.
MAIN HTML
<div class="frmDronpDown">
<div class="row">
<label>Service:</label>
<br/>
<select name="service" id="service-list" class="demoInputBox" onChange="getPackage(this.value);">
<option value="">Select Service</option>
<?php
foreach($results as $service) {
?>
<option value="<?php echo $service[" serviceid "]; ?>">
<?php echo $service["servicename"]; ?>
</option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Package:</label>
<br/>
<select name="package" id="package-list" class="demoInputBox" onChange="getPackageDesc(this.value);">
<option value="">Select Package</option>
</select>
</div>
<div class="row">
<label>Package Information:</label>
<br/>
<br/> Price:
<br>
<input type="text" name="price" id="price">
<br>
<br> Package Description:
<br>
<textarea name="packagedesc" form="usrform" id="packagedesc"></textarea>
</div>
</div>
get_package.php
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["serviceid"])) {
$query ="SELECT * FROM tbl_packages WHERE serviceid = '".$_POST["serviceid"]."'";
$results = $db_handle->runQuery($query);
?> < option value = "" > Select Package < /option>
<?php
foreach($results as $package) {
?> < option value = "<?php echo $package["
packageid "]; ?>" > <?php echo $package["packagename"]; ?> < /option>
<?php
}
}
?>
This part of the code works. I have populated the dropdowns using two SQL tables, tbl_services and tbl_packages. The next php file is where I'm having trouble.
get_packagedesc.php
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["packageid"])) {
$query ="SELECT * FROM tbl_packages WHERE packageid = '".$_POST["packageid"]."'";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_array($result)){
?>
<?php echo $price['price'] ; ?></br>
<?php echo $packagedesc['packagedesc']; ?> </br>
<?php
}
}
?>
This code is where I'm having trouble at. I don't know if it is okay to get data from the same SQL table (tbl_packages). Or maybe I messed up with the $result.
What is wrong with my code and what could be the solution?
Thanks for any help.