2

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.

5
  • What do you mean "to another page at the same time"? What are you having problems with? Just add another $.post. As for: "is this the best way" I'd suggest not using inline JavaScript, and using $('#word').keyup to bind the event. Commented Apr 27, 2012 at 22:00
  • Your getSugest is missing some }s and )s. Commented Apr 27, 2012 at 22:03
  • Thanks for correct me. I think now is best explained. I already tried "just add another $.post", but didnt work. Could you send me an example please? Commented Apr 27, 2012 at 22:10
  • How did it not work? What was the issue? Commented Apr 27, 2012 at 22:11
  • I created the same $.post just like this one, making it two block of codes just the same, only changing the page name from pag1.php to pag2.php. And when i go to the pag2.php does not appear the word. I mean, the variable doesnt go to the pag2.php. Does not display erros. Commented Apr 27, 2012 at 22:29

2 Answers 2

2

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!");
            }
         });
      }
   });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer my question. What do i need to put instead of onkeyup="pegarSugest(this.value);"
You just remove that. $('#word').keyup(function(){... does the equivalent of onkeyup="... but in a cleaner manner.
2

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

17 Comments

thanks for the answer.. the example you sent is very complex and will change much of my code, i would like to maintain the structure. Could you write an example of separating it in methods? cheers
cucuzoa, i really appreciate your help, and i think its nearly there. When i changed my code for this one, and typed a word inside of the input box, it shows me only the message "Theres nothing in DB!". Do i need to change anything in the others pages? In the pag1.php and pag2.php i keep calling the variable "word" as i did before? If so we need some adjustment in the code. cheers man
I updated the conditional logic in the code. && to || . So, this means, if there is valid response data at least one page, you will write it to page, else, you will show error message. Could you please try new version of code?
Still the same. Only show the message: "Theres nothing in DB!"
You got no data from both page?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.