我正在查看一些有关实现 jQuery ajax 方法的 Stack Overflow 线程:
How to pass parameters in GET requests with jQuery
虽然这个帖子提供了一些非常好的信息,但我正在寻求澄清这是否真的能达到我认为的效果。
我在 jQuery 点击函数中有 ajax GET 请求:
//click an image
$(".img_div").click(function() {
//get integer stored in element attribute and put it in variable
var altCheck = $(this).find('.modal_img').attr('alt');
//get MySQL data
$.ajax({
url: "get.php",
type: "get"
data: {
ajaxid: altCheck //variable from above - is this correct?
}
....
....
});
在get.php中,我希望像这样使用变量:
$sql = "SELECT * FROM table WHERE screeningId = $ajaxid";
本质上,我想通过将变量传递到 AJAX 请求来修改我的 SQL 语句,但我不确定这是否是完成的方式。
最佳答案
在你的 JavaScript 中使用这个
$.ajax({ url: 'get.php',
data: {'ajaxid':altCheck},
type: 'post',
dataType:'json'
});
并在 get.php 中使用
$ajaxid = $_POST['ajaxid'];
$sql = "SELECT * FROM table WHERE screeningId = $ajaxid";
关于php - jQuery AJAX : Clarification on How to Pass Variable into AJAX GET Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249740/