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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!