duanjiao7440 2016-05-12 07:04
浏览 63
已采纳

如何将Ajax数据值分配给PHP变量

Hiii Everyone,

My PHP Code

<?php 
date_default_timezone_set('Asia/Kolkata');
$date = date("Y-m-d");
$time = date("Y-m-d H:i:s");
require "country.php";
$per_email=$_POST['per_email'];
$stream_name=$_POST['specialization_filter'];
$country_filter=$_POST['country_filter'];
$country = new country();
$users = $country->universities($stream_name,$country_filter);

if ( empty($users) ) {
?>
<br/><br/>
   <?php       echo "<span class=\"alert alert-danger\">No universities Available For Your Search Results!!! </span> <br>" ;

}else{

 ?>
<div class="table-responsive" id="filter_universities">
<table class="table no-border">
<thead >
<tr class="row_univ">
<th class="university_sort" id="name_total_sort" status="asc" style="cursor:pointer">University 
<i class="fa fa-caret-down font-grey-salsa"></i>
</th>
<th class="col-md-3">Program</th>
<th class="fee_structure" id="fee_total_sort" status="asc" style="cursor:pointer">Fee 
<i class="fa fa-caret-down font-grey-salsa"></i>
</th>
</tr>
</thead>
<tbody>
<?php

$i=0;
 foreach ($users as $user){ 
$i++;
?>
        <tr class="tr_bgs">
        <td class="col-md-4">
        <div class="row">
        <div class="col-md-3">
        <img src="images/university1.jpg" class="img-circle" width="62px" height="62px" alt="">
        </div>
        <div class="col-md-9">
        <h5 class="bold fees">
        <a target="_blank" class="university_class" mode="ambitious" cid="292"><?php echo $user['university']?> </a>
        </h5>
        <span class="font-grey-lite"><?php echo $user['country']?> </span>
        </div>
        </div>
        </td>
        <td>
        <h5 class="bold fees"><?php echo $user['course']?> </h5>
        <span class="font-grey-lite"> 
        <?php echo $user['stream_name']?> </span>
        </td>
        <td>
        <h5 class="bold fees"><?php echo $user['fees']?> </h5>
        <span class="font-grey-lite">Per Year</span>
        </td>
        <td class="text-center">
 <div id="added_course<?php echo $user['id']; ?>"><br/><button type="button" class="btn btn-primary" onClick="delete_course_list(<?php echo $user['id']?>);">
 <i class="fa fa-star" aria-hidden="true"></i>&nbsp;Course Added</button></div>
 <div id="add_course<script alert(<?php echo $user['id']?>);></script>">              
 <br/><button type="button" class="btn btn-primary" 
 onClick="save_course_list(<?php echo $user['id']?>);">
 <i class="fa fa-star-o" aria-hidden="true"></i>&nbsp;Add to My Course</button></div></td>
</tr>   
<tr class="row_univ tr_bgs">
<td colspan="6">
<table class="table no-border no-margin text-grey">
<tbody>
<tr class="tr_bgs">

</tr>
</tbody>
</table>
</td>
</tr>  
<?php
 } 
          }  ?>
</tbody>
</table>
</div>
<script>
  function save_course_list(str)
  {
    var course_id=str;
    var per_email= '<?php echo $per_email; ?>' ;
    var datas="&course_id="+course_id+"&per_email="+per_email;
    alert(datas);
   $.ajax({
    type: "POST",
    url: 'php/country/add_course.php',
    data:datas
    }).done(function( data ) 
    {
      alert(data);
      $('#added_course<?php echo $user["id"];?>').show();
      $('#add_course<?php echo $user["id"];?>').hide();    
      });

  }
  function delete_course_list(str)
  {
    var course_id=str;
    var per_email= '<?php echo $per_email; ?>' ;
    var datas="&course_id="+course_id+"&per_email="+per_email;
    alert(datas);
   $.ajax({
    type: "POST",
    url: 'php/country/delete_course.php',
    data:datas
    }).done(function( data ) 
    {
      alert(data);
   $('#added_course<?php echo $user["id"]?>').hide();
   $('#add_course<?php echo $user["id"]?>').show();    
      });

  }
</script>

And My Example UI Page

enter image description here

Based On selection Of Country and specialization

$users = $country->universities($stream_name,$country_filter);

this function will retrieve university details from database and onclick of add course it will retrieve particular userid and save in database and also i want hide add course button and i want to display course added button but what happening here is.while click on add course it will hide last user_id because script is outside of foreach loop so the value which is getting will be last modified in php.So that what i want to do is.

 $('#added_course<?php echo $user["id"];?>').show();

instead od $user['id'] i want to pass data value return in ajax how can i do this.Please anyone help me to get out of this problem.Thanks in Advance.

  • 写回答

1条回答 默认 最新

  • dongyuzhu2244 2016-05-12 07:23
    关注

    When you click the save_course_list function, now you pass the corresponding id to backed php . Then you just pass this value to ajax response and use

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料