我想将点击的按钮ID插入到我的数据库中,但是我无法捕获正确的id,可能是我的jQuery / ajax语法或其他任何我看不到的问题。
我们来看看我的代码
PHP登陆页面:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Insert</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($) {
$('#btn').click(function() {
var btnid = $(this).attr('name');
console.log(btnid);
$.ajax({
type: "POST",
url: "insert_test.php",
data: btnid,
success: function(html){
$('#response').html(html);
}
});
});
});
</script>
</head>
<body>
<div name="001" id="btn">Click me to send Data!</div>
<div id="response"></div>
</body>
</html>
PHP插入文件
<?php
$con = mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
$hostname = gethostname();
$ipadress = $_SERVER['HTTP_HOST'];
$btnid = $_POST['btnid'];
$page = $_SERVER['PHP_SELF'];
$sql = "INSERT INTO lovecounter (ip,hostname,id,page) VALUES ('$ipadress', '$hostname', '$btnid', '$page')";
if (!mysqli_query($con,$sql))
{
die('funzt nich weil: ' . mysqli_error($con));
}
echo "funzt!";
mysqli_close($con);
?>
ajax帖子工作正常但不发送正确的数据 当我检查我的数据库时,除了应该识别单击按钮的btnid之外,所有内容都正确地着陆。我做错了什么?
错误消息:注意:未定义的索引:btnid在C:\ Users ...
中