【问题标题】:Post name with array by clone通过克隆发布带有数组的名称
【发布时间】:2017-02-12 03:31:52
【问题描述】:

我想发布到文件 crud.php,但它是由 jquery clone 创建的,它产生带有数组中数组的名称。请看sn-p!

$(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']的相同数据

如何解决?

【问题讨论】:

  • 我只是想让你知道你很容易受到 sql 注入:stackoverflow.com/questions/2200256/…
  • 感谢 Thomas 的建议,非常有帮助。你能告诉我,如何在上面创建类似代码的语句而没有漏洞吗?
  • 寻找prepared statement以防止自己被SQL Injection

标签: php jquery multidimensional-array clone implode


【解决方案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

【讨论】:

  • 是的,谢谢阿南特。我忘了内爆必须接收数据,否则会显示错误。
  • @wawanSetiyawan 很高兴为您提供帮助。请注意备注部分
猜你喜欢
  • 2015-05-13
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
  • 2012-04-12
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
相关资源
最近更新 更多