Reputation: 171
I am developing a combo box whenever i select something in the contractor column( as per attach in the link) , the vehicle combo box will retrieve the info from database based on the contractor selection.
Problems faced:
Vehicle combo box can't display the data based on the contractor selection.
Illustration about the interface design:
I've attached in the first part of the code which is my HTML table code.
<td>
<select class="form-control" name='opt_contractor[]' id='opt_contractor' onChange="getState(this.value);" <?php if ($view==1) {?> disabled <?php }?> >
<option>-- Select One --</option>
<?php
foreach ($get_contractor as $contractor ){
?>
<option value='<?php echo $contractor->ref_id;?>'<?php if($contractor->ref_id == $reqtype){echo ' selected';}?>><?php echo $contractor->ref_desc;?>
</option>
<?php } ?>
</select>
</td>
<td>
<select class="form-control" name="opt_vehicle[]" id="opt_vehicle" >
<option value="">-- Select Vehicle --</option>
</select>
</td>
Script
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getState(countryId)
{
var strURL="refresh.php?country="+countryId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
document.getElementById('opt_vehicle').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
PHP CODE
$query ="SELECT ref_id, ref_desc FROM ref_mst WHERE ref_val5 = '" . $country . "'";
$result = $db->query($query);
?>
<select name="opt_vehicle" >
<option>Select State</option>
<?php while($row=mysqli_fetch_assoc($result)) { ?>
<option value=<?php echo $row['ref_id'] ?> ><?php echo $row['ref_desc'] ?>
</option>
<?php } ?>
</select>
For this part( refer php code), i've made several attempt to fix the problem occur, i've attached the problem in below as well.
1. <?php while($row=mysqli_fetch_assoc($result)) { ?>
2. <?php while($row=mysqli_fetch_array($result)) { ?>
3. <?php while($row=mysqli_fetch_object($result)) { ?>
Upvotes: 1
Views: 179
Reputation: 171
I've resolved my problem, instead of using
<?php while($row=mysqli_fetch_assoc($result)) { ?>
I used this method
$results = $db->get_results($query);
<?php
foreach($results as $vhc) {
?>
<option value='<?php echo $vhc->ref_id;?>'><?php echo $vhc->ref_desc; ?> </option>
I've attached in the modification of the codes i've made to resolved my issue.
if(!empty($contract)) {
$query = "SELECT ref_id, ref_desc FROM ref_mst WHERE ref_val5 = '" .$contract. "'";
$results = $db->get_results($query);
?>
<select name="opt_vehicle" >
<option value="">Select Vehicle ....</option>
<?php
foreach($results as $vhc) {
?>
<option value='<?php echo $vhc->ref_id;?>'><?php echo $vhc->ref_desc; ?> </option>
<?php
} ?>
<select>
<?php }
Thank you once again to Ajmal Praveen for your assistance.
Upvotes: 0
Reputation: 413
Just Viewed your PHP Code
I think there is a Issue in your PHP Code Kindly Replace it with my code below
<?php
//re updated the query code instead , now used the AND and markers
$query ="SELECT `ref_id` AND `ref_desc` FROM `ref_mst` WHERE `ref_val5` = '.$country.'";
$result = $db->query($query);
?>
<select name="opt_vehicle" >
<option>Select State</option>
<?php while($row=mysqli_fetch_assoc($result)) { ?>
<?php
foreach ($row as $key => $value) {
echo $value;
echo $tableRow[$key];
}
?>
<option value="<?php echo $tableRow['ref_id']; ?>"><?php echo $tableRow['ref_desc']; ?></option>
<?php } ?>
</select>
To Debug your Self i was Echo'ed there
echo $value;
There is Issues with the Quotation i Think.. So give a try and Update me.. Thank you..
and Kindly Try to use PDO Prepared Statements or Mysqli Prepared statements, in Your Normal Code Your are using Direct Queries to DB.
Upvotes: 0