Andre Oliveira
Andre Oliveira

Reputation: 357

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

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.

Upvotes: 2

Views: 525

Answers (2)

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

There is an example scenario here

You can implement a method for each call, and after calling all of them, you can combine their results

You can also have a look at my sample code for reproduce your scenario (modify it according to your needs)

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

Upvotes: 2

gggrishna1
gggrishna1

Reputation: 36

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

Upvotes: 2

Related Questions