我有一个JavaScript代码,该代码是我从在线教程中复制的,以了解更多主题,而我一直在使用它,而并不了解其中一些代码的用途。现在我想将脚本中的变量ID传递给另一个名为main.php的php页面,我该怎么做。
这是脚本的样子
(function($) {
var id = "<?php echo variable; ?>"; //this line is where i want to set the variable and pass it to the main.php
$.fn.loaddata = function(options) {
// Settings
var settings = $.extend(
{
loading_gif_url: "ajax-loader.gif", //url to loading gif
end_record_text: "End of Feed", //no more records to load
data_url: "main.php", //url to PHP page
start_page: 1, //initial page
},
options,
);
var el = this;
loading = false;
end_record = false;
contents(el, settings); //initial data load
$(window).scroll(function() {
//detact scroll
if ($(window).scrollTop() + $(window).height() >= $("#cont2").height()) {
//scrolled to bottom of the page
contents(el, settings); //load content chunk
}
});
};
//Ajax load function
function contents(el, settings) {
var load_img = $("<img/>")
.attr("src", settings.loading_gif_url)
.addClass("loading-image"); //create load image
var record_end_txt = $("<div/>")
.text(settings.end_record_text)
.addClass("end-record-info"); //end record text
if (loading == false && end_record == false) {
loading = true; //set loading flag on
el.append(load_img); //append loading image
$.post(settings.data_url, { page: settings.start_page }, function(data) {
//jQuery Ajax post
if (data.trim().length == 0) {
//no more records
el.append(record_end_txt); //show end record text
load_img.remove(); //remove loading img
end_record = true; //set end record flag on
return; //exit
}
loading = false; //set loading flag off
load_img.remove(); //remove loading img
el.append(data); //append content
settings.start_page++; //page increment
});
}
}
})(jQuery);
$("#results").loaddata();
答案 0 :(得分:0)
使用以下代码,您可以在main.php文件中将id读取为$ _POST ['id']。 我已经将“ data_id”添加到“设置”变量中 然后在$ .post中,我传递了一个附加参数,例如“ id:settings.data_id” 请检查以下代码:
(function($) {
var id = "<?php echo $variable; ?>";
$.fn.loaddata = function(options) {// Settings
var settings = $.extend(
{
loading_gif_url: "ajax-loader.gif", //url to loading gif
end_record_text: "End of Feed", //no more records to load
data_url: "main.php", //url to PHP page
start_page: 1, //initial page
data_id: id
},
options,
);
var el = this;
loading = false;
end_record = false;
contents(el, settings); //initial data load
$(window).scroll(function() {
//detact scroll
if ($(window).scrollTop() + $(window).height() >= $("#cont2").height()) {
//scrolled to bottom of the page
contents(el, settings); //load content chunk
}
});
};
//Ajax load function
function contents(el, settings) {
var load_img = $("<img/>")
.attr("src", settings.loading_gif_url)
.addClass("loading-image"); //create load image
var record_end_txt = $("<div/>")
.text(settings.end_record_text)
.addClass("end-record-info"); //end record text
if (loading == false && end_record == false) {
loading = true; //set loading flag on
el.append(load_img); //append loading image
$.post(settings.data_url, { page: settings.start_page,id: settings.data_id}, function(data) {
//jQuery Ajax post
if (data.trim().length == 0) {
//no more records
el.append(record_end_txt); //show end record text
load_img.remove(); //remove loading img
end_record = true; //set end record flag on
return; //exit
}
loading = false; //set loading flag off
load_img.remove(); //remove loading img
el.append(data); //append content
settings.start_page++; //page increment
});
}
}
})(jQuery);
$("#results").loaddata();