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

标签 javascript php jquery ajax

我正在用 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/

相关文章:

javascript - AJAX JS 循环 - 更简单的方法来完成多个帖子?

javascript - 如何通过javascript更新asp.net中现有的cookie

php - 将 PDO::FETCH_ASSOC 数组转换为索引数组

php - skelton 应用程序中的 Zend 框架 fatal error

javascript - 如何使用 Express 4 和 Multer 保存文件?

javascript - Vue-konva : Unable to fill rectangles with an image

php - AWS Beanstalk 文档目录

javascript - 无法根据 Google Chrome 上第一个 <select> 中选择的值填充 <SELECT> 选项

jQuery 在所有浏览器中的 SlideUp() 上闪烁...附有示例页面

jquery - 在选择时更改输入字段的值