簡體   English   中英

如何使用javascript將相同的變量從php發送到兩個不同的php頁面

[英]How to send the same variable from php to two different php pages using javascript

我有這4頁,並且工作正常,但現在我需要將在(index.php)的輸入字段中鍵入的“單詞”發送到(pag1.php),同時發送到另一頁(pag2.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

感謝您的幫助。

有一個示例場景在這里

您可以為每個調用實現一個方法,在調用所有調用之后,可以合並其結果

您也可以查看我的示例代碼以重現您的場景(根據需要進行修改)

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;
            }
        });
}

不要使用內聯函數,而應使用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!");
            }
         });
      }
   });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM