简体   繁体   English

动态表格并通过jquery和ajax发送到mysql

[英]Dynamic form and send to mysql via jquery and ajax

I have already asked some questions about this, I was helped, but kind of the way they said it only works if the form is normal, with inputs with name "something here" my form only has an input "text" name "table" to put The table number the rest of the form comes via mysql ajax. 我已经对此提出了一些问题,我得到了帮助,但是在某种程度上,他们说只有在表单正常的情况下它才起作用,输入名称为“ something here”,而我的表单只有输入“ text”,名称为“ table”把表号的其余部分通过mysql ajax提交。 My question is how can I pass this form to the database since it is dynamic? 我的问题是,由于它是动态的,如何将其传递给数据库? My code below. 我的代码如下。

My form 我的表格

<div class="well">


                <!-- left -->
                <div id="theproducts" class="col-sm-5">
                </div>
                <!-- left -->
                <form method="post" action="relatorio.php" id="formRel">
                <span>Mesa</span>
         <input type="text" id="numero_mesa" name="numero_mesa">
                <input type="text" id="theinputsum">

                <!-- right -->
                <div id="thetotal" class="col-sm-7">
                   <h1 id="total"></h1>
                   <button type="submit" class="btn btn-lg btn-success btn-block"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Finalizar Pedido</button>
                </form>
                </div>
               <!-- right -->


            </div>

And the javascript code. 和javascript代码。

<script>
$('#formRel').submit(function(event){
        event.preventDefault();
        var formDados = new FormData($(this)[0]);
$.ajax({
  method: "POST",
  url: "relatorio.php",
  data: $("#formRel").serialize(),
  dataType : "html"
})
};
 </script>

The insert query is like this. 插入查询就是这样。

<?php
error_reporting(-1);
ini_set('display_errors', 'On');


//Criar a conexao
$link = new mysqli ("localhost", "root", "", "restaurant");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}
//echo "<pre>"; print_r($_POST); exit;
$mesa = $_POST['numero_mesa'];
$pedido = $_POST['products'];
$preco = $_POST['products'];


$sql = "INSERT INTO spedido ('pedido','preco','numero_mesa') VALUES ('$mesa','$pedido','$preco')";

$link->query($sql);





?>

enter image description here 在此处输入图片说明

<script>
$(document).on('submit', '#formRel', function(event) {
    event.preventDefault();
    var numero_mesa = $('#numero_mesa').val();
    $.ajax({
        type: "POST",
        url: "relatorio.php?",
        data: "numero_mesa="+ numero_mesa+
        "&products="+ products,
        success: function (data) {
            alert(data) // this will send you a message that might help you see whats going on in your php file
        });
    })
};
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM