Display Table based on dropdownlist selection in PHP - php

My table is not displayed when i select any project name. I guess the onchange function is not working properly but i couldn't figure out the problem.
Code is as follows:
<div class="span6">
<?php $sql = "SELECT * from login_projects WHERE login_id='".$record['login_id']."'";
$res_sql = mysql_query($sql); ?>
<label>Project Name <span class="f_req">*</span></label>
<!--<input type="text" name="city" class="span8" />-->
<select name="project_name" onchange="get_list_onnet()" id="project_name" class="span8">
<option value="">--</option>
<?php while($rec_sql = mysql_fetch_array($res_sql)){ ?>
<option value="<?php echo $rec_sql['project_id']; ?>">
<?php echo $rec_sql['project_name']; ?></option>
<?php } ?>
</select>
</div>
Function:
<script>
function get_list_onnet(){
var project_name=$("#project_name").val();
$.ajax
({
type: "POST",
url: "ajax.php",
data: {action: 'get_list_onnet',list_onnet:project_name},
success: function()
{
document.getElementById("dt_a").style="block";
$("#dt_a").html(html);
}
});
};
</script>
<script>
$(document).ready(function() {
//* show all elements & remove preloader
setTimeout('$("html").removeClass("js")',1000);
});
</script>
Ajax.Php Page:
function get_list_onnet(){
$list_onnet=$_POST['list_onnet'];
$sql_list_onnet=mysql_query("SELECT * from projects,project_wise_on_net_codes
where projects.project_id = project_wise_on_net_codes.project_id AND
project_wise_on_net_codes.project_id='$list_onnet'");
$row1 = mysql_num_rows($sql_list_onnet);
if($row1>0)
{
echo "<tr><th>id</th><th>Project Name</th><th>Country Code</th><th>On-net prefix</th>
<th>Action</th></tr>";
$k = 1; while($row_list_onnet=mysql_fetch_array($sql_list_onnet))
{
$project3 = $row_list_onnet['project_name'];
$countrycode1 = $row_list_onnet['country_code'];
$prefix1 = $row_list_onnet['on_net_prefix'];
$id_proj = $row_list_onnet['project_id'];
$on_prefix = $row_list_onnet['on_net_prefix'];
echo "<tr><td>".$k."</td><td>".$project3."</td><td>".$countrycode1."</td>
<td>".$prefix1."</td><td><a href='process/update_process_onnet.php?ID=".$id_proj."&Onnet=".$on_prefix."'>Delete</a></td>
</tr>";
$k++;
}
}
else
{
echo "<script>alert('No Record Found')</script>";
}
}
The problem is that it is always going in the else condition and nothing is displayed in the table.

Related

AJAX POST Not Returning php mysql ajax

I'm trying to get data from the database using ajax to insert it in other element but the post data not passing to get-data.php
so what the reason can be and the solution
addBuilding.php
<?php
require_once("./dbConfig.php");
$selectIL = "SELECT * FROM iller ";
$selectIL = $db->prepare($selectIL);
$selectIL->execute();
$res = $selectIL->get_result();
?>
<form action="" method="post">
<select name="pp" id="cites">
<option value="">-select state-</option>
<?php
while ($row = $res->fetch_assoc()) {
?>
<option value="<?= $row['id'] ?>"><?= $row['il_adi'] ?></option>
<?php
}
?>
</select>
<select name="district" id="district">
<option value="">-select district-</option>
</select>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="getdata.js"></script>
getdata.js
$(document).ready(function() {
$("#cites").change(function() {
if ( $("#cites").val()!="") {
$("#district").prop("disabled",false);
}else {
$("#district").prop("disabled",true);
}
var city = $("#cites").val();
$.ajax({
type: "POST",
url:"get-data.php",
data:$(city).serialize(),
success: function(result) {
$("#district").append(result);
}
});
});
});
get-data.php
I can see the form data in network inspection put no data passing to get-data.php
<?php
require_once("./dbConfig.php");
if (isset($_POST['pp'])) {
$cites = $_POST['cites'];
$selectIlce = "SELECT * FROM ilceler where il_id=? ";
$selectIlce = $db->prepare($selectIlce);
$selectIlce->bind_param("i", $cites);
$selectIlce->execute();
$res = $selectIlce->get_result();
?>
<?php
while ($row = $res->fetch_assoc()) {
?>
<option value="<?= $row['id'] ?>"><?= $row['ilce_adi'] ?></option>
<?php
}
}
?>
You need to echo the results in get-data.php
<?php
while ($row = $res->fetch_assoc()) {
?>
echo "<option value='". $row["id"]."'>".$row['ilce_adi']."</option>";
<?php
}
}
?>
1- Get data by serialize from form:
$("form").serialize()
2- Add dataType: "json" to ajax option:
$.ajax({
type: "POST",
url:"get-data.php",
data:$(city).serialize(),
dataType: "json",
success: function(result) {
$("#district").append(result);
}
});

Auto populate text field depending on dropdown using same SQL table

I'm trying to populate a text field and a text area depending on the choice of the second dropdown list using an SQL table. Somehow I got it all wrong. Here's my code right now:
AJAX/HTML
<script>
function getPackage(val) {
$.ajax({
type: "POST",
url: "get_package.php",
data:'serviceid='+val,
success: function(data){
$("#package-list").html(data);
}
});
}
function getPackageDesc(val) {
$.ajax({
type: "POST",
url: "get_packagedesc.php",
data:'packageid='+val,
success: function(data){
$("#price").html(data);
}
});
}
function selectService(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
I feel like there is already something wrong with function getPackageDesc. I don't know if it's in this function or in the php file.
MAIN HTML
<div class="frmDronpDown">
<div class="row">
<label>Service:</label>
<br/>
<select name="service" id="service-list" class="demoInputBox" onChange="getPackage(this.value);">
<option value="">Select Service</option>
<?php
foreach($results as $service) {
?>
<option value="<?php echo $service[" serviceid "]; ?>">
<?php echo $service["servicename"]; ?>
</option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Package:</label>
<br/>
<select name="package" id="package-list" class="demoInputBox" onChange="getPackageDesc(this.value);">
<option value="">Select Package</option>
</select>
</div>
<div class="row">
<label>Package Information:</label>
<br/>
<br/> Price:
<br>
<input type="text" name="price" id="price">
<br>
<br> Package Description:
<br>
<textarea name="packagedesc" form="usrform" id="packagedesc"></textarea>
</div>
</div>
get_package.php
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["serviceid"])) {
$query ="SELECT * FROM tbl_packages WHERE serviceid = '".$_POST["serviceid"]."'";
$results = $db_handle->runQuery($query);
?> < option value = "" > Select Package < /option>
<?php
foreach($results as $package) {
?> < option value = "<?php echo $package["
packageid "]; ?>" > <?php echo $package["packagename"]; ?> < /option>
<?php
}
}
?>
This part of the code works. I have populated the dropdowns using two SQL tables, tbl_services and tbl_packages. The next php file is where I'm having trouble.
get_packagedesc.php
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["packageid"])) {
$query ="SELECT * FROM tbl_packages WHERE packageid = '".$_POST["packageid"]."'";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_array($result)){
?>
<?php echo $price['price'] ; ?></br>
<?php echo $packagedesc['packagedesc']; ?> </br>
<?php
}
}
?>
This code is where I'm having trouble at. I don't know if it is okay to get data from the same SQL table (tbl_packages). Or maybe I messed up with the $result.
What is wrong with my code and what could be the solution?
Thanks for any help.
It looks like you're not grabbing the information from the row itself but from undeclared variables. Try this:
<?php echo $row['price'] ; ?></br>
<?php echo $row['packagedesc']; ?> </br>

I This i called the function on the Onsubmit event of form but when my jquery function return true still its not going to action page

In this programme i want to submit my form data if state name is already not present in database.if it is present in database then it gives mi alert that name already present and form not submited. but in the following programme if name is already present then then it gives alert message and form also not submitted. but if state name is not present then still it is not submitted my values(it is not calling form action="add_state_process.php"). i think i am wrong in returning true and false in javascript function.plz tell mi where i am wrong
<form name="addcategoryfrm" id="addcategoryfrm" onsubmit="return check_state();" action="add_state_process.php" method="POST">
<div class="formrow" style="margin-top:0px;height:80px;width:100%; padding-left:30%;">
<div class="txttitle" >Country Name</div>
<div class="txtinputouter1" style="padding-left:8px;">
<select name="country" id="country" class="required txtinput" value="">
<option value="">--- Select Country --- </option>
<?php
include("../config/database.php");
$query="SELECT * FROM `country`";
$result = mysql_query($query) or die(mysql_error());
echo $query;
//$result2 = mysql_fetch_array($result);
$row1=mysql_num_rows($result);
echo $row1;
while($numrow=mysql_fetch_array($result))
{ ?>
<option value=" <?php echo $numrow['id']; ?> ">
<?php echo $numrow['name']; ?>
</option>
<?php } ?>
</select>
</div>
</div>
<div class="formrow" style="margin-top:0px;height:80px;width:100%; padding-left:30%;">
<div class="txttitle">State Name </div>
<div class="txtinputouter1" style="padding-left:1%;">
<input type="text" class="required txtinput" name="state" id="state" class="categoryname" onblur=";"/>
</div>
</div>
<div class="txtinputouter1" style="padding-left:43%;padding-top:10px;">
<input type="submit" class="subbutton" value="Submit"/>
</div>
<div id="demo"></div>
</form>
////function call
<script>
function check_state()
{
valid=false;
var txtarea12 = document.getElementById("country").value;
var name = document.getElementById("state").value;
//var valid=true;
//alert(name);
//alert(txtarea12);
//$name = name;
$.ajax({ type: "GET",
url: 'ajax-state-model.php',
data: { name: name,
countryid:txtarea12
},
success: function(result)
{
if (result == 1)
{
alert("state name already present");
var txtarea = document.getElementById("state");
txtarea.value="";
txtarea.focus();
valid=false;
}
else
{
valid=true;
}
},
error: function(err)
{ alert(err); }
});
return valid;
}
</script>
///function ajax call
<?php
session_start();
if(isset($_SESSION['username1']))
{
include("../config/database.php");
$name=$_GET['name'];
$countryid=$_GET['countryid'];
$query="select * from state where country_name=$countryid && state_name='$name'";
$result = mysql_query($query) or die(mysql_error());
//echo $query;
$rows=mysql_num_rows($result);
if($rows>0)
{
echo 1;
}
else
{
echo 0;
}
}
?>

dropdown based on previous dropdown

I am trying to create a cascading dropwdown for state, city and zip. I have part of it working like when a user chooses a state the corresponding city list comes up, but when a user chooses a city no zips come up. Not sure what i am missing or doing wrong. Can someone please help me with this?
index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="body">
<div id="dropdowns">
<div id="center" class="cascade">
<?php
$sql = "SELECT DISTINCT state FROM tbl_zip ORDER BY state ASC";
$query = mysqli_query($con, $sql);
?>
<label>State:
<select name="state" id = "state">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
<option value="<?php echo $rs["state"]; ?>"><?php echo $rs["state"]; ?></option>
<?php } ?>
</select>
</label>
</div>
<div class="cascade" id="city"></div>
<div id="zip" class="cascade"></div>
</div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select#state").change(function(){
var state = $("select#state option:selected").attr('value');
//alert(state);
$("#city").html( "" );
$("#zip").html( "" );
if (state.length > 0 ) {
//alert(state.length);
$.ajax({
type: "POST",
url: "fetch_state.php",
data: "state="+state,
cache: false,
beforeSend: function () {
$('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#city").html( html );
}
});
}
});
$("select#city").change(function(){
var city = $("select#city option:selected").attr('value');
// alert(state_id);
if (city.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_city.php",
data: "city="+city,
cache: false,
beforeSend: function () {
$('#zip').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#zip").html( html );
}
});
} else {
$("#zip").html( "" );
}
});
});
</script>
</body>
</html>
fetch_state.php
<?php
include("connection.php");
//var_dump($_POST);
$state = trim(mysqli_escape_string($con, $_POST["state"]));
$sql = "SELECT DISTINCT city FROM tbl_zip WHERE state = '".$state ."' ORDER BY city";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>City:
<select name="city" id="city">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["city"]; ?>"><?php echo $rs["city"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}
?>
fetch_city.php
<?php
include("connection.php");
$city = trim(mysqli_escape_string($con, $_POST["city"]));
$sql = "SELECT DISTINCT zip FROM tbl_zip WHERE city = '".$city ."' ORDER BY zip ASC";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>zip:
<select name="zip" id="zip">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["zip"]; ?>"><?php echo $rs["zip"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}
?>
Here is the link, I think this will help :
http://www.91weblessons.com/php-ajax-country-state-city-drop-down/
happyCoding :D
Move the javascript code
$("select#city").change(function(){
var city = $("select#city option:selected").attr('value');
// alert(state_id);
if (city.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_city.php",
data: "city="+city,
cache: false,
beforeSend: function () {
$('#zip').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#zip").html( html );
}
});
} else {
$("#zip").html( "" );
}
});
in fetch_state.php file along the generation of select.

how can i get all record for user on select type

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>
<? } ?>

Categories