如何使用值调用php函数? html.php
如何在单击按钮时调用Delete$passid函数如何使用值调用php函数? html.php,php,Php,如何在单击按钮时调用Delete$passid函数 <?php function Delete($passid) { echo "Delete $passid"; } ?> <button name="Delete" id="Delete" onclick="Delete($row['ID'])" >Delete</button> 您必须使用表单和提交
<?php
function Delete($passid)
{
echo "Delete $passid";
}
?>
<button name="Delete" id="Delete" onclick="Delete($row['ID'])" >Delete</button>
您必须使用表单和提交按钮发布数据,然后使用$\u POST['passid']中的参数调用此函数
使用AJAX post数据并处理相同的选项1
还可以使用ajax-->我想这就是你想要的假设您正在尝试使用javascript编辑DOM
<input type="button" onclick="Delete($row['ID'])" name="delete" value="Log In" />
<script>
function Delete(e) {
alert(e);
$.ajax({
type: "POST",
url: "script.php",
data:{"delete":e},
success: function(data) {
if (data) {
alert(data);
}
else {
alert('Successfully not posted.');
}
}
});
}
</script>
JS
HTML
JQuery
PHP-->file.PHP运行函数
<?php
if(isset($_POST["id"]))
{
$id = $_POST["id"];
$query = mysql_query("DELETE FROM table WHERE id='$id'");
}
?>
这将删除数据库表行,其中id设置为我们从HTML按钮的值传递给它的id
因此,此函数执行以下操作:
从满足条件的数据库表中获取所有ID
使用span标记填充HTML页面,该标记向我们显示将删除同一元素的按钮旁边的元素id
单击按钮时,jQuery click事件将捕获它
jQuery函数获取单击按钮的id并将其发送给ajax函数
Ajax函数使用post方法将变量id发送到document file.php
php检查通过post方法发送的变量id是否实际存在
如果post变量id存在,它将为其设置$id。
调用Query删除数据库中的一个表行,其中id等于表本身生成的初始按钮的id值$id
不能在onclick事件上调用php函数,就像在服务器端呈现此php一样,在呈现后不能调用此函数。删除需要的ajax代码。。。我将编辑我的优秀教程
if(isset($_POST))
{
$id=$_POST['delete'];
echo "Delete ".$id;
}
function Delete(passid)
{
//Do something with passid variable
}
<button id="Delete" onclick="Delete(this.id)" >Delete</button>
<?php
$query = mysql_query("SELECT * FROM table WHERE condition");
while($row = mysql_fetch_array($query))
{
echo '<span>'.$row["id"].'</span>
<button class="Delete" value='.$row["id"].'>Delete</button>';
}
?>
$(".Delete").click(function()
{
var id = $(this).attr("id");
$.ajax({
type:"POST",
url:"file.php",
data:{id:id}
});
});
<?php
if(isset($_POST["id"]))
{
$id = $_POST["id"];
$query = mysql_query("DELETE FROM table WHERE id='$id'");
}
?>