【问题标题】:Cant get data from form sent to mysql database无法从发送到mysql数据库的表单中获取数据
【发布时间】:2019-09-22 08:55:24
【问题描述】:

有人可以帮忙吗,我似乎无法从要发送到 mysql 数据库的表单中收集数据。

我对编码很陌生,我似乎无法弄清楚为什么表单数据没有被发送到 mysql 数据库表。

如有任何帮助,将不胜感激。 一旦我按下提交页面关闭而不是刷新没有任何错误,但数据尚未发送到数据库表。

请看下面的代码。

<?php include'inc/header.php'; ?>
<div class="container">

    <center>
        <h2 style="color: #odc16f">Shipped</h2>
        <hr>
    </center>

    <center>
        <!-- Button trigger modal -->
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#addEmpModal">
            Add Order
        </button>
    </center>

    <!-- Modal -->
    <div class="modal fade" id="addEmpModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="" method="post">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria- hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Add New Order</h4>
                    </div>
                    <div class="modal-body">

                        <div class="form-group">
                            <label>Enter Name</label>
                            <input class="form-control" type="text" name="customer" id="customer" placeholder="Enter Name">
                            <label id="lbcustomer" style="color:red"></label>
                        </div>

                        <div class="form-group">
                            <label>Enter Date</label>
                            <input class="form-control" type="date" name="date" id="date" placeholder="Enter Date">
                            <label id="lbdate" style="color:red"></label>
                        </div>

                        <div class="form-group">
                            <label>Enter Invoice</label>
                            <input class="form-control" type="number" name="invoice" id="invoice" placeholder="Enter Invoice">
                            <label id="lbinvoice" style="color:red"></label>
                        </div>


                        <div class="form-group">
                            <label>Enter eBay</label>
                            <input class="form-control" type="number" name="ebay" id="ebay" placeholder="Enter eBay">
                            <label id="lbebay" style="color:red"></label>
                        </div>

                        <div class="form-group">
                            <label>Enter Shipped</label>
                            <input class="form-control" type="text" name="shipper" id="shipper" placeholder="Enter Shipped">
                            <label id="lbshipper" style="color:red"></label>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="save">Save changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div><!-- /.container ends here -->

<?php
    include'inc/footer.php';
?>
<script>
    $(document).ready(function() {
        $(document).on('click', '#save', function() {

            var customer = $("#customer").val();
            var date = $("#date").val();
            var invoice = $("#invoice").val();
            var ebay = $("#ebay").val();
            var shipper = $("#shipper").val();

            if (customer == "") {
                $("#lbcustomer").html("Enter Name");
            } else if (date == "") {
                $("#lbdate").html("Enter Date");
            } else if (invoice == "") {
                $("#lbinvoice").html("Enter Invoice");
            } else if (ebay == "") {
                $("#lbebay").html("Enter eBay");
            } else if (shipper == "") {
                $("#lbshipper").html("Enter Shipper");
            } else {
                $.ajax({
                    url: "save_data.php",
                    type: "post",
                    data: {
                        customer: customer,
                        date: date,
                        invoice: invoice,
                        ebay: ebay,
                        shipper: shipper
                    },
                    success: function(data) {
                        alert("Order Has Been Successful");
                        $("#addEmpModal").modal('hide');
                        location.reload();
                    }
                });
            }

        });
    });

</script>

请看下面的save_data.php代码

<$php
include 'config/config.php';
global $con;

   $customer = $_POST['customer'];
   $date = $_POST['date'];
   $invoice = $_POST['invoice'];
   $ebay = $_POST['ebay'];
   $shipper = $_POST['shipper'];

$save_data = "INSERT INTO orders(customer, date, invoice, ebay, shipper)VALUES('$customer','$date','$invoice','$ebay','$shipper')";
$result = mysqli_query($con, $save_data);

下面是config.php代码。

<?php

$con = mysqli_connect("localhost","root","Password","shippedorders");
if (!$con) {
  echo "Failed to connect to MySQL: ".mysqli_connect_error($con);
  } 

【问题讨论】:

标签: javascript php html mysql ajax


【解决方案1】:

你不见了

动作:

<form action="save_data.php" method="post">

或者您是否在与 html 相同的页面中运行您的 php?或者你

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2019-06-16
    • 2021-08-21
    • 2017-01-05
    • 2011-07-02
    • 2023-04-05
    相关资源
    最近更新 更多