I jut created a dynamic dropdown list that are dependant on each other on Comugg.com. Heres the code.
function dropdownmenu() {
include_once "connection.php";
?>
<div class="make">
<label>Make</label>
<select name="make" id="makelist" onchange="getId(this.value);">
<option value="">Select Make</option>
<?php
$query = "select distinct(Make) from websitemasterlist order by Make ASC";
$results = mysqli_query($conn, $query);
foreach($results as $info) {
?>
<option value="<?php echo $info[Make]; ?>"><?php echo $info[Make]; ?></option>
<?php
}
?>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" onchange="getId2(this.value);">
<option value="">Select Model</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" onchange="getId3(this.value);">
<option value="">Select Year</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
<script>
function getId(val){
jQuery.ajax({
method: "POST",
url: "getdata.php",
data: "make="+val,
success:function(data){
jQuery("#modellist").html(data);
}
});
}
function getId2(val){
jQuery.ajax({
method: "POST",
url: "getdata.php",
data: "model="+val,
success:function(data){
jQuery("#yearlist").html(data);
}
});
}
function dropdownbutton(val){
}
</script>
<?php
}
and heres my getdata.php
<?php
include_once "connection.php";
$make = $_POST["make"];
$model= $_POST["model"];
if (!empty($_POST["make"])) {
$make = $_POST["make"];
echo $query = "SELECT Distinct Model FROM websitemasterlist where Make='".$make."'";
$results = mysqli_query($conn, $query);
?>
<option value="">Select Model</option>
<?php
foreach ($results as $info){
?>
<option value="<?php echo $info["Model"]; ?>"><?php echo $info["Model"]; ?></option>
<?php
}
}
if (!empty($_POST["model"])) {
$model = $_POST["model"];
echo $model;
echo $query = "SELECT Distinct Year FROM websitemasterlist where Model='".$model."'";
$results = mysqli_query($conn, $query);
?>
<option value="">Select Year</option>
<?php
foreach ($results as $info){
?>
<option value="<?php echo $info["Year"]; ?>"><?php echo $info["Year"]; ?></option>
<?php
}
}
?>
Im trying to figure out how to go about creating a dynamic link. For example i wanted to do something like this but i am unsure of how to get the selected values from each field.
<a href="http://comugg.com/category/<?php echo $year; ?>/<?php echo $make; ?>/<?php echo $model; ?>>text</a>
How should i go about this?
Related
Why can't I get the value of the element? sorry I'm new at coding and trying to learn
<script>
function getdll() {
document.getElementById('lblmess').innerHTML =
(formid.listid[formid.listid.selectedIndex].value)
$element = document.getElementById("lblmess");
console.log($element.innerHTML)
}
</script>
<form name="formid">
<select class="custom-select" name="listid" onchange="getdll()">
<option value="0"></option>
<?php
$hall_qry = $conn->query("SELECT * FROM `office_hall` WHERE o_id !=0 order by `office_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['o_id'] ?>"><?php echo $row['office_name'] ?></option>
<?php
endwhile;
?>
</select>
<br><br>
<select class="custom-select">
<?php
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['room_name'] ?></option>
<?php
endwhile;
?>
</select>
<label id="lblmess"></label>
</form>
this is where I use this code sorry if my coding id is like that .............................................................
Try:
onchange="getdll(this.value)"
THEN in your script, put a parameter
function getdll(val){
alert(val);
}
Should be able to get the value you selected
I am currently making a system for a project. We need to create a grade calculator for the system.
I was stuck because I can only calculate the first set of data in the HTML table.
Grade Table
How can I change the other values? The data that is in the table is generated by PHP and that came from our database. I am just new to PHP, especially to ajax.
Here is the webpage:
?>
<select name="midterm" id="midterm" required class="form-control" onchange="DisableMenu()">
<?php
if($dblMidterm!="0.00"){
?>
<option value=" <?php echo $dblMidterm; ?> " selected> <?php echo $dblMidterm; ?> </option>
<?php
}
else{
?>
<option value="0.00">0.00</option>
<?php
}
?>
<option value="1.00">1.00</option>
<option value="1.25">1.25</option>
<option value="1.50">1.50</option>
<option value="1.75">1.75</option>
<option value="2.00">2.00</option>
<option value="2.25">2.25</option>
<option value="2.50">2.50</option>
<option value="2.75">2.75</option>
<option value="3.00">3.00</option>
<option value="5.00">5.00</option>
</select>
<?php
echo '</td>';
echo '<td>';
?>
<select name="finals" id="finals" required class="form-control">
<option value=" <?php echo $dblFinals; ?> "> <?php echo $dblFinals; ?> </option>
<option value="1.00">1.00</option>
<option value="1.25">1.25</option>
<option value="1.50">1.50</option>
<option value="1.75">1.75</option>
<option value="2.00">2.00</option>
<option value="2.25">2.25</option>
<option value="2.50">2.50</option>
<option value="2.75">2.75</option>
<option value="3.00">3.00</option>
<option value="5.00">5.00</option>
</select>
<?php
echo '</td>';
echo '<td>';
?>
<select name="overall" id="overall" disabled required class="form-control">
<?php
if($dblOverall!="0.00"){
?>
<option value=" <?php echo $dblOverall; ?> "> <?php echo $dblOverall; ?> </option>
<?php
}
else{
?>
<option value=" "></option>
<?php
}
?>
</select>
<?php
Here is the PHP part:
<?php
require_once('server.php');
?>
<?php
if(isset($_POST['dblmidterm'])&&isset($_POST['dblfinals'])){
$dblMidterm = mysqli_real_escape_string($objConn, $_POST['dblmidterm']);
$dblFinals = mysqli_real_escape_string($objConn, $_POST['dblfinals']);
$dblOverall = ($dblMidterm + $dblFinals)/2;
$dblOverall = number_format((float)$dblOverall, 2, '.', '');
echo '<option value="' . $dblOverall . '">' .$dblOverall. '</option>';
$dblMidterm = 0;
$dblFinals = 0;
}
?>
Here is the AJAX part:
$(document).ready(function() {
$("#midterm, #finals").change(function() {
var midterm = $("#midterm").val();
var finals = $("#finals").val();
if(midterm != "" && finals!= "") {
$.ajax({
url:"compute.php",
data:{dblmidterm:midterm,dblfinals:finals},
type:'POST',
success:function(response) {
var resp = $.trim(response);
console.log(midterm);
console.log(finals);
console.log(resp);
$("#overall").html(resp);
}
});
} else {
$("#overall").html("<option value=''> </option>");
}
});
});
The problem is you don't select specific selects, so JavaSript finds the first one.
You need to do some reading on this keyword.
var row = $(this).closest('tr'); //finds the row the select you used is in
var midterm = row.find('#midterm').val(); //finds the #midterm in that row
var finals = row.find('#finals').val(); //finds the #finals that row
I have a problem when submit form value from the second select don't show. Can someone explain me what I'm doing wrong because in new in this (maybe is because i use onchange event). Thanks in advance.
index.php
<?php
include("includes/conn.php");
$sql = "SELECT * FROM opstina";
$result = $conn->query($sql);
if($result->num_rows > 0){
$opstineArr = array();
while($row = $result->fetch_assoc()){
$opstineArr[] = $row;
}
}
if(isset($_POST["addNesto"])){
$opstine = $_POST["opstine"];
$ime = $_POST["ime"];
if(isset($opstine)){
if(empty($opstine)){
$error["opstine"] = "izaberite opstinu";
}
}
$naselja = $_POST["naselja"];
if(isset($naselja)){
if(empty($naselja)){
$error["naselja"] = "izaberite naselje";
}
}
if(isset($ime)){
if(empty($ime)){
$error["ime"] = "izaberite ime";
}
}
if(count($error) == 0){
echo 2222;
}
}
include("includes/header.php");
?>
<form action="" method="POST">
<select name="opstine" class="form-control" id="select_id" onchange="getNaselja(this.value)">
<option>..Izaberite Opstinu..</option>
<?php foreach($opstineArr as $key => $opstina): ?>
<option value="<?php echo $opstina['OpstinaID']; ?>" <?php if(isset($opstine) && $opstine==$opstina['OpstinaID']) echo "selected"; ?> ><?php echo $opstina['NazivOpstine']; ?></option>
<?php endforeach; ?>
</select>
<?php if(isset($error["opstine"])) echo $error["opstine"]; ?>
<p>
<select name='naselja' id='naseljeLista' class='form-control' style='margin-top:10px;'>
</select>
<span><?php if(isset($error["naselja"])) echo $error["naselja"]; ?></span>
</p><br>
<input type="text" name="ime" value="<?php if(isset($ime)) echo $ime; ?>" class="form-control col-md-7 col-xs-12"><?php if(isset($error["ime"])) echo $error["ime"]; ?><br><br>
<input type="submit" name="addNesto" class="btn btn-sm btn-success">
</form>
<?php include("includes/footer.php"); ?>
getNaselja.php
global $conn;
include("includes/conn.php");
if(!empty($_POST["opstina_id"])){
$opstinaID = $_POST["opstina_id"];
$sql1 = "SELECT * FROM naselje WHERE OpstinaID=".$opstinaID;
$result1 = $conn->query($sql1);
if($result1->num_rows > 0){
$naseljaArr = array();
while ($one = $result1->fetch_assoc()) {
$naseljaArr[] = $one;
}
}
echo "<option value=''>---Izaberite naselje---</option>";
foreach ($naseljaArr as $key => $value) {
if(isset($_POST["naselja"]) && $value["NaseljeID"]==$_POST["naselja"]){
echo "<option value='".$value['NaseljeID']."' selected='selected' style='display:block;'>".$value['NazivNaselja']."</option>";
}else{
echo "<option value='".$value['NaseljeID']."'>".$value['NazivNaselja']."</option>";
}
}
}
footer.php
<script>
function getNaselja(val){
$.ajax({
type: 'POST',
url: 'getNaselja.php',
data: 'opstina_id='+val,
success: function(data){
$('#naseljeLista').html(data);
console.log(data);
}
});
}
</script>
example of form after submit on index.php
When I choose from first select my data on the console looks like:
<option value=''>---Izaberite naselje---</option>
<option value='4610'>Beška</option>
<option value='4611'>Inđija</option>
<option value='4612'>Jarkovci</option>
<option value='4613'>Krčedin</option>
<option value='4614'>Ljukovo</option>
<option value='4615'>Maradik</option>
<option value='4616'>Novi Karlovci</option>
<option value='4617'>Novi Slankamen</option>
<option value='4618'>Slankamenački Vinogradi</option>
<option value='4619'>Stari Slankamen</option>
<option value='4620'>Čortanovci</option>
Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>
i have db of countries from here http://www.webmasterworld.com/html/3018309.htm
with 239 countries, every country have value. After i have selected another country in option, how can i update zone list with zones from new value?
$country_query = mysql_query("SELECT * FROM country WHERE status = '1' ORDER BY name ASC");
$zone_query = mysql_query("SELECT * FROM zone WHERE country_id = '" . (int)$country_id . "' AND status = '1'");
<select name="country_id">
<option value=""><?php echo '$text_select;' ?></option>
<?php while ($country=mysql_fetch_array($country_query)) { ?>
<?php if ($country['country_id'] == $country_id) { ?>
<option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
First time by default counties list will be loaded..
to load zones use this
<select name="country" id='country'>
<option value=""><?php echo '$text_select;' ?></option>
<?php while ($country=mysql_fetch_array($country_query)) { ?>
<?php if ($country['country_id'] == $country_id) { ?>
<option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
Here you need to call ajax when country changed
JQuery :
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('#country').change(function(){
$.post('ajax/getZones',
{Country : $('#country').val()},
function(response){
//prepare the zones html code
//write that code to zone dropdown
}, 'json');
});
});
</script>
Here 'ajax/getZones' refers getZones is the method in ajax controller
write the getZones query in getZones(ajax)
get the Country id with $_POST['Country'] in ajax controller
try implement ajax controller everything work fine..