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.
I am trying to populate Material Description from the Material Number. Bot hte values are stored in same SQL tasble. So what I want when I select Material Maiterial Description shpul auto populate.
Fileds in table are Material & MaterialDescripotion
Below is code in in main file where data is fetched
<?php
include_once "dbConnect.php";
$sql = "SELECT * FROM DRLINK";
$result2 = mysqli_query($conn, $sql);
if (!$result2) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";
}
?>
<html>
<!DOCTYPE html>
<head>
<title>Dropdown Ajax</title>
</head>
<body>
<form action ="DSSTRsubmit.php" method="POST">
<table border="1">
<tr>
<td>Select Retailer</td>
</tr>
<tr>
<td>
<?php
echo "<select>";
echo $options;
echo "</select>";
?>
</td>
</tr>
</table>
<br/>
<br/>
<br/>
<table border="1">
<tr>
<td>Material</td>
<td>Material Description</td>
<td>Quantity</td>
<td>Unit of Measure</td>
</tr>
<tr>
<td>
<div class="Material">
<select name="Material" onchange="getId(this.value);">
<option value="">Select Country</option>
<?php
$query ="SELECT * FROM MATERIALLIST";
$results = mysqli_query($conn, $query);
foreach($results as $MATERIALLIST) {
?>
<option value="<?php echo $MATERIALLIST["Material"];?>"><?php echo $MATERIALLIST["Material"];?></option>
<?php
}
?>
</select>
</div>
</td>
<td>
<div class="MaterialDescription">
<select name="MaterialDescription" id="DesList">
<option value=""></option>
</select>
</div>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
function getId(val){
$.ajax({
type:"POST",
url:"getdata1.php",
data:"Material="+val,
success: function(data){
$("#DesList").html(data);
}
});
}
</script>
</td>
<td><input type="text" name="dquantity_name" /> </td>
<td><input type="text" name="duom_name" /> </td>
</tr>
</table>
<legend> </legend>
<p> <button type="submit" class="pure-button pure-button-primary">Send Stock</button>
<br>
<br>
<?php
echo "Distributor Page";
?>
</body>
</html>
below is the getdata1.php
<?php
include_once "dbConnect.php";
if(!empty($_POST["Material"])){
$Material= $_POST["Material"];
$query = "SELECT * FROM MATERIALLIST WHERE Material = $Material";
$results = mysqli_query($conn,$query);
foreach($results as $MaterialDescription){
?>
<option value="<?php echo $Des["Material"];?>"><?php echo $materialDescription ["MaterialDescription"];?></option>
<?php
}
}
?>
Iam able to select the material but on selection of material no material description auto populates.
Thanks for the help
PHP is case sensitive. Be careful about naming variables.
foreach contains upper case variable, while echo - lowercase. Also in echo you have space after variable and before opening bracket.
$MaterialDescription
$materialDescription_["MaterialDescription"]
I have a list proxy.Once I select and save it I want to display the selected data. How can I do this?.I want to display it in selected list and once I reload or get back to that page `
<?php
include_once("../noaccess.php");
include_once(CLASS_PATH."fetch_service.php");
$objfetch = new fetchService();
include_once(CLASS_PATH."proxy.php");
$objproxy = new Proxy();
include_once(CLASS_PATH."log.php");
$objlog= new changelog();
$account_id=$_SESSION['account_id'];
global $mysqli;
if($_POST['submit']=="Save")
{
$pname=$_POST['proxy'];
$query="UPDATE `proxy` SET `proxy_default`='1' where `proxyname`='$pname'";
$mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
setcookie("msg","Proxy Seleted",time()+5,"/");
header("location:".SITE_URL."index.php?view=default_proxy");
}
?>
<form name="frmcr" id="frmcr" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" id="mode" value="insert" />
<table align="left" id="tblworking_hours" class="tbl_altcolor shadow" style="width:25%;margin-left:30px">
<thead>
<tr>
<th align="center"><b>Proxy</b></th>
<td><select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<!-- <option value="<?php echo $i ;?>"<?php echo $i==$ring21 ? "selected":"";?> ><?php echo $i;?></option> -->
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']== $resrproxy['proxyname'] ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="Save" class="btn" style="margin-left:35px;"/></td>
</tr>
</thead>
</table>
</div>
You've to compare $resrproxy['proxyname'] value with $pname. Your code should be like this:
// your code
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy){
$option = "<option value=\"{$resrproxy['proxyname']}\"";
if(isset($pname)){
if($pname == $resrproxy['proxyname']){
$option .= " selected=\"selected\"";
}
}
$option .= ">{$resrproxy['proxyname']}</option>";
echo $option;
}
?>
</select>
// your code `
<td>
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' and `proxy_default`='1'");
$pname=$result[0]['proxyname'];
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']==$pname ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select>
</td>
I have this problem of saving the option value into the database. Everything seems to look correctly but when submitted, the value in the database does not change. The form is in a table which retrieve user's submission and consent_id was use to update the option the admin. Hope anyone can spot what I am missing or what it should be written. Thanks.
This is my code:
manageLeave.php
<script type="text/javascript">
$(document).ready(function () {
$("#confirmForm").click(function () {
$("#statusForm").submit();
});
});
</script>
<body>
...
<tr>
<td><?php echo $staffName; ?></td>
<td><?php echo $convertDateFrom; ?></td>
<td><?php echo $convertDateTo; ?></td>
<td><?php echo $reason; ?></td>
<td>
<form method="post" action="doManageLeave.php" id="statusForm" name="statusForm">
<input type="hidden" name="consentId" value="<?php echo $consentId; ?>">
<select name="leaveStatus" data-native-menu="false">
<option value="" disabled selected style="display: none">Please select one...</option>
<option value="1">Approved</option>
<option value="2">Rejected</option>
</select>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
}
?>
<input type="button" id="confirmForm" value="Submit" />
</body>
doManageLeave.php
$getConsentId = $_POST['consentId'];
$leaveStatus = $_POST['leaveStatus'];
$editQuery = "UPDATE consent SET approval_id = '$leaveStatus' WHERE consent_id = $getConsentId";
$successInsert = mysqli_query($link, $editQuery) or die ("Error Query ". mysqli_error($link));
mysqli_close($link);
I have 2 dropdowns on my HTML page : The first drop down contains the database column names based on which the the second dropdown would be populated i.e.
I have a single table with fields: <Student Name, Degree, City> and following would be the entries;
1. "A", "BS", "New York"
2. "B", "BS", "Chicago"
3. "C", "MS", "Boston"
4. "D", "MS", "New York"
So my first dropdown would contain the column names i.e. "Degree" and "City".
If I select "Degree", the 2nd dropdown should populate "BS" and "MS" and if I select "City", the 2nd dropdown should select "New York", "Boston" and "Chicago".
How can I go about with the implementation?
[Adding my code]:
the changeSecond(first) method remains exactly the same as you suggested
<body>
<form method="POST" action="" name="mainForm">
<table>
<tr>
<td> Filter by: </td>
<td>
<div id="first">
<select onChange="changeSecond(this.value)">
<option value="1">All</option>
<option value="2">Degree</option>
<option value="3">City</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td>
<div id="second">
<select name="val">
<option value=""></option>
</select>
</div>
</td>
</tr>
</table>
</form>
</body>
And this is the second_script.php as you suggested:
<?
$link = mysql_connect("localhost", "root", "");
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_select_db("myDatabase", $link))
{
$first=mysql_real_escape_string($_REQUEST["first"]);
$query="SELECT ".$first." FROM myTable GROUP BY ".$first;
$data=mysql_query($query);
echo "<select id=\"second\">";
while($row=mysql_fetch_row($data))
{
echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select>";
}
echo mysql_error();
?>
code for the main file in which you want to populate drop down based on other
in below example i show localities in child dropdown for that city that is selected in parent drop down
<script type="text/javascript">
function city_locality(val)
{
// alert (val);
url="<?php echo $this->baseurl ?>/components/com_ezportal/includes/query.php";
data="stid="+val;
$.post(url,data,function(data){
$("#locid").html(data);
});
}
</script>
<select name="filter_1state" id="filter_1state" onChange="city_locality(this.value)">
<option value="0">-- Select City --</option>
<option value="1">Lahore</option>
<option value="2">Karachi</option>
<option value="3">Islamabad</option>
<option value="4">Quetta</option>
<option value="5">Multan</option>
</select>
code for the file query.php
<?php
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('mydatabase');
if (isset($_POST['stid']))
{
$stid = $_POST['stid'];
$query= mysql_query("SELECT id,ezcity FROM tbl_locality WHERE stateid = '".$stid."' GROUP BY ezcity");
?>
<option value="0">-- Select Locality --</option>
<?php
while($row = mysql_fetch_array($query))
{
?>
<option value="<?php echo $row['id']?>"><?php echo $row['ezcity']?> </option>
<?php
}
}
?>
If you want a more dynamic solution (that will accommodate changes to the background DB) you can do something like this on your page:
<script>
function changeSecond(first){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var res=xmlhttp.responseText;
document.getElementById("second").innerHTML=res;
}
}
xmlhttp.open("GET","second_script.php?first="+first,true);
xmlhttp.send();
}
</script>
...
<select onChange="changeSecond(this.value)">
<option value="Degree">Degree</option>
<option value="City">City</option>
</select>
<div id="second"><select><option value=""></option></select></div>
and then a script similar to:
<?php
//database connection
$first=mysql_real_escape_string($_REQUEST["first"]);
$query="SELECT ".$first." FROM tablename GROUP BY ".$first;
$data=mysql_query($query);
echo "<select>";
while($row=mysql_fetch_row($data)){
echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select>";
?>
I guess for real flexibility you'd also want to dynamically populate that first one using mysql_field_name in another script similar to above
You could either have all the dropdown needed preloaded and hidden, and show them when the 'change' event is triggered in the first dropdown, or have two dropdowns and empty it on that same 'change' event.
If you choose the second approach you should buffer the data to insert in the dropdown list, partially or totally
You can use ajax for this purpose
eg1.html
<html>
<head><title></title>
</head>
<body>
<form method="POST" action="" name="mainForm">
<table>
<tr>
<td> Filter by: </td>
<td>
<div id="first">
<select id="first_dropdown">
<option value="All">All</option>
<option value="Degree">Degree</option>
<option value="City">City</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td>
<div id="second">
<select id="second_dropdown">
</select>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
<script src = "https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#first_dropdown').change(function(){
$.ajax({
'type' : 'post',
'url' : 'getDropdownOptions.php',
'data': 'first=' + $(this).val(),
'success' : function(data) {
$('#second_dropdown').html(data);
}
});
});
});
</script>
getDropdownOptions.php
<?php
$link = mysql_connect("localhost", "root", "");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (mysql_select_db("myDatabase", $link)) {
$first = mysql_real_escape_string($_REQUEST["first"]);
$query = "SELECT " . $first . " FROM myTable GROUP BY " . $first;
$data = mysql_query($query);
$rtn_data = '';
while ($row = mysql_fetch_row($data)) {
$rtn_data .= "<option value=\"" . $row[0] . "\">" . $row[0] . " </option>";
}
echo $rtn_data;
exit;
}
echo mysql_error();
?>
Divyesh here, your answer is
==============================
edit.php
==============================
<!-- edit.php -->
<?php
include("config.php");
$id=$_REQUEST['id'];
echo $id;
$query=mysqli_query($con,"SELECT * FROM register r
INNER JOIN country c ON r.cuid = c.cuid
INNER JOIN state s ON r.sid = s.sid
INNER JOIN city ct ON r.cid = ct.cid where id='$id'");
while($r=mysqli_fetch_array($query))
{
$fn=$r['firstname'];
$add=$r['address'];
$gn=$r['gender'];
$hobby=$r['hobby'];
$h=explode(',',$hobby);
$q=array('reading','traveling','cricket','drawing');
$country=$r['cuid'];
$state=$r['sid'];
$city=$r['cid'];
echo $gn;
$edu= $r['education'];
$email=$r['email'];
$pass=$r['password'];
$conpass=$r['conpassword'];
$phno=$r['phoneno'];
}
?>
<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'cuid='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html(html);
}
});
}else{
$('#state').html(html);
$('#city').html(html);
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'sid='+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html(html);
}
});
});
</script>
</head>
<body>
<form method="post" action="update.php">
<table border="1" align="center">
<caption>Edit user data</caption>
<tr>
<td>First name</td>
<td><input type="text" name="fn" value="<?php echo $fn;?>"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="add" value="<?php echo $add;?>"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gn" value="male" <?php echo ($gn=='male')?'checked':'' ; ?> size="17">Male
<input type="radio" name="gn" value="female" <?php echo ($gn=='female')?'checked':'' ; ?> size="17">Female
</td>
</tr>
<tr>
<td>Hobby</td>
<td><input type="checkbox" name="hobby[]" value="reading" <?php if(in_array('reading',$h)){echo ("checked:'checked'");}?> >reading
<input type="checkbox" name="hobby[]" value="traveling" <?php if(in_array('traveling',$h)){echo ("checked:'checked'");}?> >traveling
<input type="checkbox" name="hobby[]" value="cricket" <?php if(in_array('cricket',$h)){echo ("checked:'checked'");}?> >cricket
<input type="checkbox" name="hobby[]" value="drawing" <?php if(in_array('drawing',$h)){echo ("checked:'checked'");}?> >drawing</td>
</tr>
<?php
$query = mysqli_query($con,"SELECT * FROM country");
//Count total number of rows
$rowCount = mysqli_num_rows($query);
?>
<td>Country</td>
<td><select name="country" id="country">
<option value="<?php echo $country;?>"><?php echo $country;?></option>
<?php
if($rowCount > 0)
{
while($row = mysqli_fetch_array($query))
{
echo '<option value="'.$row['cuid'].'">'.$row['country'].'</option>';
}
}
else
{
echo '<option value="">Country not available</option>';
}
?>
</select>
</td></tr>
<tr>
<td>State</td>
<td>
<select name="state" id="state">
<option value="<?php echo $state;?>"><?php echo $state;?></option>
</select>
</td></tr>
<tr>
<td>City</td>
<td>
<select name="city" id="city">
<option value="<?php echo $city;?>"><?php echo $city;?></option>
</select>
</td>
</tr>
<tr>
<td>Education</td>
<td><input type="text" name="edu" value="<?php echo $edu;?>"></td>
</tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email;?>"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="pass" value="<?php echo $pass;?>"></td>
</tr>
<tr>
<td>Confirm password</td>
<td><input type="text" name="conpass" value="<?php echo $conpass;?>"></td>
</tr>
<tr>
<td>Phone no</td>
<td><input type="text" name="phno" value="<?php echo $phno;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php echo $id;?>">
<input type="submit" name="update" value="update"></td>
</tr>
</table>
</form>
</body>
</html>
================
<h3>ajaxData.php</h3>
================
<!--ajaxData.php-->
<?php
//Include database configuration file
include("config.php");
if(isset($_POST["cuid"]) && !empty($_POST["cuid"]))
{
//Get all state data
$query = mysqli_query($con,"SELECT * FROM state WHERE cuid = ".$_POST['cuid']."");
//Count total number of rows
$rowCount = mysqli_num_rows($query);
//Display states list
if($rowCount > 0)
{
echo '<option value="">Select state</option>';
while($row = mysqli_fetch_array($query))
{
echo '<option value="'.$row['sid'].'">'.$row['state'].'</option>';
}
}
else
{
echo '<option value="">State not available</option>';
}
}
if(isset($_POST["sid"]) && !empty($_POST["sid"]))
{
//Get all city data
$query = mysqli_query($con,"SELECT * FROM city WHERE sid =".$_POST['sid']."");
//Count total number of rows
$rowCount = mysqli_num_rows($query);
//Display cities list
if($rowCount > 0)
{
echo '<option value="">Select city</option>';
while($row = mysqli_fetch_array($query))
{
echo '<option value="'.$row['cid'].'">'.$row['city'].'</option>';
}
}
else
{
echo '<option value="">City not available</option>';
}
}
?>