2014-03-30 27 views
-1

如何传递id="'.$row["courseid"].'"到AJAX功能,如何传递PHP变量到AJAX功能

我想data:{'courseid':deleteId},但不工作,就如何解决这个问题的任何想法。

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

?> 

这是Ajax的功能,这是在同一个页面

<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> 

这是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"); 
} 


?> 
+0

'url'属性应包含在你的'$ .ajax'对象字符串''deleteCourse.php''。顺便说一下,你能告诉我们你是如何将'courseid'传递给ajax处理程序的吗? –

+0

这就是我的主要问题,我不知道如何通过它,实际上是。$ row [“courseid”]我想要传入ajax – user3424633

回答

0

取而代之的是:

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

只需将该ID作为段落传递给JS函数即可计和使用它:

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 { 

     } 
    } 
}); 
} 
+0

谢谢你,现在我的函数正在工作,但它似乎不是读取检测。 $ row ['courseid']有没有办法将这段代码传入ajax – user3424633

+0

courseid是一个整数吗? – nXu

+0

谢谢你这是我的deleteCourse文件中的错误,现在正在工作。 – user3424633