[英]Ajax jQuery MySql Delete field
I have a fields of phone numbers that are pulled from the database through ajax and what I'm trying to do is add a delete button next to the info Here is what I have so far it seems this should be working but it is not. 我有一个电话号码字段,这些电话号码是通过ajax从数据库中提取的,而我想做的是在信息旁边添加一个删除按钮。这是我目前为止似乎应该可以使用的功能,但事实并非如此。 I'm also using bootstrap. 我也在使用引导程序。
HTML CODE: HTML代码:
<input type="hidden" value="<?php echo _e($_SESSION['id']); ?>" id="userid">
<div class="row">
<div class="col-md-4">
<p>',_e($r->label),'</p>
</div>
<div class="col-md-6">
<p>',_e($r->phone),'</p>
</div>
<div class="col-md-2">
<a class="delPhone" rel="',_e($r->id),'" href="#">
Delete Number
</a>
</div>
</div>
AJAX CODE AJAX代码
$('.delPhone').on('click', function() {
$id = $('#userid').val();
$.ajax({
type: 'POST',
url: "functions.php",
data: {"userid":$id, "phoneid": $(this).attr('rel')},
success: function(data) {
console.log(data);
}
});
});
PHP CODE PHP代码
if(isset($_POST['phoneid']) === true && isset($_POST['userid']) === true) {
if(empty($_POST['phoneid']) === false && empty($_POST['userid']) === false) {
$phoneid = $_POST['phoneid'];
$userid = $_POST['userid'];
require_once 'connect.php';
$sql = "DELETE FROM numbers WHERE userid=:userid AND id=:phoneid";
$query = $handler->prepare($sql);
$query->execute(array(
':userid' => $userid,
':phoneid' => $phoneid
));
}
}
Solution: 解:
The problem is that you are NOT specifying the value of $id in you AJAX which is why PHP doesn't know what to delete. 问题是您没有在AJAX中指定$ id的值,这就是PHP不知道要删除什么的原因。
Error message: Uncaught ReferenceError: $id is not defined 错误消息: 未捕获ReferenceError:未定义$ id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.