简体   繁体   English

我如何在jQuery的ajax函数调用中发送参数

[英]How do i send parameter in ajax function call in jquery

I'm creating an online exam application in PHP and am having trouble with the AJAX calls. 我正在用PHP创建一个在线考试应用程序,而AJAX调用遇到了麻烦。

I want the questions to be fetched (and used to populate a div) using an AJAX call when one of the buttons on the right are clicked. 我希望在单击右侧的按钮之一时使用AJAX调用来提取问题(并用于填充div)。 These buttons are not static; 这些按钮不是静态的。 they are generated on the server (using PHP). 它们是在服务器上生成的(使用PHP)。

这是前端的接口

I'm looking for an AJAX call to be something like this: 我正在寻找这样的AJAX调用:

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

and the button should call a function like this: 该按钮应调用如下函数:

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

Please suggest an AJAX call that would make this work 请提出一个AJAX调用,以使其正常工作

You are doing it the wrong way. 您做错了方法。 jQuery has in-built operators for stuff like this. jQuery具有内置的运算符,可以处理类似这样的事情。

Firstly, when you generate the buttons, I'd suggest you create them like this: 首先,当您生成按钮时,建议您像这样创建它们:

<button id="abc" data-question-id="<?php echo $question->q_id; ?>">

Now create a listener/bind on the button: 现在在按钮上创建一个监听器/绑定:

jQuery(document).on('click', 'button#abc', function(e){
    e.preventDefault();
    var q_id = jQuery(this).data('question-id'); // the id

    // run the ajax here.
});

I would suggest you have something like this to generate the buttons: 我建议您使用类似的方法来生成按钮:

<button class="question" data-qid="<?php echo $question->q_id ?>">

And your event listener would be something like the following: 您的事件侦听器将如下所示:

$( "button.question" ).click(function(e) {
  var button = $(e.target);
  var questionID = button.data('qid');
  var url = "http://somewhere.com";
  $.ajax({ method: "GET", url: url, success: function(data) {
    $("div#question-container").html(data);
  });
});

HTML 的HTML

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

Script 脚本

$('.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");
     }
 });

}) })

The type variable tells the browser the type of call you want to make to your PHP document. type变量告诉浏览器您要对PHP文档进行调用的类型。 You can choose GET or POST here just as if you were working with a form. 您可以在此处选择GET或POST,就像处理表单一样。

data is the information that will get passed onto your form. 数据是将传递到您的表单上的信息。

success is what jQuery will do if the call to the PHP file is successful. 如果成功调用PHP文件,则jQuery将执行成功。

More on ajax here 更多关于ajax的信息

PHP 的PHP

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jquery向ajax调用添加参数? - how do I add a parameter to an ajax call using jquery? 如何使用jQuery将ajax调用的结果存储在函数中? - How do I store the result of an ajax call in a function using jQuery? 如何在ajax调用中发送参数 - how to send parameter in ajax call 如何通过具有多个参数的jQuery AJAX将文件发送到MVC控制器? - How do I send files via jQuery AJAX with multiple parameter to an MVC controller? 如何通过jQuery的.ajax()方法在GET请求中发送包含斜杠的参数? - How do I send a parameter containing a slash in GET request, via jQuery's .ajax() method? 如何将函数参数发送到 AsyncStorage? - How do I send a function parameter to AsyncStorage? 如何使用jquery ajax调用将texbox和附件发送到webmethod? - How do I send texbox and attachement to webmethod using jquery ajax call? 如何从多个下拉选择框中获取数据以发送jquery ajax调用? - How do I get data from Multiple dropdown select boxes to send a jquery ajax call? 如何在“ jQuery.ajax({success:function(data)””中设置类似“ data”的回调函数参数? - How do I set a callback function parameter like 'data' in the “jQuery.ajax({ success : function(data)”? 如何在.ajax内部调用函数? - how do I call function inside .ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM