Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery代码正在阻止将产品插入数据库_Javascript_Php_Jquery_Mysql - Fatal编程技术网

Javascript jQuery代码正在阻止将产品插入数据库

Javascript jQuery代码正在阻止将产品插入数据库,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,当我包含$.post jQuery代码时,产品信息不会插入到数据库中。但是,当我删除$.post jQuery代码时,产品已成功插入数据库。请提供帮助 index.html <html> <body> <h1>Enter Product Details</h1> <form id = "product_form" method = "post" action = "insert.php">

当我包含$.post jQuery代码时,产品信息不会插入到数据库中。但是,当我删除$.post jQuery代码时,产品已成功插入数据库。请提供帮助

index.html

<html>

    <body>
        <h1>Enter Product Details</h1>
        <form id = "product_form" method = "post" action = "insert.php">
            <input type = "text" id = "n" name = "productname" placeholder = "Product name"></br>
            <input type = "text" id = "b" name = "brandname" placeholder = "Brand name"></br>
            <input type = "number" id = "q" name = "quantity" placeholder = "Quantity"></br>
            <button id = "save_button">Save information</button>
        </form>

        <p id = "result"></p>

        <button id = "btn">Test Button</button>


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

</html>

在$.post的第二个参数中,您设置了要发送到服务器的数据,您有

{
  productname:$("#n").val(),
  brandname:$("#b").val(),
  value:$("#q").val()
}

因此,您需要将
值:
更改为
数量:
,以使ajax提交与传统表单提交匹配。

我认为您需要
type=“submit”
按钮,使该代码工作
保存信息
否则按钮将不会生成提交event@RiggsFolly我按下id=“保存”按钮。但是,它仍然没有插入。是的,但是按钮不会生成提交事件,没有
type=“submit”
,并且您正在尝试捕获提交事件。您是否在devtools中看到ajax请求并收到“Error!”消息?@Riggs我很抱歉,我的意思是我放置了type=“submit”。但是,仍然没有插入到数据库中。
$(document).ready(function(){

    $('#btn').click(function(){
        alert();
    });

    //The problem code!!!!
    $('#product_form').submit(function(event){
        event.preventDefault();     
        $.post(
            'insert.php',
            {
                productname:$("#n").val(),
                brandname:$("#b").val(),
                value:$("#q").val(),
            },
            function(result){
                if(result == "success"){
                    $("#result").html("Values inserted successfully!");
                }
                else{
                    $("#result").html("Error!");
                }
            }
        );
    }); 
});
{
  productname:$("#n").val(),
  brandname:$("#b").val(),
  value:$("#q").val()
}