在php行中插入多个值

时间:2019-03-07 11:28:57

标签: php jquery mysql ajax

我找不到该公式的解决方案,所以我问你们我该如何解决:(1)我有这个公式:

<form id="formRetur" action="" method="POST" novalidate="novalidate">
        <div id="mail-status" style="color: black;text-align: center;"></div>
        <h3><strong>
            Detalii client
        </strong></h3>
          <div class="row">
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="nume_prenume" id="nume_prenume" placeholder="Nume Prenume">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="nr_tlf" id="nr_tlf" placeholder="Numar telefon">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="adresa_email" id="adresa_email" placeholder="Adresa email">
              </div>
            </div>
          </div>
        <h3><strong>
            Detalii comanda
        </strong></h3>
        <div class="row">
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="nr_comanda" id="nr_comanda" placeholder="Numar comanda">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="data_comanda" id="data_comanda" placeholder="Data comanda">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="nr_factura" id="nr_factura" placeholder="Numar factura">
              </div>
            </div>
        </div>
        <h3><strong>
            Detalii produse
        </strong></h3>
        <div class="row">
            <a href="javascript:void(0)" id="add"><i class="fa fa-plus"  aria-hidden="true" style="position: absolute;z-index:999;"></i></a>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="den_prod" id="den_prod" placeholder="Denumire produs">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="culoare_prod" id="culoare_prod" placeholder="Culoare produs">
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input type="text" class="form-control" name="cantitate" id="cantitate" placeholder="Cantitate">
              </div>
            </div>
        </div>
        <div class="maimulte">

        </div>
        <h3><strong>
            Motiv retur
        </strong></h3>
        <div class="row">
            <div class="col-md-12">
                <textarea name="motiv" id="motiv" class="form-control" cols="30" rows="10"></textarea>
            </div>
        </div>
        <div class="row">
             <div class="col-md-12" style="margin-top:10px;margin-bottom:10px;"><div class="g-recaptcha" data-sitekey="<?php echo SITE_KEY; ?>"></div>  </div>
            <div class="col-md-12">
                <button type="submit" id="send-message" class="btn btn-default">Trimite </button>
            </div>
            <div id="loader-icon" style="display:none;"><img src="images/loader.gif"></div>
        </div>
    </form>

我使用ajax在数据库中插入数据,但是我有一个选项,您可以在用户按+添加具有3个字段的名称,颜色,数量的+1行时添加更多产品,我也想在数据库中插入这些产品是我的ajax:

<script>
$(document).ready(function (e){
    $("#formRetur").on('submit',(function(e){
        e.preventDefault();

        $("#mail-status").hide();
        $('#send-message').hide();
        $('#loader-icon').show();
        $.ajax({
            url: "valid_retur.php",
            type: "POST",
            dataType:'json',
            data: {
            "nume_prenume":$('input[name="nume_prenume"]').val(),
            "nr_tlf":$('input[name="nr_tlf"]').val(),
            "adresa_email":$('input[name="adresa_email"]').val(),
            "nr_comanda":$('input[name="nr_comanda"]').val(),
            "data_comanda":$('input[name="data_comanda"]').val(),
            "nr_factura":$('input[name="nr_factura"]').val(),
            "den_prod":$('input[name="den_prod"]').val(),
            "culoare_prod":$('input[name="culoare_prod"]').val(),
            "cantitate":$('input[name="cantitate"]').val(),
            "motiv":$('textarea[name="motiv"]').val(),
            "g-recaptcha-response":$('textarea[id="g-recaptcha-response"]').val()},             
            success: function(response){
                // $( '#frmContact' ).each(function(){
                //     this.reset();
                // });
            $("#mail-status").show();
            $('#loader-icon').hide();
            if(response.type == "error") {
                $('#send-message').show();
                $("#mail-status").attr("class","alert alert-danger");               
            } else if(response.type == "message"){
                // grecaptcha.reset();
                $('#send-message').show();
                $("#mail-status").attr("class","alert alert-success");                          
            }
            $("#mail-status").html(response.text);  
            },
            error: function(){} 
        });
    }));
});
</script>

这是我的数据库:enter image description here

还有一个我的php文件,它使用来自ajax的值插入db:

 <?php
 if($_POST)
 {
 include('config.php');

$nume_prenume      = filter_var($_POST["nume_prenume"], FILTER_SANITIZE_STRING);
$nr_tlf      = filter_var($_POST["nr_tlf"], FILTER_SANITIZE_STRING);
$adresa_email     = filter_var($_POST["adresa_email"], FILTER_SANITIZE_STRING);
$nr_comanda     = filter_var($_POST["nr_comanda"], FILTER_SANITIZE_STRING);
$data_comanda     = filter_var($_POST["data_comanda"], FILTER_SANITIZE_STRING);
$nr_factura     = filter_var($_POST["nr_factura"], FILTER_SANITIZE_STRING);
$den_prod     = filter_var($_POST["den_prod"], FILTER_SANITIZE_STRING);
$culoare_prod     = filter_var($_POST["culoare_prod"], FILTER_SANITIZE_STRING);
$cantitate     = filter_var($_POST["cantitate"], FILTER_SANITIZE_STRING);
$motiv     = filter_var($_POST["motiv"], FILTER_SANITIZE_STRING);



if(empty($nume_prenume)) {
    $empty[] = "<b>nume_prenume</b>";       
}
if(empty($nr_tlf)) {
    $empty[] = "<b>nr_tlf</b>";
}   
if(empty($adresa_email)) {
    $empty[] = "<b>adresa_email</b>";
}
if(empty($nr_comanda)) {
    $empty[] = "<b>nr_comanda</b>";
}
if(empty($data_comanda)) {
    $empty[] = "<b>data_comanda</b>";
}
if(empty($den_prod)) {
    $empty[] = "<b>data_comanda</b>";
}
if(empty($culoare_prod)) {
    $empty[] = "<b>data_comanda</b>";
}





if(!empty($empty)) {
    $output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' obligatoriu!'));
    die($output);
}

// if(!filter_var($email2, FILTER_VALIDATE_EMAIL)){ //email validation
//     $output = json_encode(array('type'=>'error', 'text' => '<b>'.$email2.'</b> is an invalid Email, please correct it.'));
//  die($output);
// }

//reCAPTCHA validation
if (isset($_POST['g-recaptcha-response'])) {

    require('component/recaptcha/src/autoload.php');        

    $recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());

    $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

      if (!$resp->isSuccess()) {
            $output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
            die($output);               
      } 
}


// bag in db


$sql0 = "INSERT INTO retur (nume_prenume, nr_tlf, email, nr_comanda, data_comanda, nr_factura, den_prod, culoare_prod, cantitate_prod, motiv_retur)
 VALUES ('$nume_prenume', '$nr_tlf', '$adresa_email', '$nr_comanda', '$data_comanda', '$nr_factura', '$den_prod', '$culoare_prod', '$cantitate', '$motiv')";
mysqli_query($conn, $sql0);
 }

所以基本上这是退货产品的公式,如果他有更多退货产品,我添加了该jquery以添加更多字段,并且我想插入数据库中,那么我将获取数据并将其显示在CMS中。

1 个答案:

答案 0 :(得分:0)

1。您不必在ajax中编写那么多代码。您可以轻松使用serialize()

使用以下代码减少ajax代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form=$("#your_form_id");
$("#Your_submit_id").click(function(){
$.ajax({
        type:"POST",
        url:"URL",
        data:form.serialize(),
        success: function(response){
            console.log(response);  
        }
    });
});
});
</script>

2。要添加多个产品:
-您必须将每个输入字段创建为产品信息的数组,例如:

  <input type="text" name="prod_name[]">
  1. 此数组语句会明智地为您存储每个产品信息索引。如果你 使用正常变量,那么它将仅发布您的最后一个产品 详细信息。
  2. 在Php代码中,您必须计算已发布的数量 产品标题并使用for循环获取每个产品信息。

    for($i=0;$i<count($prod_name);$i++) { //Insert/fetch All Values like : $prod_name[$i] }

-单击添加时,只需创建上一个可用div的克隆即可。