将值从ajax变量传递给php变量

时间:2015-04-01 05:40:12

标签: php jquery ajax

当我们使用ajax时,值在服务器端,成功执行时可以使用

显示
.done(function(data) {console.log(data)}

ajax的结果在"数据"内。我的问题是我们能否传递"数据"在php变量中。

我的方案是:有一个下拉列表,下面是一个html表。因此,当用户在下拉列表中选择一个项目时,使用ajax选择的值将传入"数据"。

在此之后我运行了一个Mysql查询

"Select name, age, sex from student where name = '{$retvalue}';

现在而不是" ??",我希望将值传递到"数据"在php变量$retvalue中。

我怎样才能做到这一点?

我已经尝试过最好的水平,即使在网上彻底搜索后,我也找不到任何东西。

代码如下: 主要 - 文件

<?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);
?>

0 个答案:

没有答案