使用 AJAX 从 MySQL 数据库中删除值而无需重新加载页面(已编辑)

问题描述:

我正在使用通过 ID 系统删除.用户输入他/她希望删除的记录的 ID,并通过 PHP 完成.这一切正常,成功删除后,一个小警告框确认了这一点,并返回到原始页面,显示记录不再存在.但是,我想以不刷新页面并将表格保留在屏幕上的方式使用 AJAX.

I am using delete by ID system. The user enters the id of the record he/she wishes to remove and is done so through PHP. This is all working and when successfully removed a little alert box confirms this and is brought back to the original page, which shows the record is no longer there. However, I would like to use AJAX in a way that does not refresh the page and keeps the table on screen.

<head>
<title>ajax_test</title>
<script language="javascript" type="text/javascript" src="prototype.js"></script>
<script>
function updateDIV(myDiv)
{
var url='delete.php';
var params = 'id';
var myAjax = new Ajax.Updater(myDiv, url, {method: 'get', parameters: params});
}
</script>
</head>




<body>
    <form action="delete.php" method="get">
        <input type="text" name="id" placeholder="ID" />
        <input type="submit" value="Delete" onclick="updateDIV(targetDiv)" />
        <div id="targetDiv"></div>
    </form>
</body>

正如您可能看到的,这行不通.有什么明显的问题需要我进一步研究吗?

As you can probably see, this is not working. Is there any blatant problems that I should be researching further?

将updateDIV函数改成这样:

Change the updateDIV function to this :

<script type="text/javascript">
function updateDIV(myDiv)
{
   var url='delete.php';
   var id = $F($('id'));
   var myAjax = new Ajax.Updater(myDiv, url, {method: 'get', parameters: 'id='+id});
}
</script>

并在您的输入中添加一个 id 值:

and add an id value to your input :

<body>
    <form action="delete.php" method="get">
        <input type="text" id="id" name="id" placeholder="ID" />
        <input type="submit" value="Delete" onclick="updateDIV(targetDiv)" />
        <div id="targetDiv"></div>
    </form>
</body>

$F 是一个获取元素值的原型函数.然后在你的 php 代码中,你可以通过 $_GET['id'] 获取 id.

$F is a prototype function which get the value of an element. Then on your php code, you can get the id with $_GET['id'].