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 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀