I have written a query to fetch data from the table and display in textbox. I need to echo the result from ajaxfile1.php to ajaxfile2.php
Moving. This isn’t a database question.
What do you mean echo it to another file?
yeah, not enough information, basically
php -f ajaxfile1.php > php -f ajaxfile2.php
Or, just use the same query in the other file.
Or, store the result in another table.
Or, make a function that gets the database as a parameter and returns the results.
Or, more sophisticated, make a model that Returns this data.
Or, i guess you want something like sessions.
Or cookies.
Or, If its ajax like implied, use “local storage”
Sorry for the confusion. I have a file named ajax1.php where I have created 2 dropdown where I am fetching data from the database on selection of 1st dropdown.
I have written a ajax function which redirects to ajax2.php, in this I have written the sql query where I am echoing the row’s data. All I want is these echoed row data should be passed to ajax1.php file
In ajax1.php I have taken input tag within table data where I want the row data from ajax2.php to echo. I tried using include but I am unable to get it.
From that description, you shouldn’t be using ajax for anything other than populating the second dropdown.
So,
Page 1:
user makes a selection in dropdown 1.
Ajax call is made to populate dropdown 2.
User makes selection from dropdown 2.
Form submits for processing to Page 2.
Page 2:
Form from Page 1 is submitted.
Display results.
Still makes no sense, what startegy from the above did you use, why, and what results did you expext and get from which codes?
In page 1: I have done just the way you have mentioned.
In Page 2:
$reg_number =$_POST['reg_number'];
$sql = mysqli_query($conn, "SELECT b.user_tgi, p.proj_manager_id, concat(e.first_name, e.last_name) as ApproverName FROM hris_employee_details b
INNER JOIN hris_cost_centers c ON c.id = b.cost_centre_id
INNER JOIN bta_project p ON p.project_id = b.project_id
INNER JOIN employee_main_db e ON b.user_tgi = e.tgi OR p.proj_manager_id = e.tgi WHERE c.cost_name = '".$reg_number."' OR p.bta_proj_name = '".$reg_number."' GROUP BY b.user_tgi, p.proj_manager_id");
if($sql){
while ($row = mysqli_fetch_array($sql)) {
$data = $row['user_tgi'];
echo $data;
}
}
Sorry for the above mess but that is how I have my query where I want the $data to be displayed in Page 1
<td colspan ="2" name="" id="approver[2]">
<?php
// Display that $data here.....
?>
</td>
Still not enough info.
Let’s take a different approach. You want an ajax call from one page to another and the results to go to page 1?
So, what is the ajax code?
I will share the code then I hope you will get an idea.
ajaxfile1.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
if (!isset($_SESSION)) {
session_start();
}
if (!isset($_SESSION['tgi'])) {
header("Location:" . dirname($_SERVER['PHP_SELF']) . "/bta_show_message.php?msgid=e6");
}
$username = $_SESSION['tgi'];
$username = strtoupper($username);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Travel: Create Page</title>
<script type="text/javascript" language='javascript' src='../JavaScripts/add_delete.js'></script>
<script type="text/javascript" language='javascript' src='../JavaScripts/data_validation.js'></script>
<script type="text/javascript" language='javascript' src='../JavaScripts/jquery-1.4.2.min.js'></script>
<link rel="stylesheet" type="text/css" href="../../Common/Css/main.css"/>
<link rel="stylesheet" type="text/css" href="../Css/bta_formstyle.css"/>
<link rel="stylesheet" type="text/css" href="../Css/formstyle_print.css" media="print"></link>
<style>
table{border-collapse:collapse; }
td, tr{border-collapse:collapse;}
input[readonly]{
background-color:#F7F5F5;
}
body {
margin-bottom: 0px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head>
<body>
<?php
$conn = connect_to_db();
?>
<link rel="stylesheet" type="text/css" media="all" href="../Widgets/ATM_DatePicker/atmDPstyle.css" />
<script language="javascript" type="text/javascript" src="../Widgets/ATM_DatePicker/atmDP.js"></script>
<div id="bodysize">
<div id="page">
<div class="formframe"></div>
<form name="MainBTA" id="MainBTA" onsubmit="return validateBTAForm()" method="POST" action="generate_request.php?bta_id=<?php echo $id; ?>" align="center">
<table align="center" width="850" border="0">
<tr valign="top">
<!-- <tr> -->
<td align="left">Cost Assignment <span class="redtext">*</span></td>
<td>
<div class="">
<div class="form-group">
<!-- <input type="text" class="form-control" name="distplace1" id="distplace1" placeholder="District Preference 1*" > -->
<select class="form-control" name="distplace1" id="distplace1" onchange="gettaluka(this.value);" required>
<option value="0">Select Assignment</option>
<option value="1">Cost Charges</option>
<option value="2">Projects</option>
<option value="3">Other Recovery</option>
</select>
</div>
</div>
</td>
<td>
<div class="">
<div class="form-group">
<select class="form-control" name="talukaplace" id="talukaplace" maxlength="10" required>
<option value="0" maxlength="10">Select Values</option>
</select>
</div>
</div>
</td>
<script type="text/javascript">
$(document).ready(function(){
$('#talukaplace').change(function(){
var reg_number = $('#talukaplace').val();
var data_String = $('#distplace1').val();
$.ajax({
type:'POST',
url:'ajax_file.php',
data:{reg_number, data_String},
success:function(data){
console.log(data);
// return;
$('#approval2').val(data);
$('#distplace1').val(data);
$('#approverName').val(data);
// var value = $('#approval2').val(data);
}
});
});
});
</script>
<!-- </tr> -->
</tr>
<tr>
<td colspan="6"> </td>
</tr>
<!-- Level 2 Approver Testing -->
<tr>
<td>
<label>Level2 Approver:</label>
</td>
<div align="left">
<td>
<input type="text" id="approval2" name="approvals[2]" onblur=""/>
</td>
<td colspan ="2" name="" id="ApproverName">
<?php
include 'ajax_file.php';
// foreach ($row1 as $value) {
// # code...
// echo $value['ApproverName'];
// }
?> </td>
</div>
</tr>
<!-- Level 2 Approver End Testing -->
<tr colspan="0">
<td height="25"> </td>
</td>
</tr>
</table>
<table width="968px" border="0" cellspacing="0" cellpadding="0" class="submitbg" align="center" >
<tr class="submitbg">
<td height="20px" colspan="4" ></td>
</tr>
<tr>
<td width="416"> </td>
<td width="86"><input type="reset" name="reset" value="Reset" style="width:60px;" /></td>
<td width="116">
<input type="submit" name="bta_submit" id="submit" value="Submit" style="width:60px;"/>
</td>
<td width="350"> </td>
</tr>
<tr class="submitbg">
<td height="20px" colspan="4" ></td>
</tr>
</table>
</form>
</div>
<?php close_db_connect($conn); ?>
<?php include ($DOCPATH . '/Common/Php/footer.php'); ?>
</div>
</body>
</html>
In the above code which was posted earlier is my ajaxfile2.php where I have written the query and fetching the name.
As u can see I have called ajax function below level 2 approver, I want to echo the Approver Name from ajax_file.php in this ajaxfile1.php
So, this section here, you are sending whatever is returned to different places.You are also overwriting whatever is returned, so you will only get the last values returned.
What you want to do is in the file it calls return a JSON object, then parse that and send the individual parts to the correct elements.
– Use prepared statements!
$sql = "SELECT b.user_tgi, p.proj_manager_id, concat(e.first_name, e.last_name) as ApproverName FROM hris_employee_details b
INNER JOIN hris_cost_centers c ON c.id = b.cost_centre_id
INNER JOIN bta_project p ON p.project_id = b.project_id
INNER JOIN employee_main_db e ON b.user_tgi = e.tgi OR p.proj_manager_id = e.tgi WHERE c.cost_name = ? OR p.bta_proj_name = ? GROUP BY b.user_tgi, p.proj_manager_id";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_POST['reg_number'], $_POST['reg_number']]);
$resp = $stmt->fetchAll(PDO::FETCH_OBJECT);
echo json_encode($resp);
exit();
So, if you are only after a single column, why are you selecting so many columns?
Thank you for your guidance. I will try that and see, all these columns are required for the project that I am working on.
All of the columns may be in the table, but that doesn’t mean all of them need to be selected for a particular process. If you just need the id, then just select the id.