Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Help for ajax

  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help for ajax

    I am trying to make this.

    1. Suggest name in select menu as typed in text box
    2. Once the preferred name is clicked in the menu then show that users profile in other form (currently div would be ok).

    Part 1 works good. But part 2 doesnot show any change.

    Here is my file Please help urgent. Tried other forums but nothing happened. http://rathour.com.np/ajax.zip

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    That URL fails to load. Browser says it can't find the server (both MSIE and FF).

    Look, instead of giving us a ZIP file, which is a royal pain anyway (we have to download it and then find some safe place to unzip it and and and...) how about just showing some code here? Be sure to put it between [ code ] and [ /code ] tags (without the spaces).

    Don't show us code that works; just show us the part that doesn't.

    For example, you say you are loading <option>s into a <select> via AJAX. Is that right? And then the user clicks on one of those <option>s and nothing happens. So what we really need to see is the <SELECT> tag itself and then whatever JS code it invokes, presumably via ONCHANGE?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Ahhh...just tried the URL for the 5th time and was able to get to the zip file.

    But your code is *NOT* doing what you said!!!

    You said:
    1. Suggest name in select menu as typed in text box
    ...
    Part 1 works good.
    But all your code does is dump the search result into the innerHTML of the <div>!!! You don't even *TRY* to create <option>s in a <select>. You don't even have a <select> to put the options into!
    Code:
         var ajaxSearchResults = document.getElementById("ajaxDiv"); // the place holder of the result.
         ajaxSearchResults.innerHTML = ajax.responseText;
    So what are you talking about when you say "Suggest name in select menu"??? WHAT "select menu"???

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry if my coding is all wrong.

    But the select menu that shows values once any thing is typed in the textbox of index.php is in that div. and from that now what i want is to load the data into another form when i choose something that is being displayed in the select menu (div - as u say).

    So what can i do to fix it?
    Could u fix it for me?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Okay, I see. You are just having your PHP code return a <SELECT>. Not the best way to do this.

    But your <SELECT> is all messed up.

    You have
    Code:
    <select name="names" id ="names" onchange="getdata();">
            <?php 
    		while ($row = mysql_fetch_array ($result)) {
    		?>
            <option id="names1" onchange="getdata()" value="<?php echo $row['name'];?>">
            <?php 
    		echo $row['name'];
    		?>
            </option>
            <?php
    		$str = "";
                $i++;
            }
    		?>
            </select>
            <?php } }?>
    <option>s can *NOT* have IDs. <option>s can *NOT* have ONCHANGE.

    And you have $str and $i++ in there for who knows what reason. They aren't doing anything useful, at all.

    And you do all that work to set up a $resultDiv PHP variable...but then you never DO anything with that variable.

    On top of that, you are including "search.js" in the HTML that is returned! So your HTML page will have *TWO COPIES* of that code. Huge mistake.

    You "search.php" page should *PROBABLY* look ONLY like this:
    Code:
    <?php
    $conn=mysql_connect('localhost','root','') or die("<h5 align=center>Could Not Connect to the Server</h5>");
    $db=mysql_select_db("recess1",$conn) or die("<h5 align=center>Could Not Find the Databassse</h5>");
    
    $searchQuery = $_GET['searchQuery'];
     
    if ($searchQuery != "" || $searchQuery != null) 
    {
     
        $result = mysql_query(
            "SELECT * FROM category WHERE name like '$searchQuery%' ORDER BY id desc"
                     ); 
        
        if ($result != false || $result || (mysql_num_rows ($result)> 0)) 
        { 
            //if more than 0 results are found then display them in select menu.
            echo '<select name="names" onchange="getdata();">';
            while ($row = mysql_fetch_array ($result)) 
           {
                echo "<option>" . $row['name'];? . "</option>";
            }
            echo '</select>'
       } 
    }
    ?>
    Untested. I'm not a PHP person.

  • #6
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    Fixed. The above code had quite a few errors. The new data is here:
    Code:
    <?php
    $conn=mysql_connect('localhost','root','') or die("<h5 align=center>Could Not Connect to the Server because ".mysql_error()."</h5>");
    $db=mysql_select_db("recess1",$conn) or die("<h5 align=center>Could Not Find the Database because ".mysql_error()."</h5>");
    
    $searchQuery = $_GET['searchQuery'];
     
    if ($searchQuery != "" || $searchQuery != null) 
    {
     
        $result = mysql_query(
            "SELECT * FROM category WHERE name like '$searchQuery%' ORDER BY id desc"
                     ); 
        
        if ($result != false || $result || (mysql_num_rows ($result)> 0)) 
        { 
            //if more than 0 results are found then display them in select menu.
            echo "<select name=\"names\" onchange=\"getdata();\">\n";
            while ($row = mysql_fetch_array ($result)) 
            {
                echo "<option>" . $row['name'] . "</option>\n";
            }
            echo "</select>\n";
        }
    }
    ?>
    Btw. if u need to use echo, use double quotations as single quotations dont allow for \n to be used. Also. if u need to use more than 1 set of ANY quote in a single echo, use backslashes ONLY on the quotes which will be inside the echo statement. example: <?echo "Hi, \"friend\"\n";?> it will say: Hi, "friend", instead of Hi, friend Same also happens with <? $friend = "friend"; echo "Hi, \"".$friend."\""; ?> it will return Hi, "friend" as well, as u use a dot, then php variable, or a dot then a set of quotes with text in the middle of quotes and w/e is put in the variable, is what it will say in quotes

    Good luck

    ~SI~
    Last edited by ShadowIce; 05-20-2009 at 11:57 PM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Sorry, Shadow, I don't see anything you changed that matters. Except my typo where I had ";?" for who knows what reason.

    There's no reason to put the \n's into the HTML, because it will never be seen by a human being (remember, this is an AJAX page) and browsers don't care about line breaks, at all.

    Actually, I see other things wrong with the code:
    (1) Why make a DB connection at all if there is no search term passed?
    (2) Why check for the search term both != "" *OR* != null?? If it's not null, it *will* be a string--rules of what $_GET returns.

    *PROBABLY* a more sensible page would look like this:
    Code:
    <?php
    $searchQuery = $_GET['searchQuery'];
    if ($searchQuery != null && || $searchQuery != "" ) 
    {
        $conn=mysql_connect('localhost','root','') 
                 or die("<h5 align=center>Could Not Connect to the Server</h5>");
        $db=mysql_select_db("recess1",$conn) 
                 or die("<h5 align=center>Could Not Find the Databassse</h5>");
        $result = mysql_query(
            "SELECT * FROM category WHERE name like '$searchQuery%' ORDER BY id desc"
                     ); 
        if ( $result == null || mysql_num_rows ($result) == 0 )
        {
            // optional message
            echo "<h5 align=center>No matches</h5>";
        } else
        { 
            //if more than 0 results are found then display them in select menu.
            echo '<select name="names" onchange="getdata();">';
            while ($row = mysql_fetch_array ($result)) 
            {
                echo "<option>" . $row['name'] . "</option>";
            }
            echo '</select>'
       } 
    }
    ?>
    If you want the \n's in there for some odd reason--debugging, perhaps?--then change to Shadow's strings. I see no reason to use \" or \' when there is no need for them, especially not to just put in \n.
    Last edited by Old Pedant; 05-21-2009 at 04:43 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •