1

我想将表单提交到 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);
}
?>
4

4 回答 4

1

尝试这个:

  $.post("test.php", { category_id: category_id, category_name: category_name })
      .done(
           function(data) {
               alert("Finish!");
           }
      );
于 2013-03-19T14:25:04.037 回答
1

你在那里放了三个参数,它应该是两个。你可以试试这个:

<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 })
    .done(function() {
      alert("Finish!");
    });
    }
</script>

祝你好运!

于 2013-03-19T14:28:11.987 回答
0

尝试这个

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 })
    .done(function(data) {
           alert("Finish!");
    });
}

文档http://api.jquery.com/jQuery.post/

于 2013-03-19T14:19:48.710 回答
0

试试这个解决方案

    function SubmitForm() {
        var category_id = $("#category_id").val();
        var category_name = $("#category_name").val();
        $.ajax({
               type: "POST",
               url: "test.php", 
               data: { category_id: category_id, category_name: category_name }
        }).done (function() {
             alert("Finish");
        }).fail (function(status) {
            console.log(status);          
        });

现在检查数据,看看出了什么问题。

于 2013-03-19T14:36:08.143 回答