jquery ajax插入查询不起作用

时间:2015-07-05 06:05:11

标签: javascript php jquery mysql ajax

我正在使用一个Web应用程序,每当用户单击“创建采购订单”按钮时,将显示一个表单,单击时将禁用采购订单按钮并对数据库执行插入查询。我已经使用ajax来防止浏览器在点击按钮时重新加载。但是,我的问题是,当我单击按钮时,它没有将任何数据插入数据库。我的代码有问题吗?

的index.php

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>this is a test</title>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<script type="text/javascript" src="bootstrap/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
    function disableCreatePoButton(form){
        form.btnCreatePo.disabled="true";
    }

    $(function(){
        $('#createPo').on('submit','', function(event){
            event.preventDefault();
            $.ajax({
                data: $('#createPo').serialize(),
                type: 'post',
                url: 'createPurchaseOrderId.php',
                //dataType: 'json',
                success: function(){
                    alert('success!');
                }
            });
        });
    });
</script>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="createPo" onSubmit="return disableCreatePoButton(this);">
    <input type="submit" name="btnCreatePo" class="btn btn-primary" value="Create Purchase Order" id="btnCreatePo" data-toggle="collapse" data-target="#collapsePurchaseForm" aria-expanded="false" aria-controls="collapsePurchaseForm">
</form>
<div class="collapse" id="collapsePurchaseForm">
<div id="purchase_form" class="well well-default">
    <?php
        require('connect.php');
        $getMaxPOID = "SELECT max(`po_id`) AS `max_po_id` FROM `tbl_poid`";
        $getMaxPOIDResult = mysql_query($getMaxPOID);
        $maxPOIDRow = mysql_fetch_array($getMaxPOIDResult);
        $maxPOID = $maxPOIDRow['max_po_id'] + 1;
        echo '<h4>Purchase Order ID: '.$maxPOID.'</h4>';
        mysql_close($connection);
    ?>
    <h2>Add a Product</h2>
    <input type="text" placeholder="Product Name" class="form-control"><br>
    <input type="text" placeholder="Quantity" class="form-control"><br>
    <input type="text" placeholder="Description" class="form-control"><br><br>
    <button id="btnAddProduct" class="btn btn-success">Add Product</button>
</div>
</div>
<table class="table table-bordered">
<thead>
    <th>Purchase Order ID</th>
    <th>Product ID</th>
    <th>Product Name</th>
    <th>Quantity</th>
    <th>Description</th>
</thead>
<tr>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
    <td>data</td>
</tr>
</table> 
</body>
</html>

createPurchaseOrderId.php

<?php
if(isset($_REQUEST)){
    require('connect.php');
    date_default_timezone_get('Asia/Manila');
    $date = mysql_real_escape_string(date("M d, Y h:i:s a"));
    $enc_date = md5($date);
    if(isset($_POST['btnCreatePo'])){
        $insertPOIDQuery = "INSERT INTO tbl_poid (`po_key`) VALUES ('$enc_date')";
        $insertPOIDResult = mysql_query($insertPOIDQuery);
    }
    mysql_close($connection);
}
?>

0 个答案:

没有答案