【问题标题】:passing a variable with get request to PHP将带有 get 请求的变量传递给 PHP
【发布时间】:2020-10-13 21:04:46
【问题描述】:

目标: 我正在尝试从数据库表列中填充下拉列表...我需要将一个变量从前端传递给 PHP,以指示要从哪个表 ping,因为它可能会根据用户事先的选择而改变...

  • 这是我试图传递我的which 变量以用作 这个指标——但我很难过,因为我试图 在$.get 请求中这样做?如果我可以让$which = $_GET['id']; 使用let which = $(frm).attr("id"); 报告/收集我在 jQuery 中的内容,那我应该很高兴。
  • 我怎么能这样做,我不认为我不能也不认为它会 尝试将post 包裹在我的get 或vica 周围的任何好的做法 反之?或者链接 post 然后 get - 我只需要 PHP 来访问 我的 jQuery which 变量,所以它知道要查询哪个表。

以下是我最近的尝试: jquery

$('#agent').click(function(){

  let which = $(frm).attr("id");
  console.log(which);

  $.get('dlteopt', 'id='+which, function (response) { // attempt at id
       console.log(response);

        $.each(response,function(i,obj){
          let dd_data="<option value="+obj+">"+obj+"</option>";
          $(dd_data).appendTo('#agent');

          console.log(obj);

        });  
  });
});

还有 PHP 方面:

   $app->get('/dlteopt', function ($request, $response, $args) {   
             
        $which = $_GET['id'];

        var_dump($which);
    
        if ($which) {
    
            var_dump($which);

            $db = $this->db;
            $repo = new coolDBclass($db);
            $selectIt = $repo->byCol($which);
    
        }
    
    });

这里是函数byCol btw:(如果我能传递正确的表变量,一切都应该没问题)

public function byCol($which) {

    var_dump($which);

    if ($which == 'table_1'){
        $sql = "SELECT table_1 FROM tab.cool";
    } else if ($which == 'table_2'){
        $sql = "SELECT table_2 FROM tab.awesome";
    } else if ($which == 'table_3'){
        $sql = "SELECT table_3 FROM tab.rad"; 
    }

    // ............/

相关标记:

<select name='agent' id='agent'><option>Placeholder</option></select>

【问题讨论】:

    标签: javascript php jquery get


    【解决方案1】:

    如果您尝试使用 $.get 方法的第二个参数来传递数据,它需要是一个对象。

    请参阅docs 中的示例。

    .get('dlteopt', {id: which }, function (response) { 
    

    【讨论】:

    • 我先给它
    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多