duanmu0834 2017-04-22 10:00
浏览 42

我怎样才能在mysql查询中使用jquery变量

At the moment, I am using a $_GET to query mysql and populate a select statement, which works fine. However, I now need to query db using jquery variable and am unable to find a way to use 'depts' instead of '$_GET['dept']'.

I have declared the var global, but realise that you cannot use var in query.

I would be grateful if someone could show me how to amend my code to achieve this. Thanks

php code to populate select

<?php
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("sample", $conn);
    $result = mysql_query("SELECT * FROM boxes where department = '{$_GET['dept']}' and status = 1 ORDER BY custref ASC");
?>

    <select name="boxdest[]" id="boxdest" size="7" multiple="multiple">

<?php
    $i=0;
    while($row = mysql_fetch_array($result)) {
    ?>
    <option value="<?php echo $row["custref"];?>"><?php echo $row["custref"];?></option>
    <?php
    $i++;
    }
?>

    </select>

jQuery change event code

<script type="text/javascript">

    var depts;

    $('#dept').on('change', function() {

    depts = $('#dept option:selected').html();

    if (depts === 'Select a Department') {

       $('#deptResult').html('<p>ERROR: You must Select a department to proceed<p/>').css({'color':'red'});
       $( "#submit" ).prop( "disabled", true );
       return;
    }
      $('#deptResult').html('<p>SUCCESS: You have selected the following dept: ' + depts + '</p>').css({'color':'black'});
    });
</script>
  • 写回答

2条回答 默认 最新

  • dongzhong2018 2017-04-22 10:04
    关注

    Use jquery ajax() like:

    $.ajax({
        url     : 'process.php',
        method  : 'get',
        async   : false,
        data    : {
            variable : value,  
            // you can pass multiple variables like this and this is available in php like $_REQUEST['variable']
        },
        success : function(response){
            // do what ever you want with the server resposne
        }
    });
    

    process.php:

    $variable = $_REQUEST['variable'];  // you can use $variable in mysql query
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?