I'm doing a car dealership website where I have to fetch cars from database and enable user search form by multiple criteria (brand, model, energy). Depending on the selection, on the same page the list of all the cars matching this criteria appears in the form of table, and clicking on one of the cars opens a new page where all the available information about the car are shown. The form method is POST, and I'm having trouble passing the id variable onto a new page where I could search the database based on that car id and list the information.
My JQUERY code is as follows:
<script type="text/javascript">
$(document).ready(function(){
$('#car').on('change',function(){
var carID = $(this).val();
if(carID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'marque_id='+carID,
success:function(html){
$('#test1').html(carID);
$('#model').removeAttr("disabled");
$('#model').html(html);
}
});
}else{
$('#model').attr("disabled");
$('#energy').attr("disabled");
}
});
$('#model').on('change',function(){
var modelID = $(this).val();
if(modelID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'modele_id='+modelID,
success:function(html){
$('#test').html(modelID);
$('#energy').removeAttr("disabled");
$('#energy').html(html);
}
});
}else{
$('#energy').attr("disabled");
}
});
$('tr[data-href]').on("click", function() {
document.location = $(this).data('href');
});
});
</script>
Form search code:
<form method="post" name="form">
<div class="select-boxes">
<?php
//Include database configuration file
include('dbConfig.php');
//Get all car data
$query = $db->query("SELECT marque_name,marque_id FROM vehicule_marque order by marque_name");
//Count total number of rows
$rowCount = $query->num_rows;
$car = isset($_POST['car']) ? $_POST['car'] : '';
$model = isset($_POST['model']) ? $_POST['model'] : '';
$energy = isset($_POST['energy']) ? $_POST['energy'] : '';
?>
<div>Select car</div>
<select name="car" id="car" >
<option value="">Select Car</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['marque_id'].'">'.$row['marque_name'].'</option>';
}
}else{
echo '<option value="">Car not available</option>';
}
?>
</select>
<div id="test1"></div>
<div>Select car model</div>
<select name="model" id="model" disabled>
<option value=""><!--Select car first--></option>
</select>
<div id="test"></div>
<div>Select energy</div>
<select name="energy" id="energy" disabled>
<option value=""><!--Select model first--></option>
</select>
<input type="submit" name="submit" value="submit">
</div>
</form>
<form id="form1" name="form1" method="post">
<div class="well">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Picture</th>
<th>Marque</th>
<th>Model</th>
<th>Energy</th>
<th>KM</th>
<th>Price</th>
<th style="width: 26px;"></th>
</tr>
</thead>
<tbody>
<?php
$query2 = $db->query("SELECT reference_id, marque_name, modele_name, energie_name, vehicule_kilometrage, vehicule_price_ttc FROM vehicule AS a INNER JOIN vehicule_marque AS b INNER JOIN vehicule_modele AS c INNER JOIN vehicule_energie AS d WHERE a.marque_id = '".$car."' AND a.modele_id = '".$model."' AND a.energie_id = '".$energy."' AND b.marque_id = a.marque_id AND c.modele_id = a.modele_id AND d.energie_id = a.energie_id");
$rowCount2 = $query2->num_rows;
if($rowCount2>0){
while($myrow = $query2->fetch_assoc()){
?>
<tr data-href="results.php?id=">
<td><?php echo $myrow["reference_id"]; ?></td>
<td><img src="<?php echo 'images/' ?>"</td>
<td><?php echo $myrow["marque_name"]; ?></td>
<td><?php echo $myrow["modele_name"]; ?></td>
<td><?php echo $myrow["energie_name"]; ?></td>
<td><?php echo $myrow["vehicule_kilometrage"]; ?></td>
<td><?php echo $myrow["vehicule_price_ttc"]; ?> €</td>
</tr>
<?php
}
}else {
echo 'Error';
}
?>
</tbody>
</table>
</div>
</form>
Everything works correctly, the only problem I have is that I'm not sure how to get the $myrow["reference_id"]
and append it to results.php?id=
so I could show the information on that car on the new page using a new query.
Have you tried this?
<tr data-href="results.php?id=<?php echo $myrow["reference_id"]?>">
Would probably be cleaner to create the whole link as a string beforehand, and then echoing that string.