【问题标题】:Submitting a Form to insert SQL with ajax [duplicate]提交表单以使用 ajax 插入 SQL [重复]
【发布时间】:2017-02-02 10:12:49
【问题描述】:

这是我第一次尝试使用 ajax 提交表单,所以我不确定我哪里出错了。

我有一个弹出窗口,显示用户每天第一次访问,它有一个记录出勤的按钮,基本上会插入提交的时间戳以及用户等,我已经手动测试了 PHP,将其取出一个 isset 并且只是点击页面并且它正在插入,但是当我尝试使用 ajax 时它不是,我已经在开发人员工具中观察了网络,并且它在点击时点击了页面但没有插入数据。

这是我的模态:

<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);
}
?>

【问题讨论】:

  • 您错过了 ajax 函数中的数据部分。您没有向 php 页面发布任何内容
  • 我会提交什么数据,是表单名称吗?
  • 你的 php 中的这些 $val 是做什么用的?请贴出真实数据

标签: php sql-server ajax


【解决方案1】:

检查一下

    <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>

log-attendance.php

<?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);
?>

【讨论】:

  • 试过了,我得到了完全相同的结果,ajax 触发了 url 但没有任何反应
  • 现在试试上面的代码...
  • 这行得通,但就像我说它没有包含在 isset 中,所以我不能修改它以在将来添加其他按钮
  • 检查上面的代码现在我提到了这两个事件 1.在提交表单 2.在点击按钮上。
  • @PHPNewbie 为什么需要 isset?你使用 ajax 来提交,因此 ajax 有 on submit 功能。因此在您的 php 上,您无需检查按钮是否被点击
猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 2019-06-04
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 2012-04-04
  • 2018-04-01
  • 1970-01-01
相关资源
最近更新 更多