通过克隆发布带有数组的名称

时间:2016-10-04 04:41:50

标签: php jquery multidimensional-array clone implode

我想发布到文件crud.php,但是它由jquery clone创建,它在数组中生成带有数组的名称。请参阅片段!

$(document).ready(function() {
  addBarang();
});

function addBarang() {
  var new_barang = $(".hidden_input").find(".barang_in").clone().addClass("barang_in_clone");
  $(".target_clone:last").append(new_barang);
}

$('body').on('click', '.btn_kurangi_barang', function() {
  $(this).closest('.barang_in_clone').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    
  <div class="hidden_input" style="display: none">
    <div class="barang_in">
      <label class="col-md-4 control-label">Model : </label><input type='text' name='model[]'>
      <br>
      <label class="col-md-4 control-label">Kode Hanca : </label>
                        <input type="checkbox" name="aksesoris[]" value="ziper">Ziper
                        <input type="checkbox" name="aksesoris[]" value="kancing">Kancing
                        <input type="checkbox" name="aksesoris[]" value="size">Label Size
                        <input type="checkbox" name="aksesoris[]" value="kain_keras">Kain Keras
                        <input type="checkbox" name="aksesoris[]" value="karet">Karet
                        <input type="checkbox" name="aksesoris[]" value="logo">Label Logo
      <br>
      <button onclick="addBarang()" type="button">add</button>
      <button type="button" class='btn_kurangi_barang'> Remove</button>
    </div>
  </div>
  <div class='target_clone'>
  </div>

</body>

和这个php文件:

<?php
include 'conection.php';

$model = $_POST['model'];
$count_model = count($model);
$acc = implode(',' , $_POST['aksesoris']);

for($i=0; $i < $count_model; $i++){
$qry_insert = $db->query("INSERT INTO product SET model='$model[$i]', aksesoris='$acc[$i]' "); 
}


?>

但是,结果不是我想要的。结果显示$_POST['aksesoris']

的相同数据

如何解决?

1 个答案:

答案 0 :(得分:1)

要区分输入元素数据,您必须更改某些部分(已注释): -

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <form id = "submit_data" action="<?=$_SERVER['PHP_SELF'];?>" method="post"><!-- add one form so that you can submit -->
        <div class="hidden_input"><!-- remove the style -->
            <div class="barang_in">
                <label class="col-md-4 control-label">Model : </label><input type='text' name='new_model[]'>
                <br>
                <label class="col-md-4 control-label">Kode Hanca : </label>
                <input type="checkbox" name="model[0][]" value="ziper">Ziper
                <input type="checkbox" name="model[0][]" value="kancing">Kancing
                <input type="checkbox" name="model[0][]" value="size">Label Size
                <input type="checkbox" name="model[0][]" value="kain_keras">Kain Keras
                <input type="checkbox" name="model[0][]" value="karet">Karet
                <input type="checkbox" name="model[0][]" value="logo">Label Logo
                <br>
            </div>
        </div>
        <div class='target_clone'>
        </div>
        <button onclick="addBarang()" type="button">add</button><!-- put add button outside of the div -->
        <button type="button" class='btn_kurangi_barang'> Remove</button><!-- put remove button outside of the div -->
        <input type = "submit" value = "Submit">
    </form>
</body>

<script type="text/javascript">
    var i = 1; // add a counter 
// remove body reday clone function call
    function addBarang() {

        var new_barang = $(".hidden_input").find(".barang_in").clone().addClass("barang_in_clone"); // clone the entire div
        new_barang.find('input').each(function () {
            this.name = this.name.replace('[0]', '[' + i + ']'); // change the check box index so that you can distinguish
        });
        $(".target_clone:last").append(new_barang); // appen clone to div
        i++;
    }

    $('body').on('click', '.btn_kurangi_barang', function () {
        $('.barang_in_clone').last().remove();
    });

</script>

<?php
include 'conection.php';
if(isset($_POST) && count($_POST)>0){
     $new_model = $_POST['new_model'];
     $count = count($new_model);
    for($i = 0; $i < $count; $i++){
        echo $new_model[$i] . " - " .  implode(",", $_POST['model'][$i]);
    }
 }
?>

注意: -

1.查看prepared statement以阻止自己SQL Injection