When we use ajax the value is in server side, which on successful execution can be shown using
.done(function(data) {console.log(data)}
The result of the ajax is inside "data". My question is can we pass the value of "data" in a php variable.
My scenario is : There is a dropdown, below this is an html table. So when the user selects an item in the dropdown, using ajax the selected values passes in the "data".
After this I am running a Mysql query
"Select name, age, sex from student where name = '{$retvalue}';
Now instead of "??", I want to pass value inside "data" in a php variable $retvalue
.
How can I achieve this?
I have tried my level best and even after a thorough search in the net, I am not able to find anything.
Code is as follows : Main - File
<?php
include ("dbcon.php");
include ("included_functions.php");
?>
<section>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/jquery.min.js"></script>
<script src="<?php bloginfo('template_url');?>/js/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#fyearddl").change(function(){ /* Drop down List */
var fyear = $(this).val();
data = {fyear:fyear};
$.ajax({
type: "GET",
url: '<?php bloginfo("template_url");?>/showonselectFY.php',
data: data,
cache: false
})
.done(function(data) {
console.log(data);
$('#inboxfy').empty();
$('#inboxfy').val(fyear);
})
});
});
</script>
<div style="width:100%; height:30px; padding-bottom:45pt; background-color:#fff"></div>
<form id="formCatg" action="" method="POST">
<div class="centr-div">
<div class="divbox">
<label id="ddltxt">To view : </label>
<?php
$fyquery = "select fyear from finyear";
$fyresult = mysqli_query($connection, $fyquery);
echo "<select id='fyearddl' name='fyearddl'>";
echo "<option value = '-- Select Financial Year --'>-- Select Financial Year --</option>";
while ($resultarr = mysqli_fetch_assoc($fyresult)){
echo "<option value = '$resultarr[fyear]'>$resultarr[fyear]</option>";
}
echo "</select>";
?>
<input id="inboxfy" type="text" name="inboxfy" value="" style="width:20%; height:15pt" />
</div>
<div id="tablediv" class="scroll">
<table id="showselfy">
<thead>
<tr>
<th>S.no.</th>
<th>Quantity</th>
<th>Price ( £ ) </th>
</tr>
</thead>
<?php
//$query="select rt_id, rt_qty, rt_cost,fin_yr from rate_mast where fin_yr='2014-2015'";
$ddlvalue = $_POST['fyearddl'];
$query="select rt_id, rt_qty, rt_cost from rate_mast where fin_yr='{$ddlvalue}'";
$retval = mysqli_query($connection, $query);
while( $retvalarr = mysqli_fetch_assoc($retval)){
?>
<tbody>
<tr>
<td><?php echo $retvalarr['rt_id'] ?></td>
<td><?php echo $retvalarr['rt_qty'] ?></td>
<td><?php echo $retvalarr['rt_cost'] ?></td>
<td style="display:none"><?php echo $retvalarr['fin_yr'] ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
<span id="dataresult"></span>
</div>
</div>
</div>
</form>
</section>
<?php
mysqli_close($connection);
?>
<?php //get_footer(); ?>
showonselectFY.php
<?php var_dump($_GET); ?>
<?php
include("dbcon.php");
include("included_functions.php");
if(isset($_GET['fyear'])){
$getfinyear = $_GET['fyear'];
echo $getfinyear;
}
?>
<?php
mysqli_close($connection);
?>