0

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&nbsp;:&nbsp;</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&nbsp;(&nbsp;&pound;&nbsp;)&nbsp;</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);
?>
Suneet
  • 97
  • 2
  • 11
  • show your code what you have tried in brief – Ghostman Apr 01 '15 at 05:42
  • You are missing Ajaxing from client to server. `$.get("mysql.php?student="+$("#student").val(),function(data) { do something on client with name, age...});` – mplungjan Apr 01 '15 at 05:44
  • you mean nested ajax? – roullie Apr 01 '15 at 05:52
  • Why would you change the value of the select to the value of the select via the server? Not sure your code reflects your needs. I assume you need to split the above into TWO php processes. One to build the page and one to return the result of the sql based on the drop down value, i.e. the showonselect needs to be changed. Have a look here http://stackoverflow.com/questions/19155192/jquery-ajax-call-to-php-script-with-json-return – mplungjan Apr 01 '15 at 06:28
  • You mean I should run the select where statement in the second php "showonselectFY.php". ok this is done. But how will I populate my html table with the data that will return on running the sql query. Pls guide ?? – Suneet Apr 01 '15 at 06:59
  • Look around SO. `[jquery][json]` - http://stackoverflow.com/a/27814032/295783 for example – mplungjan Apr 01 '15 at 07:51

0 Answers0