【问题标题】:Using Ajax with a php site [closed]将 Ajax 与 php 站点一起使用 [关闭]
【发布时间】:2021-10-20 13:13:22
【问题描述】:

所以我有一个网站,它有三个表单用于将数据添加到我的数据库中,每次我提交表单时页面都会刷新,我听说我应该使用 AJAX 来完成,而不需要页面自行刷新。谁能指出我应该使用什么?

【问题讨论】:

  • 是的,你应该使用 AJAX。

标签: php ajax xml


【解决方案1】:

这是使用 AJAX 向数据库添加数据的演示示例。

form.php


<html>
<head>
    <title>Php submit for using Ajax</title>
</head>
<body>
    <form>
        <input type="text" name="name"><br>
        <input type="email" name="email"><br>
        <input type="number" name="contact"><br>
        <button type="button" id="submit">Submit</button>
    </form>
</body>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    $(document).ready(function () {
        $('#submit').click(function () {
            $.ajax({
                type: 'post',
                url: 'post.php',
                data: $('form').serialize(),
                success: function (data) {
                    alert(data);
                }
            });
            return false;
        });
    });
</script>
</html>


post.php



<?php

//incluce db connection file

if(isset($_POST["name"]) || isset($_POST["email"]) || isset($_POST["contact"])) {
    
    //Data Insert Login Here

    echo 'response here';//Message to be shown on success
}

?>

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多