php - 使用 jQuery 向 MySQL 表中插入数据

标签 php mysql jquery

我正在努力让它发挥作用,但我遇到了问题。我在这里缺少什么?

我正在尝试通过 jQuery 将一些数据插入到本地 MySQL 表中。如果我自己运行 save.php,它会在数据库中插入一个空白行,这样就可以了。有什么想法吗?

**index.php**

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/style.css" />

        <script src="jquery.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function() {
                $('form#submit').submit(function () {
                    var nume = $('#nume').val();
                    $.ajax({
                        type: "POST",
                        url: "save.php",
                        data: "nume="+ nume,
                        success: function() {
                            $('#nume').val('');
                        }
                    });
                return false;
                });
            });
        </script>
    </head>

    <body>

        <form id="submit" method="post">
            <p>Nume: <input id="nume" name="nume"  type="text"></p>
            <p><input id="submitButton" type="button" value="Submit"></p>
        </form>

    </body>
</html>


**save.php**

<?php
    mysql_connect('localhost', 'root', 'root') or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());

    $nume = htmlspecialchars(trim($_POST['nume']));

    $add = "INSERT INTO ajax_test (nume) VALUES ('$nume')";
    mysql_query($add) or die(mysql_error());
?>

最佳答案

您确定表单正在提交吗?您的按钮不是 input type="submit"- 它只是一个按钮。它不会自行提交表单。

关于php - 使用 jQuery 向 MySQL 表中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1759577/

相关文章:

php - Symfony 和 AJAX 404 响应

php - 在使用 jQuery $.ajax PHP/MySQL 插入后添加表行

mysql - PHP SQL IF NOT EXISTS 将不起作用

jquery - 如何让一个元素始终占据其父元素的特定百分比,即使在调整浏览器大小时也是如此?

javascript - jQuery 在按钮后追加

php - 如何在 Joomla 中使用 php 页面?

php - 如何交换php变量字符

php - 无法在谷歌云上使用 PHP 连接到 mysql。没有错误弹出

php - 将数组保存到mysql数据库中

javascript - 选择除指定类之外的所有 html 元素