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);
?>
Related
i'm just learning about jquery and ajax, trying to create combo box and get the data based on the combo box value
here is my code on index.php
<form action="cari.php" method="post">
<select name="kamar">
<option value="regular single">Regular Single Rp 99.000</option>
<option value="regular double">Regular Double Rp 120.000</option>
<option value="family single">Family Single Rp 127.500</option>
<option value="family double">Family Double Rp 150.000</option>
<option value="vip single">V.I.P Single Rp 170.000</option>
<option value="vip double">V.I.P Double Rp 200.000</option>
</select>
<input type="submit" value="submit" name="submit">
</form>
<br>
<div id="tampil"></div>
<script>
$(document).ready(function(){
loadData();
$('form').on('submit', function(e)
{
e.preventDefault();
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success:function(){
loadData();
},
});
})
})
function loadData(){
$.get("tampil3.php", function(data){
$("#tampil").html(data)
})
}
</script>
here for cari.php
<?php
include "koneksi.php";
$jenis = $_POST['kamar'];
$sql= "select * from kamar where jkamar = '$jenis' ";
$result = mysqli_query($conn, $sql);
?>
<table cellpadding=2 cellspacing=2>
<?php
if(mysqli_num_rows($result) > 0)
{
while ($data = mysqli_fetch_array($result))
{
?>
<tr><?php echo $data['nokamar']; ?></tr>
<?php
}
}
else
{
echo "no more data!";
}
?>
</table>
tampil3.php, for getting all records from database
<?php
include "koneksi.php";
$sql= "select * from kamar";
$result = mysqli_query($conn, $sql);
?>
<table border=1 cellpadding=2 cellspacing=2>
<?php
if(mysqli_num_rows($result) > 0)
{
while ($data = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $data['nokamar']; ?></td>
</tr>
<?php
}
}
else
{
echo "no more data!";
}
?>
</table>
i already tried this code and its not working, any help?
any suggestion will be appreciated
thanks!
I have this select , i wanna save each value after change , save it and use in other select
This is my code :
<?
$sql = "SELECT * FROM championnat ";
$result = $conn->query(sprintf($sql));
if($result){
if ($result->num_rows != 0)
{
$rows=array();
?>
<select name="nom_championnat" id="nom_championnat" >
<option value=""></option>
<?php
while($r=mysqli_fetch_assoc($result))
{
?>
<option value=" <?php echo $r['idChampionnat']?>" name="nom_championnat" selected >
<?php echo $r['nomChampionnat'] ?></option>
<?php
}
}
}
?>
</select>
</div>
I need the variable $r['idChampionnat'] to save it in each select and use it in this requete , how can it asve and put it in that requete sql ????
<?php
$sql = "SELECT * FROM equipe where idChampionnat=???? ";
$result = $conn->query(sprintf($sql));
if($result){
if ($result->num_rows != 0)
{
$rows=array();
?>
<select name="equipe1" >
<option value=""></option>
<?php
while($r=mysqli_fetch_assoc($result))
{
?>
<option required value=" <?php echo $r['nomEquipe']?>" name="equipe1" selected ><?php echo $r['nomEquipe'] ?>
</option>
<?php
}
}
}
?>
</select>
just to clear it ,
You need to use jQuery to fire an AJAX call when the first box is selected.
Its been a while since I've done this but this should give you some idea. I took some code from here and here as example
Say your html looks like this
<select id="nom_championnat">
<option value="value1">value1</option>
<option value="value2">value2</option>
</select>
<select id="equipe1"></select>
then you need to tell jquery what to do when nom_championnat changes selection
$('#nom_championnat').change(function() {
var data = "";
$.ajax({
type:"POST",
url : "queryfile.php",
data : "value="+$(this).val(),
async: false,
success : function(response) {
data = response;
return response;
},
error: function() {
alert('Error occured');
}
});
var string = data.message.split(",");
var array = string.filter(function(e){return e;});
var select = $('equipe1');
select.empty();
$.each(array, function(index, value) {
select.append(
$('<option></option>').val(value).html(value)
);
});
});
and then you need a queryfile.php to handle the ajax requests, something like
<?php
print_r($_POST);
$value = $_POST["value"];
$sql = "select where {$value} ..."
$result = execute($sql);
echo $result;
?>
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?.
I've a html form where the countries in the drop down are coming from a database. If user selects any country, then the city drop diwn will appear based on selected country.
If user wrongly input any field of the form (there are other field in this form) then country drop down will will remember which country the user initially selected, but clears the city, resetting it to --Select City--. I'm trying to selected the city name but I can't. Any idea ?
Ajax Code here:
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
}
});
});
});
</script>
ajax_city.php here
<?php
require_once("toplevel/content/database/databd.php");
if($_POST['id'])
{
$id=$_POST['id'];
$sql=mysql_query("select Name from cities WHERE CountryCode ='$id'");
while($row=mysql_fetch_array($sql))
{
$id=$row['Code'];
$data=$row['Name'];
if($_POST['city'] == $data)
{
$sel = 'selected = "selected"';
}
else
{
$sel = " ";
}
echo '<option value="'.$data.'" ' .$sel . ' >'.$data.'</option>';
}
}
?>
Html form here:
<tr>
<td>PAYS <span style="color: #FF0000;">*</span></td>
<td>
<select name="country" class="country">
<option value="">Select</option>
<?php
$sql=mysql_query("select * from country");
while($row=mysql_fetch_array($sql))
{
$id=$row['Code'];
$data=$row['Name'];
$data = mb_convert_encoding($data, 'UTF-8');
if($id == $_POST['country'])
{
$sel = 'selected="selected"';
}
else
{
$sel = "";
}
echo '<option value="'.$id.'"' . $sel . '>'.$data.'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td>City</td>
<td>
<select class="city" name="city">
<option selected="selected" value="">--Select City--</option>
</select>
</td>
</tr>
Not quite sure but issue is in datastring try changing this:
$(".country").change(function(){
var id=$(this).val();
var dataString = {'id': id};
$.ajax({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html){
$(".city").html(html);
}
});
});
You will need to rebuild your HTML on reload, but now with your City list (this is untested, but based on your code from the Country dropdown):
<tr>
<td>City</td>
<td>
<select class="city" name="city">
<option selected="selected" value="">--Select City--</option>
<?php
if (isset($_POST['city']) {
$sql=mysql_query("select * from city");
while($row=mysql_fetch_array($sql))
{
$id=$row['Code'];
$data=$row['Name'];
$data = mb_convert_encoding($data, 'UTF-8');
if($id == $_POST['city']) { $sel = 'selected="selected"'; }
else { $sel = ""; }
echo '<option value="'.$id.'"' . $sel . '>'.$data.'</option>';
}
}
?>
</select>
</td>
</tr>
WARNING: This is just same sample code. This should never ever go into production, because it has the potential for being extremely unsafe! As #PolishPrince has pointed out above, you should at least use PDO or MySQLi.