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?.
Related
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'm trying to implement live-table edit, but I'm having some trouble with my select options. The input type="text" is fully functional though.
Ever since I tried to add select, it seemed to brake my entire lay-out. First there were multiple rows and columns displayed on the page. But now there's only one and the columns show up empty after the one that contains select.
And the second problem is that ajax doesn't post the table edit since I added select to the mark-up. I'm thinking that this is because the mark-up breaks, but I'm not sure.
If anybody knows what to do, I'd appreciate it.
Markup
<?php
$query = ("select * from projectlist");
$resultaat = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($resultaat, MYSQL_ASSOC)){
$id = $row['projectid'];
?>
<tr class="predit">
<form action="" method="post">
//Working input
<td>
<span id="klant_<?php echo $id; ?>" class="text"><?php echo $row["Klant"]; ?></span>
<input type="text" class="ip" id="klant_ip_<?php echo $id; ?>" value="<?php echo $row["Klant"]; ?>">
</td>
//Same approach, but with select instead of input
<td>
<span id="project_<?php echo $id; ?>" class="text"><?php echo $row["Project"]; ?></span>
<select id="project_ip_<?php echo $id; ?>" class="ip">
<?php
//Fetch select options from another table
$query = ("select * from projecten");
$resultaat = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($resultaat, MYSQL_ASSOC)){
$listid = $row["projectcode"];
$projectnaam = $row["projectnaam"];
?>
<option value="<?php echo $projectnaam; ?>" id="<?php echo $listid; ?>"><?php echo $projectnaam; ?></option>
<?php
}
?>
</select>
</td>
</tr>
</form>
<?php
}
?>
JQuery Ajax
$(document).ready(function(){
//Projeclist
$(".predit").click(function(){
var ID=$(this).attr('id');
$("#klant_"+ID).hide();
$("#project_"+ID).hide();
$("#klant_ip_"+ID).show();
$("#project_ip_"+ID).show();
}).change(function(){
var ID=$(this).attr('id');
var klant=$("#klant_ip_"+ID).val();
var project=$("#project_ip_"+ID).val();
var dataString = 'id='+ ID
+'&Klant='+klant
+'&Project='+project;
//alert(dataString);
var project_txt = $("#project_ip_"+ID+" option:selected").text();
$.ajax({
type: "POST",
url: "post_table.php",
data: dataString,
cache: false,
success: function(html){
$("#project_"+ID).html(project_txt);
$("#klant_"+ID).html(klant);
},
error: function (request, error) {
console.log(arguments);
alert(" Can't do because: " + error);
},
});
});
$(".ip").mouseup(function() {
return false
});
$(document).mouseup(function(){
$(".ip").hide();
$(".text").show();
});
});
post_table.php
<?php
include('config.php');
$klant = $_POST['Klant'];
$project = $_POST['Project'];
$id = $_POST['id'];
$query = "update projectlist
set Klant='$klant',
Project='$project'
where projectid='$id'";
mysql_query($query, $con);
?>
I am new in php. When user select one of the option in dropdown menu, the list of selected item will shows in textarea form. I want to make each of every item on that list, link to another page. How do I write php/html statement in javascrip/jquery? Here is my javascript :
<script type="text/javascript">
function check(){
var select = document.getElementById('category');
var textarea = document.getElementById('model');
$.ajax({
type: 'POST',
url: 'http://localhost/system/ajax.php',
data: {txt:$('#category').val()},
dataType: 'json',
success: function(resp){
result = resp.success;
textarea.value = "";
for(i=0; i<result.length; i++){
textarea.value = result[i] += "\n" + textarea.value;
}
}
});
}
This is my html code :
<form action="" method="post">
<tr>
<td width="116">Category</td>
<td width="221">
<center>
:
<select name="category" id="category" onChange="check()">
<option>--- Choose Category ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("inventory");
$sql = mysql_query("SELECT * FROM equipment GROUP BY equip_category ASC ");
if(mysql_num_rows($sql) != 0){
while($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['equip_category'].'">'.$row['equip_category'].'</option>';
}
}
?>
</select >
</center>
</td></td></tr>
<tr>
<td><p>Model/Brand</p>
<td>
<p align="center">:<textarea name="model" id="model" rows="5" cols="25"><?php echo (''); ?></textarea>
</p>
</td></td>
</tr></form>
To insert PHP into jQuery/JS, use the following syntax:
var phpVar = <?php echo $some_php_var;?>;
console.log(phpVar); //logs $some_php_var
However, oftentimes it is best to just echo jQuery/JS in PHP:
<?php
echo 'var phpVar = '.$some_php_var.';',
'console.log(phpVar);';
?> <!--logs $some_php_var-->
In PHP use $_POST['txt'] to access the value of category that you are passing from Javascript.
Check out this tutorial for connecting PHP-Javascript-MySQL.
this is my ajax.php :-
<?php
if(!empty($_POST)) {
include "connect.php";
$txt = $_POST['txt'];
$query = "SELECT equip_name_desc FROM equipment WHERE equip_category = '$txt'";
$number = 0;
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$time = $row['equip_name_desc'];
$testarray[$number] = $time;
$number = $number+1;
}
echo json_encode(array('success'=>$testarray));
}
?>
Below is the example of output. I want to make 'Lenovo GGG' or 'HP' is a link to another page. Question is, how to write the href link in javascript?
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.
I have an HTML text input field - for example:
<input id="CustID" name="CustID" dir="rtl" value="<? echo $CustID;?>" size="35" required="true" maxlength="9" >
When I insert the number of the user, I need to open a select box to show all ticket for this user.
for example
<select name="ticket" id="ticket" >
<?
$query="SELECT * FROM ticket where CustID='$CustID' ";
$result=mysql_query($query) or die("error: " . mysql_error());
while($row=mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['ticket'] ; ?>"><?php echo $row['ticket'] ; ?></option>
<? } ?>
</select>
How can i use this with AJAX?
This is what I have so far:
<script src="js/jquery.js"></script>
<script language="javascript">
function getData(id) {
$.ajax ({
url: "php_page.php",
type: "POST",
data: {custid:id},
success: function(data){
$("#return").html(data)
}
)} // i have error her why ??
}
</script>
<input type="text" value="<?php echo $CustID;?>" onkeyup="getData(this.value)"/>
<?
include("functions/connect.php");
$query = "select * from customers2 , tickets where customers2.CustID='".$CustID."' and tickets.CustNo=customers2.CustomersNo";
$result=mysql_query($query) or die("error: " . mysqli_error());
while($row=mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['ticket'] ; ?>"><?php echo $row['ticket'] ; ?></option>
<? } ?>
</select>
Put your php on a separate page called php_page.php. Create your ajax call using the jquery library on your display page:
function getData(id) {
$.ajax ({
url: "php_page.php",
type: "POST",
data: {custid:id},
success: function(data){
$("#return").html(data)
}
)}
}
On your form page create a div with id "return" where you want your select options to show up and also call this function either with a button click or onkeyup:
<input type="text" value="<?php echo $CustID;?>" onkeyup="getData(this.value)"/>
Oh, and your mysql connect should use the mysqli library:
$con=mysqli_connect($host,$username,$password,$database);
$query = //same as before
$result=mysqli_query($query) or die("error: " . mysqli_error());
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['ticket'] ; ?>"><?php echo $row['ticket'] ; ?></option>
<? } ?>
</select>
use AJAX to pass CustID to Select page.
i.e
index.php
<script>
function callAjax(str)
{
ajax
}
</script>
<input type = "text" name = "CustID" onblur="callAjax()"/>
<div id = "show">where to display the ajax results</div>
ajax.php
all that code with the select and options.
i will do but the result is get me all record on select and i wont only record of the user i enter ID
index.php
<script src="js/jquery.js"></script>
<script language="javascript">
function getData(id) {
$.ajax ({
url: "php_page.php",
type: "GET",
data: {custid:id},
success: function(data){
$("#return").html(data)
}
})
}
</script>
<input type="text" id="CustID" name="CustID" onkeyup="getData(this.value)"/>
<div id="return"></div>
php_page.php
<select>
<option value="">عرض الكل</option>
<?
include("functions/connect.php");
if(isset($_GET['CustID']))
{
$CustID = $_GET['CustID'];
$sql_check = mysql_query("select * from customers2 , omra , haj , tickets where customers2.CustID='".$CustID."' and tickets.CustNo=customers2.CustomersNo") or die(mysql_error());
if(mysql_num_rows($sql_check)){
while($rows = mysql_fetch_array($sql_check)){
?>
<option value="<?php echo $rows['TicketType'] ; ?>">تذكرة <?php echo $rows['TicketType'] ; ?> برقم <?php echo $rows['TicketRealNo'] ; ?></option>
<? } } ?>
</select>
<? } ?>