gpt4 book ai didi

javascript - 我如何在jquery中的ajax函数调用中发送参数

转载 作者:行者123 更新时间:2023-11-30 12:29:44 25 4
gpt4 key购买 nike

我正在用 PHP 创建在线考试应用程序,但在使用 AJAX 调用时遇到了问题。

我希望在单击右侧的其中一个按钮时使用 AJAX 调用获取问题(并用于填充 div)。这些按钮不是静态的;它们是在服务器上生成的(使用 PHP)。

Here is interface of front end

我正在寻找这样的 AJAX 调用:

functionname=myfunction(some_id){
ajax code
success:
html to question output div
}

按钮应该调用这样的函数:

<button class="abc" onclick="myfunction(<?php echo $question->q_id ?>)">

请建议一个 AJAX 调用,使这项工作成功

最佳答案

HTML

<button class="abc" questionId="<?php echo $question->q_id ?>">

脚本

$('.abc').click(function () {
var qID = $(this).attr('questionId');
$.ajax({
type: "POST",
url: "questions.php", //Your required php page
data: "id=" + qID, //pass your required data here
success: function (response) { //You obtain the response that you echo from your controller
$('#Listbox').html(response); //The response is being printed inside the Listbox div that should have in your html page. Here you will have the content of $questions variable available
},
error: function () {
alert("Failed to get the members");
}
});

})

type 变量告诉浏览器您想对 PHP 文档进行的调用类型。您可以在此处选择 GET 或 POST,就像使用表单一样。

数据 是将传递到您的表单的信息。

success 是如果对 PHP 文件的调用成功,jQuery 将执行的操作。

关于 ajax 的更多信息 here

PHP

 $id = gethostbyname($_POST['id']);
//$questions= query to get the data from the database based on id
return $questions;

关于javascript - 我如何在jquery中的ajax函数调用中发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28103491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com