javascript - 没有刷新页面的 HTML 提交表单并显示警告框

标签 javascript php html forms alert

我想将表单提交到php,然后数据将更新到数据库并显示警告框。 现在,数据已经更新了数据库,但警告框没有出现。 我的代码有什么问题?? 谢谢。

测试.html

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        function SubmitForm() {
        var category_id = $("#category_id").val();
        var category_name = $("#category_name").val();
        $.post("test.php", { category_id: category_id, category_name: category_name },
           function(data) {
             alert("Finish!");
           });
        }
    </script>
</head>
<body>

    <form action="test.php" method="post">
        category_id: <input type="text" name="category_id" id="category_id"/>
        category_name: <input type="text" name="category_name" id="category_name"/>
        <input type="button" id="searchForm" onclick="SubmitForm();" value="Send" />
    </form> 
</body>
</html>

测试.php

<?php

$a = $_POST['category_id'];
$b = $_POST['category_name'];


$link = mysql_connect('localhost','root',''); 
mysql_select_db("fyp", $link);
$sql = "INSERT INTO category (CID, Category) VALUES (".
         PrepSQL($a) . ", " .
         PrepSQL($b) . ")";

mysql_query($sql);

function PrepSQL($value)
{
    // Stripslashes
    if(get_magic_quotes_gpc())
    {
        $value = stripslashes($value);
    }

    // Quote
    $value = "'" . mysql_real_escape_string($value) . "'";

    return($value);
}
?>

最佳答案

试试这个:

  $.post("test.php", { category_id: category_id, category_name: category_name })
      .done(
           function(data) {
               alert("Finish!");
           }
      );

关于javascript - 没有刷新页面的 HTML 提交表单并显示警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15501988/

相关文章:

javascript - Node.js MySQL 连接问题

html - War 文件不会显示图像/CSS/JS/HTML

html - 如何将绝对定位的图像垂直对齐到另一个图像之上?

html - 在 CSS 中突出显示右边距较短的文本

javascript - 传单 - 标记未显示

javascript - 根据选择打印多个输入字段

php - preg_match 中断超过 5100 个字符

php - mysql_real_escape_string() 允许 ' and "

javascript - 检查checkbox是否勾选并传递数据

javascript - 如何分配 JSON 对象