dou1908 2014-03-30 22:05
浏览 77
已采纳

如何将php变量传递给ajax函数

How can I pass id="'.$row["courseid"].'" into the ajax function,

I'm trying data:{'courseid':deleteId}, but is not working any ideas on how to fix this problem.

    <?php


echo "<table width='100%'>";
echo "<tr> 
      <th>Course name</th> 
      <th>Delete</th>
      <th>Edit</th>
      </tr>";
?>
    <?php foreach($rows as $row):  
    echo "<tr>"; 
    echo '<td><a href="#">' . htmlentities($row['coursename'], ENT_QUOTES, 'UTF-8') . '</a></td>';
    echo '<td><button onclick="deleteC(' . $row['courseid'] . ')");"><font color="#e70404"> Delete </font> </button></td>';
    echo '<td><a class="delete" id="'.$row["courseid"].'">Delette</a></td>';
    echo "</tr> ";
    endforeach; 
echo "</table>";

?>

And this is the ajax function which is in the same page

<script type="text/javascript">
function deleteC(deleteId){
   $.ajax({
   type: "GET",
   url: "deleteCourse.php",
   data:{'courseid':deleteId},
   success: function(result){               
       if(result=='correct'){
           window.location='index.php';
       }else {
       window.location='coursesData.php';
       }
   }
});
}
</script>

This is the deleteCourse.php

   <?php 
require("connect.php");
if (isset($_GET['courseid']) && is_numeric($_GET['courseid']))
{ 
    $id = $_GET['courseid'];

    echo"$courseid"; 
    $con=mysqli_connect("localhost","root","","independentstudyclass");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    mysqli_query($con,"DELETE FROM courses WHERE courseid=$id");
    echo "correct";
    mysqli_close($con);
    echo "correct";
}
else 
{
    header ("Location: ../index.php");
}


?> 
  • 写回答

1条回答 默认 最新

  • dongshan1036 2014-03-30 22:12
    关注

    Instead of this:

    echo '<td><a href="finalphp/deleteCourse.php?id=' . $row['courseid'] . '" onclick="deleteC()");"><font color="#e70404"> Delete </font> </a></td>';
    

    Just pass the id to the JS function as a parameter and use it:

    PHP:

    echo '<td><a href="finalphp/deleteCourse.php?id=' . $row['courseid'] . '" onclick="deleteC(' . $row['courseid'] . ')");"><font color="#e70404"> Delete </font> </a></td>';
    

    JS

    function deleteC(deleteId){
       $.ajax({
       type: "GET",
       url: "deleteCourse.php",
       data:{'courseid':deleteId},
       success: function(result){               
           if(result=='correct'){
               window.location='index.php';
           } else {
    
           }
       }
    });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口