Php 提交表单以使用ajax插入SQL
这是我第一次尝试使用ajax提交表单,因此我不确定哪里出了问题 我有一个弹出窗口,显示用户每天第一次访问时的情况,它有一个记录出席情况的按钮,基本上会插入提交的时间戳以及用户等等,我已经手动测试了PHP,将其从isset中取出,只需点击页面就可以插入,但当我尝试使用ajax时,它不是,我在开发者工具中观看了网络,它在点击页面时点击,但没有插入数据 这是我的情态:Php 提交表单以使用ajax插入SQL,php,sql-server,ajax,Php,Sql Server,Ajax,这是我第一次尝试使用ajax提交表单,因此我不确定哪里出了问题 我有一个弹出窗口,显示用户每天第一次访问时的情况,它有一个记录出席情况的按钮,基本上会插入提交的时间戳以及用户等等,我已经手动测试了PHP,将其从isset中取出,只需点击页面就可以插入,但当我尝试使用ajax时,它不是,我在开发者工具中观看了网络,它在点击页面时点击,但没有插入数据 这是我的情态: <form method="post" data-remote="true"> <div class="modal
<form method="post" data-remote="true">
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel2">Morning, <?php echo $user['userName']; ?>.</h4>
</div>
<div class="modal-body">
<?php $today = date("D M j"); ?>
<h4>Are you working today <?php echo $today; ?>?</h4>
<p>By clicking 'Log My Attendance' you are confirming that you are working today.</p>
</div>
<div class="modal-footer">
<button id="log-attendance" type="buttom" name="submit" class="btn btn-primary" data-dismiss="modal">Log My Attendance</button>
</div>
</div>
</div>
</div>
</form>
这是我的javascript:
<script type="text/javascript">
$(document).on('click', '#log-attendance', function() {
$.ajax({
type: "POST",
url: 'log-attendance.php',
success: function() {
"Attendance Log success"
}
});
});
</script>
这是我的PHP:
<?php if(isset($_POST['submit'])) {
$val1 = "test";
$val2 = "test";
$val3 = "test";
$val4 = "test";
$val5 = "test";
$val6 = "test";
$query = "INSERT INTO po_users (val1, val2, val3, val4, val5, val6)
VALUES ('$val1', '$val2','$val3', '$val4', '$val5', '$val6')";
$stmt = sqlsrv_prepare($sapconn2, $query);
sqlsrv_execute($stmt);
}
?>
检查这个
log-attention.php
尝试了这个,我得到了完全相同的结果,ajax触发了url,但现在没有发生任何事情尝试上面的代码…这是可行的,但就像我说的,它没有包装在isset中,所以接下来我不能修改它以在将来添加其他按钮检查上面的代码现在我提到了这两个事件1。在提交表单2中。单击按钮。@PHPNewbie为什么需要isset?u使用ajax提交,因此ajax具有提交功能。因此,在您的php上,您不需要检查按钮是否被单击,因为您错过了ajax函数中的数据部分。您没有在php页面上发布任何内容。我将随数据一起提交什么,它是表单名称吗?这些$val在我们的php中用于什么?请张贴真实数据
<form method="post" data-remote="true" id="myform">
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel2">Morning, <?php echo $user['userName']; ?>.</h4>
</div>
<div class="modal-body">
<?php $today = date("D M j"); ?>
<h4>Are you working today <?php echo $today; ?>?</h4>
<p>By clicking 'Log My Attendance' you are confirming that you are working today.</p>
</div>
<div class="modal-footer">
<button id="log-attendance" type="buttom" name="submit" class="btn btn-primary" data-dismiss="modal">Log My Attendance</button>
</div>
</div>
</div>
</div>
</form>
/* On form submit */
<script>
$(function() {
$('#myform').submit(function(e) {
e.preventDefault();
//var formdata = $(this).serialize();
$.ajax({
url : 'log-attendance.php',
method : 'POST',
success : function(res)
{
alert(res);
}
});
});
});
/* on button click */
$(function() {
$('#log-attendance').click(function(e) {
e.preventDefault();
//var formdata = $(this).serialize();
$.ajax({
url : 'log-attendance.php',
method : 'POST',
success : function(res)
{
alert(res);
}
});
});
});
</script>
<?php
$servername = "localhost";
$username = "username"; /* Your username */
$password = "password"; /* Your password */
$dbname = "myDB"; /* Your dbname */
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$val1 = "test";
$val2 = "test";
$val3 = "test";
$val4 = "test";
$val5 = "test";
$val6 = "test";
$query = "INSERT INTO po_users (val1, val2, val3, val4, val5, val6)
VALUES ('".$val1."', '".$val2."','".$val3."', '".$val4."', '".$val5."', '".$val6."')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>