我有这4页并且工作正常,但现在我需要将(index.php)输入字段中输入的“word”发送到另一个页面(pag2.php),同时发送到(pag1 .php)在(script.php)上使用这个javascript代码。
的index.php
<form method="post">
<input type="text" name="word" id="word" onkeyup="getSugest(this.value);">
</form>
<div class='search'><img ..... searching.gif></div>
<div id="sugest">
<div id="resultIndex"></div>
<div id="buttonsIndex">
<ul>
<?
for($i=1; $i<=page; $i++;){
echo "<li id='".$i."'>".$i."</li>";
}
?>
<ul>
</div>
</div>
的script.js
function getSugest(value){
if(value != ""){
if (callPage1(value) || callPage2(value)) {
var data1 = callPage1(value);
var data2 = callPage2(value);
//var combinedData = combine as data1 and data2 as you want
$("#sugest").html(combinedData);
} else {
$("#sugest").html("Theres nothing in DB!");
}
}
}
function callPage1(value) {
$.post("pag1.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
function callPage2(value) {
$.post("pag2.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
$(document).ready(function(){
function showLoader(){
$('.search').fadeIn(200);
}
function hideLoader(){
$('.search').fadeOut(200);
};
$("#buttonIndex li").click(function(){
showLoader();
$("#buttonIndex li").css({'background-color' : ''});
$(this).css({'background-color' : '#D8543A'});
$("#resultIndex").load("pag1.php?page=" + this.id, hideLoader);
return false;
});
$("#buttonCar li").click(function(){
showLoader();
$("#buttonCar li").css({'background-color' : ''});
$(this).css({'background-color' : '#D8543A'});
$("#resultCar").load("pag2.php?page=" + this.id, hideLoader);
return false;
});
$("#1").css({'background-color' : '#D8543A'});
showLoader();
$("#resultIndex").load("pag1.php?page=1", hideLoader);
$("#resultCar").load("pag2.php?page=1", hideLoader);
});
pag1.php
$word = mysql_real_escape_string(addslashes($_POST['word']));
echo "Word is: ".$word;
//here is the php and mysql querys to return the result
pag2.php
$word = mysql_real_escape_string(addslashes($_POST['word']));
echo "Word is: ".$word;
//here is the php and mysql querys to return the result
我感谢任何帮助。
答案 0 :(得分:2)
有一个示例方案here
您可以为每个调用实现一个方法,在调用所有调用之后,您可以组合他们的结果
您还可以查看我的示例代码以重现您的方案(根据您的需要进行修改)
function getSugest(value){
if(value != ""){
if (callPage1(value) || callPage2(value)) {
var data1 = callPage1(value);
var data2 = callPage2(value);
//var combinedData = combine as data1 and data2 as you want
$("#sugest").html(combinedData);
} else {
$("#sugest").html("Theres nothing in DB!");
}
}
}
function callPage1(value) {
$.post("pag1.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
function callPage2(value) {
$.post("pag2.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
答案 1 :(得分:2)
不要使用内联函数,而是使用jQuery的本机方法:
$(function(){
$('#word').keyup(function(){
if(this.value) {
$.post("pag1.php", { word: value }, function(data) {
if(data){
$("#suggest").html(data);
} else {
$("#suggest").html("Theres nothing in DB!");
}
});
}
});
});