【发布时间】: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 来访问 我的 jQuerywhich变量,所以它知道要查询哪个表。
以下是我最近的尝试: 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