我如何在 ajax 函数调用中发送参数 jquery
How do i send parameter in ajax function call in jquery
我正在 PHP 创建在线考试申请,但在 AJAX 调用时遇到问题。
我希望在单击右侧的其中一个按钮时使用 AJAX 调用来提取问题(并用于填充 div)。这些按钮不是静态的;它们是在服务器上生成的(使用 PHP)。
我正在寻找这样的 AJAX 电话:
functionname=myfunction(some_id){
ajax code
success:
html to question output div
}
按钮应该调用这样的函数:
<button class="abc" onclick="myfunction(<?php echo $question->q_id ?>)">
请建议一个 AJAX 可以使这项工作正常进行的调用
你做错了。 jQuery 有内置运算符来处理这样的事情。
首先,当您生成按钮时,我建议您这样创建它们:
<button id="abc" data-question-id="<?php echo $question->q_id; ?>">
现在在按钮上创建一个 listener/bind:
jQuery(document).on('click', 'button#abc', function(e){
e.preventDefault();
var q_id = jQuery(this).data('question-id'); // the id
// run the ajax here.
});
我建议你用这样的东西来生成按钮:
<button class="question" data-qid="<?php echo $question->q_id ?>">
您的事件侦听器将类似于以下内容:
$( "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
<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,就像您在处理表单一样。
data 是将传递到您的表单的信息。
success 是 jQuery 在调用 PHP 文件成功时将执行的操作。
更多关于 ajax here
PHP
$id = gethostbyname($_POST['id']);
//$questions= query to get the data from the database based on id
return $questions;
我正在 PHP 创建在线考试申请,但在 AJAX 调用时遇到问题。
我希望在单击右侧的其中一个按钮时使用 AJAX 调用来提取问题(并用于填充 div)。这些按钮不是静态的;它们是在服务器上生成的(使用 PHP)。
我正在寻找这样的 AJAX 电话:
functionname=myfunction(some_id){
ajax code
success:
html to question output div
}
按钮应该调用这样的函数:
<button class="abc" onclick="myfunction(<?php echo $question->q_id ?>)">
请建议一个 AJAX 可以使这项工作正常进行的调用
你做错了。 jQuery 有内置运算符来处理这样的事情。
首先,当您生成按钮时,我建议您这样创建它们:
<button id="abc" data-question-id="<?php echo $question->q_id; ?>">
现在在按钮上创建一个 listener/bind:
jQuery(document).on('click', 'button#abc', function(e){
e.preventDefault();
var q_id = jQuery(this).data('question-id'); // the id
// run the ajax here.
});
我建议你用这样的东西来生成按钮:
<button class="question" data-qid="<?php echo $question->q_id ?>">
您的事件侦听器将类似于以下内容:
$( "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
<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,就像您在处理表单一样。
data 是将传递到您的表单的信息。
success 是 jQuery 在调用 PHP 文件成功时将执行的操作。
更多关于 ajax here
PHP
$id = gethostbyname($_POST['id']);
//$questions= query to get the data from the database based on id
return $questions;