I have a select that is echoing out a column (jobTitle) from the database, on change I'm trying to display in another html select, selecting another column (company_name), but only values related to the first html select.
I'm not getting an errors. The first html select value is being passed, but for some reason it's not selecting from the db based on that in the second select.
I'm thinking the issue is with my second sql query.
P.S if anyone knows of a more effective way of doing this i'd be most gratefully to find out.
PHP & HTML:
<select name="jobtitle_select" class="jobtitle_select">
<option class="" name="" value="" >-- Job title --</option>
<?php
$sql = $dbh->prepare("SELECT * FROM jobs_list");
if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $sql->fetch()) {
$jobTitle = $row['jobTitle'];
echo "<option class='' name='' value='$jobTitle' > $jobTitle </option>";
} // end of while // ?>
</select>
<?php
$jobtitle_select = $_POST['jobtitle_select'];
if ($jobtitle_select){
$sql = $dbh->prepare("SELECT * FROM jobs_list WHERE company_name = :jobtitle_select");
$sql->bindParam(':jobtitle_select', $jobtitle_select, PDO::PARAM_STR);
if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}
?>
<select class="company_name_select" >
<option class="" name="" value="" >-- Company name --</option>
<?php while($row = $sql->fetch()) {
$company_name = $row['company_name'];
echo "<option class='' name='' value='$company_name'> $company_name </option>";
} // end of while //
}?> <!-- end of if -->
</select>
JQUERY:
$('.jobtitle_select').change(function(){
$.ajax({
//create an ajax request to load_page.php
type: "POST",
data:$('.jobtitle_select'),
dataType: "html", //expect html to be returned
success: function(date){
$('.company_name_select').html(date);
}
})
});
Your ajax call is not correct, or better say not complete. You need to provide the url, and the posted variable with name and value.
$('.jobtitle_select').change(function(){
$.ajax({
//create an ajax request to load_page.php
url: "load_page.php",
type: "POST",
data: {jobtitle_select: $('.jobtitle_select').val()},
dataType: "html", //expect html to be returned
success: function(date){
$('.company_name_select').html(date);
}
})
});
and your load_page.php file should have the php of code to read data from the database and the html code to display only the inner part of the select:
<?php
$jobtitle_select = $_POST['jobtitle_select'];
if ($jobtitle_select){
$sql = $dbh->prepare("SELECT * FROM jobs_list WHERE company_name = :jobtitle_select");
$sql->bindParam(':jobtitle_select', $jobtitle_select, PDO::PARAM_STR);
if($sql->execute()) {
$sql->setFetchMode(PDO::FETCH_ASSOC);
}
?>
<option class="" name="" value="" >-- Company name --</option>
<?php while($row = $sql->fetch()) {
$company_name = $row['company_name'];
echo "<option class='' name='' value='$company_name'> $company_name </option>";
} // end of while //
}?> <!-- end of if -->
Related
So i want to make my select dropdown box flexible, however i tried using AJAX which i found online, but the AJAX request is a standalone data, it does not return back the data.
Is there any convenient way to get the data and able to submit to another PHP file ?
Here is my Code
index.php
<td>
<select id="ownerID" name="OwnerID" class="id" required>
<?php
$Employee_ID='';
$sql1="SELECT Employee_ID FROM user1 WHERE Position1='QE' OR Position1='OTHER'";
$result1=odbc_exec($conn,$sql1);?>
<option value="">Choose</option>
<?php while($row1=odbc_fetch_array($result1)){
$Employee_ID=$row1['Employee_ID'];
?>
<option value ="<?php echo $Employee_ID;?>"><?php echo $Employee_ID;?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td id="response" style="margin-left:50px;">
</td>
AJAX
<script type="text/javascript">
$(document).ready(function(){
$("select.id").change(function(){
$("#response option").remove();
var selectedOwner = $(".id option:selected").val();
$.ajax({
type: "POST",
url: "process-request.php",
data: { id : selectedOwner}
}).done(function(data){
$("#response").html(data);
});
});
});
</script>
post-requst.php
if(isset($_POST["id"])){
$id = $_POST["id"];
$Form_Tracking_ID=null;
$sql="SELECT Form_Tracking_ID FROM masterlist1 WHERE Owner_I_Employee_ID = '$id' AND Tool_Status='Active' AND Dereg_Reason1 IS NULL AND CEF_ID IS NULL
UNION SELECT Form_Tracking_ID FROM masterlist1 WHERE Owner_I_Employee_ID = '$id' AND Tool_Status='Active' AND Dereg_Reason1 IS NULL AND CEF_ID = ' '
UNION SELECT Form_Tracking_ID FROM masterlist1 WHERE Owner_I_Employee_ID = '$id' AND Tool_Status='Active' AND Dereg_Reason1 = ' ' AND CEF_ID IS NULL
UNION SELECT Form_Tracking_ID FROM masterlist1 WHERE Owner_I_Employee_ID = '$id' AND Tool_Status='Active' AND Dereg_Reason1 = ' ' AND CEF_ID = ' '";
$result=odbc_exec($conn,$sql);
if($id !== 'Choose'){
echo "<label>Tool ID:</label>";
echo "<br><select id='toolid' name='ownerid' required>"; ?>
<option value="">Choose</option>
<?php while($row=odbc_fetch_array($result)){
$search=$row['Form_Tracking_ID']; ?>
<option value="<?php echo $search ?>"><?php echo $search ?></option>
<?php }
echo "</select>";
}
}
You can use a function for get data from database, getting value of 1º select and put them on 2º select
//Get value from 1 select
$("#firstSelect").change(function () {
let firstSelectValue = $("#firstSelect").val();
$("#secondSelect").text("");
//Calling a function to get value from database
getDataForSecondSelect(firstSelectId);
})
function getDataForSecondSelect(firstSelectValue) {
$.ajax({
url: `getDataForSecondSelect.php?value=${firstSelectValue}`,
method: "GET",
dataType: "JSON",
success: function (message) {
if (message.length != 0) {
$('#secondSelect').prop('disabled', false);
for (let i = 0; i <= message.length; i++) {
$("#secondSelect").prepend(`<option value=${message[i]["id"]}>${message[i]["name"]}</option>`);
}
} else {
$("#subcategoriaProduto").text("");
$("#subcategoriaProduto").prepend("<option>There's no data</option>");
$('#subcategoriaProduto').prop('disabled', true);
}
}
});
}
Okay so this is my table 'timings'
Table 'timings'
I'm showing this data in a table, and I want the availability to be updated whenever the availability is selected to a different value by the user. This is the code for the table:
<!-- Table for timings-->
<table id="tableCreneaux">
<tr>
<th> Créneau </th>
<th> Disponibilité</th>
</tr>
<?php
$sql_check = 'SELECT * FROM timings ORDER BY timingOrder';
$res_check = mysqli_query($conn, $sql_check);
if(mysqli_num_rows($res_check) > 0){
while($row =mysqli_fetch_assoc($res_check)){
$creneau = $row["timing"];
$statutCreneau = $row["available"];
echo '<tr>';
echo '<td>'.$creneau.'</td><td> <select id="statut'.$creneau.'">';
if($statutCreneau == 1)
{
echo '<option value="available"> Disponible </option> <option value="unavailable"> Non disponible </option> ';
}
else
{
echo '<option value="unavailable"> Non disponible </option><option value="available"> Disponible </option> ';
}
echo '</select> </td> </tr>';
}
}
?>
</table>
How do i bind the database's 'available' value to the value of the dropdown list?
1-you should use javascript code for update database on change
2-your select should be out of while{}
HTML:
<select id="select" onchange="update_database()">
while(condition){
<option...> </option>
}
</select>
AJAX SCRIPT:
<script>
function updatae_database(val)
{
$.ajax({
url: 'send.php', // The PHP file that you want to send your user input to
type: 'POST',
data: {data: $('#select').val()}, // The data to be sent
dataType: 'text',
success: function(data)
{
alert('update successful');
}
});
}
</script>
send.php :
<?php
include('database.php');
$name = $age = $id ="";
if(isset($_GET['data']))
{$data=$_GET['data'];}
$query = "update TABLE set COLUMN='$data'";
$sql = mysqli_query($con,$query);
?>
I have created a form in that form I want that if the user selects one element from select tag then select tag 2 should enable and shows the value related to that element.
<form action="filename.php" method="post" enctype="multipart/form-data" >
<?php while($row =mysqli_fetch_array($result)) {?>
Artist: <?php echo $row['artist'];?>
<select name="to_artist" id = "to_artist" class="form-control">
<option value="<?php echo $row['artist'];?>">Select If you want to change</option>
<?php
$sql1 = mysqli_query($con, "SELECT DISTINCT artist_name FROM album");
$row1 = mysqli_num_rows($sql);
while ($row1 = mysqli_fetch_array($sql1)){
echo "<option value='". $row1['artist_name'] ."'>" .$row1['artist_name'] ."</option>" ;
}
?>
</select>
Album : <?php echo $row['album_name'];?>
<select name="to_album" id = "to_album" class="form-control">
<option value="<?php echo $row['album_name'];?>">Select If you want to change</option>
<?php
$artistname=$_POST['to_artist'];
$sql2 = mysqli_query($con, "SELECT * FROM album where artist_name='$artistname'");
$row2 = mysqli_num_rows($sql2);
while ($row2 = mysqli_fetch_array($sql2)){
echo "<option value='". $row2['album_name'] ."'>" .$row2['album_name'] ."</option>" ;
}
?>
</select>
<?php }?>
<input type="Submit" value="Submit" name="save" id="save"/>
</form>
In this code, I want that if the user selects an artist name then related to that artist albums will be shown in another select tag can anyone help me?
I recommend to use jquery for that and make an ajax call to php. For example
<script>
$("#to_artist").change(function () {
var selected_artist= $("#to_artist option:selected").val();
$.ajax({
url: "phpfile.php",
type: "POST",
data:{artist:selected_artist},
success: function (response) {
//here append response to select box.
}
});
});
</script>
<?php
if(isset($_POST['artist'])){
$artistname=$_POST['artist'];
//here your query to get albums for the selected artist and return them.
$sql2 = mysqli_query($con, "SELECT * FROM album where artist_name='$artistname'");
$row2 = mysqli_num_rows($sql2);
while ($row2 = mysqli_fetch_array($sql2)){
echo "<option value='". $row2['album_name'] ."'>" .$row2['album_name'] ."</option>" ;
}
}
?>
//you can create a div inside select box where you would like to append options.
Ex.
<select>
<div id="appended-options">
</div>
</select>
use this in jquery : $("#appended-options").append(response);
Here is the code snippet :
<select name="isbn" onchange=" ">
<?php
//Displaying all ISBN in drop down
$result = mysqli_query($con,"SELECT * FROM book");
while($row = mysqli_fetch_array($result)) {
?>
<option> <?php echo $row['ISBN']; ?></option>
<?php } ?>
</select>
</label></td>
</tr>
<tr>
<td>Copy Number </td>
<td><select name="copy_number">
<?php
//Now based on selected book I want to fetch number of copies from database
$result = mysqli_query($con,"SELECT number_of_copies FROM book where isbn = [ SELECTE VALUE FROM ABOVE]");
?>
How can I do that?
You can do that with jQuery ajax and simple php handler to get copies of selected book,
<select name="isbn">
.....
</select>
<div id="copies"></div>
<script>
$.ajax({
url: "getCopies.php?",
data: "isbn=" + $("select[name='isbn']").val(),
type: "POST",
dataType: "json",
success: function(response) {
$.each(response, function(i, item) {
$("#copies").append(item.name); // Sample json format {id: "213123", name:"Lord of the rings", isbn:"887799..."}
})
}
});
</script>
getCopies.php
<?php
$isbn = $_POST["isbn"];
// Some db connections
$result = mysqli_query($con,"SELECT number_of_copies FROM book where isbn = $isbn");
$resultArr = array();
while($row = mysqli_fetch_array($result)) {
$resultArr[] = $row;
}
echo json_encode($resultArr); // This will return rows in json format. You can iterate it in js side
I have some problems with my html/php/ajax code about dependent (or chained) select. I want to show in my menu the list of faculties after I have decided the university.
I'll show you my (italian) code. I hope you'll help me. Thanks.
javascript ajax code:
<script type="text/javascript">
$(document).ready(function()
{
$(".universita").change(function()
{
var dataString = 'id='+ $(this).val();
$.ajax
({
type: "POST",
url: "ajax_facolta.php",
data: dataString,
cache: false,
success: function(html)
{
$(".facolta").html(html);
}
});
});
});
html code about two select boxes:
<td align="right">Università: </td>
<td>
<select class="input" name="universita">
<option selected="selected">--Seleziona Università--</option>
<?php
require('config.php');
$query = mysqli_query($con, "SELECT * FROM UNIVERSITA order by id ASC");
$num_righe = mysqli_num_rows($query);
for($x=0; $x<$num_righe; $x++)
{
$rs = mysqli_fetch_row($query);
$id = $rs[0];
$nome = $rs[1];
?>
<option value="<?php echo $id;?>"> <?php echo $nome; ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td align="right">Facoltà: </td>
<td><select class="input" name="facolta">
<option selected="selected">--Seleziona Facoltà--</option>
</select></td>
</tr>
the file ajax_facolta.php:
<?php
require('config.php');
if($_POST['id'])
{
$id=$_POST['id'];
$sql = mysqli_query($con, "SELECT * FROM FACOLTA WHERE id_univ='$id' ");
echo '<option selected="selected">--Selziona Facoltà--</option>';
while($row=mysqli_fetch_array($sql))
{
$id=$row['id'];
$nome=$row['nome'];
echo '<option value="'.$id.'">'.$nome.'</option>';
}
}
?>
and the simple configure.php:
<?php
$con = mysqli_connect("127.6.143.130","xxxxx","xxxxx", "jeme");
if (!$con)
{
die('Errore nella connessione: ' . mysqli_connect_error());
}
?>
The database is very simple.
UNIVERSITA has (id, nome)
FACOLTA has (id, nome, id_univ).
I do not find any errors but it does not work. Thanks for the help.
In HTML you have :
<select class="input" name="universita">
and you are using
$(".universita").change(function()
$(".universita") will try to search HTML control having class name "universita"
So change few things in code. I hope it will work
1). <select class="input" name="universita" id="universita">
2). $("#universita").change(function()
3). <td><select class="input" name="facolta" id="facolta">
4). $("#facolta").html(html);
let me know is this helpfully?.