[英]How can I pass php variable into ajax function
How can I pass id="'.$row["courseid"].'"
into the ajax function,如何将
id="'.$row["courseid"].'"
传入 ajax 函数,
I'm trying data:{'courseid':deleteId}
, but is not working any ideas on how to fix this problem.我正在尝试
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>";
?>
And this is the ajax function which is in the same page这是同一页面中的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>
This is the deleteCourse.php这是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");
}
?>
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:只需将 id 作为参数传递给 JS 函数并使用它:
PHP: PHP:
echo '<td><a href="finalphp/deleteCourse.php?id=' . $row['courseid'] . '" onclick="deleteC(' . $row['courseid'] . ')");"><font color="#e70404"> Delete </font> </a></td>';
JS JS
function deleteC(deleteId){
$.ajax({
type: "GET",
url: "deleteCourse.php",
data:{'courseid':deleteId},
success: function(result){
if(result=='correct'){
window.location='index.php';
} else {
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.