dtgsl60240 2016-01-19 07:28
浏览 8
已采纳

如何在javascript中进行基于下拉的查询mysql? [重复]

This question already has an answer here:

<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
    rowNum ++;
    var row = '<p id="rowNum'+rowNum+'"> Barang: ';
    row += '<select name="???">';
    row += '<option value="A1">A1</option>';
    row += '<option value="A2">A2</option>';
    row += '<option value="A3">A3</option>';
    row += '<option value="A4">A4</option>';
    row += '</select>';
    row += ' Satuan: <input type="text" size="5" name="satuan[]" value="'+frm.add_satuan.value+'"> Quantity: <input type="text" name="qty[]" value="'+frm.add_qty.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"><hr color=red></p>';

    $('#itemRows').append($(row));
     frm.add_qty.value = '';
     frm.add_nama.value = '';
         frm.add_satuan.value = '';
};
  </script>

How to make drop-down list but with query mysql to show A1, A2, dll. When submit button for drop-down can't post data anything. Can I store data with this javascript. For text input success post data.

</div>
  • 写回答

2条回答 默认 最新

  • douyinglan2599 2016-01-19 07:43
    关注

    DO NOT DO WHAT YOU ARE TRYING TO DO. You should never, ever, ever write queries on the front-end. You should do your absolute best to hide every detail of the server/database from the user. It is a massive security risk. Please read about SQL injection attacks for starters.

    How you should do this:

    Store the values of the dropsdowns in JavaScript. Let's keep them in a single object to make life easy:

    Your JS:

    var options = {
      A1: $("#rowNum select option[value='A1']").text(),
      A2: $("#rowNum select option[value='A2']").text(),
      A3: $("#rowNum select option[value='A3']").text(),
      A4: $("#rowNum select option[value='A4']").text()
    };
    
    // Now, send this object to your PHP via an AJAX call. Let's assume for simplicity that you will do this using jQuery:
    $.ajax({
      url: 'my/php/script.php',
      data: options,
      success: function (data) { console.log('Yay, it worked!'); },
      error: function (jqXHR, textStatus, error) { console.log('crap it didn't work', jqXHR, textStatus, error); }
    });
    

    Your PHP

    <?php
    
    $options = $_REQUEST['options']
    // You need to verify the options are valid (and don't have bad values) but that's a different question
    
    // Build your query here. Your PHP is run on the server only so no one else will see it or be able to change it.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?