0

I realize similar questions have been asked thousands times and yet it doesn't seem to work for me. I have a textbox called "movieTitle", it is generated via Javascript by clicking a button. And I'm calling jQueryUI autocomplete on that textbox just like in the official example http://jqueryui.com/autocomplete/#remote.

It works well if I hardcode "movieTitle" in the original page; however it just fails when I create "movieTitle" by changing the innerHTML of the div "formsArea". searchMovies.php is the same with search.php from the example. I had tried many answers from internet and from here. I learned that I would have to use .on() to bind the dynamic element "movieTitle". Still it doesn't seem to work. Even the alert("hahaha") works. Thanks for your time. :) Here's my script:

$(function()
{   
$(document).on('focus', '#movieTitle', function(){
    //alert("hahaha");

    $("#movieTitle").autocomplete({
            source: "../searchMovies.php",
            minLength: 2
        });
    }
);    

window.onload = main;

function main()
{
    document.getElementById("movieQuery").onclick = function(){showForms(this.value);};
    document.getElementById("oscarQuery").onclick = function(){showForms(this.value);};

// displays query forms based on user choice of radio buttons

function showForms(str)
{
    var heredoc = "";

    if (str === "movie")
    {
        heredoc = '\
            <h1>Movie Query</h1>\
            <form action="processQuery.php" method="get">\
            <div class="ui-widget">\
                <label for="movieTitle"><strong>Name: </strong></label>\
                <input type="text" id="movieTitle" name="movieTitle" />\
                <input type="submit" name="submitMovie" value="Submit" />\
            </div>\
            </form>';

        //document.getElementById("formsArea").innerHTML = heredoc;
        //$("#formsArea").append(heredoc);
        $("#formsArea").html(heredoc);



    }
    else if (str === "oscar")
    {
        heredoc = '\
            <h1>Oscar Query</h1>\
            <form action="processQuery.php" method="get">\
                <strong>Name: </strong>\
                <input type="text" name="oscarTitle" />\
                <input type="submit" name="submitOscar" value="Submit"/>\
                </form>';

        document.getElementById("formsArea").innerHTML = heredoc;

    }
}
}

});

The HTML is:

<form action=$scriptName method="get">
        <label for="movieQuery"><input type="radio" name="query" id="movieQuery" value="movie" />Movie Query</label>
        <label for="oscarQuery"><input type="radio" name="query" id="oscarQuery" value="oscar" />Oscar Query</label>
    </form>

    <div id="formsArea">
        <b>Please choose a query.</b>
    </div>
clouddreams
  • 622
  • 1
  • 4
  • 13
  • 1
    `showForms()` is producing invalid HTML. The `` should be before ``. Try switching it. _that is inside `if (str === "movie"){`_ – Ejaz Apr 29 '13 at 22:35
  • not catch :). But still doesn't fix the issue. – clouddreams Apr 29 '13 at 22:42
  • it is working for me. Can you post the `searchMovies.php` code. Does it even try to send ajax request to `searchMovies.php`? – Ejaz Apr 29 '13 at 22:53
  • searchMovies.php is a huge file. I just downloaded "search.php" from jQueryUI site and renamed it to "searchMovies.php". It returns JSON array. So the code should be fine. Plus it works with static "movieTitle". – clouddreams Apr 29 '13 at 23:01
  • Can I know how it is working for you? This problem is really bugging me. – clouddreams Apr 29 '13 at 23:02
  • Update: Thank you. I think you are in the right direction. When I change the source to an array like: ['one', 'two'], it works. And the 'searchMovies.php' is supposed to be fine too. That left me to check ajax request. – clouddreams Apr 29 '13 at 23:09
  • 1
    check for the right path in ajax request `source: "../searchMovies.php",` in your browser's debugging tool – Ejaz Apr 29 '13 at 23:10
  • You are right. I changed it to "searchMovies.php" and it is working now. I feel like breaking some stuffs. Because the thing is the script is in /web/scripts/javascripts/ and the php is in /web/scripts/ so why isn't "../searchMovies.php" working but "searchMovies.php" does? any idea? Anyway I will accept your answer. Please post and explain this weird behavior if you can. Thank you so much :) – clouddreams Apr 29 '13 at 23:17
  • 1
    hah :D the wrong URL. I think the resultant path depends on the path that's generating the ajax request, which is `/web/scripts` in your case. – Ejaz Apr 29 '13 at 23:22
  • I've been doing the right way after all. Sigh... If you don't post an answer, I will post my own answer for future victims :D – clouddreams Apr 29 '13 at 23:36
  • Ok, posted to save you the effort ;) Please feel free to improve it. – Ejaz Apr 29 '13 at 23:47

1 Answers1

1

You should check for the URL you're sending an AJAX request to. The paths in script files are relative to the page they're being displayed in. So albeit your script is in /web/scripts/javascripts/js.js, when this file is included in /web/scripts/page.php, the path to /web/scripts/searchMovies.php should be searchMovies.php instead of ../searchMovies.php because your script is being used in /web/scripts/.
Good ways to avoid such confusion is to
a. use absolute URL
b. the URL that're relative to root of your domain (that start with a /),
c. or define your domain's path in a variable, var domain_path = 'http://www.mysite.com/' and use it in your scripts.

I hope it clarifies things :)

Relative Paths in Javascript in an external file

Community
  • 1
  • 1
Ejaz
  • 8,719
  • 3
  • 34
  • 49
  • excellent answer... :) good day... it's like the real evil is in disguise... foolin' me to fix the .on() thing which is already correct... – clouddreams Apr 30 '13 at 00:05