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.
ie
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>
<? } ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.