duanbing8817 2012-04-27 21:54
浏览 74
已采纳

如何使用javascript将相同的变量从php发送到两个不同的php页面

I have these 4 pages and works fine, but now i need to send the "word" typed in the input field at (index.php) to another page (pag2.php) at the same time it sends to (pag1.php) using this javascript code at (script.php).

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

I appreciate any help.

  • 写回答

2条回答 默认 最新

  • dongpiansui8755 2012-04-27 22:06
    关注

    Don't use inline functions, use jQuery's native methods instead:

    $(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!");
                }
             });
          }
       });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写