SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    autosuggest search

    Im trying to figure out best way to explain it. Im using a autosuggest search script in my search form. The script grabs the data from the database as I type (this is for searching tenants names). So you hit H and then click the name you want and it goes in the input box...then you click submit and I want it to open that persons datasheet.

    I already got it working so that you can search people like normal search and where you can click that individual persons name but its bringing up all other searches related to it...but the autosuggest already did the search so it should only bring up that one persons page.
    Last edited by chase636; Jun 16, 2008 at 16:50.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing the action attribute in the first form to to the page which loads that person's data sheet. Right now when you click Submit the form submits to 'search.php'.

    Change it to <form action="datasheet.php"> like so:

    PHP Code:
    <form action="datasheet.php" method="get">
    <
    input name="q".... />
    </
    form>

    //datasheet.php
    $id $_GET['q']; //or 'inputString'
    show_data_sheet($id); 
    By the way, I like that autocomplete.

  3. #3
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I have for my datasheet.php, when you use the autosuggest form and click submit, it will always bring back the id=1 record from database... which is not correct.

    I would think I would need to do something along the lines of a reverse search.

    PHP Code:
    <?php

    //connect info

    if (isset($_GET['q'])) {

        
    $p = (int)$_GET['q'];
        
    $sql mysql_query('SELECT * FROM articles WHERE title = ' $p) or die(mysql_error());
        
    $row mysql_fetch_array($sql);

    //Table header
    echo "<center><table border=\"0\"><tr><td>";
    echo 
    "<table border=\"0\" bgcolor=\"#d5dff3\">";
    echo 
    "<tr>";
    echo 
    "<td width=\"25\"><div align=\"left\">id:</div></td>";
    echo 
    "<td width=\"500\"><div align=\"left\">title:</div></td>";
    echo 
    "<td width=\"25\"><div align=\"left\">content:</div></td>";
    echo 
    "<td width=\"150\"><div align=\"left\">date:</div></td>";
    echo 
    "</tr>";

    $id $row["id"];
    $title $row["title"];
    $content $row["content"];
    $date $row["date"];

    echo 
    "<tr>";
    echo 
    "<td>$id</td>";
    echo 
    "<td>$title</td>";
    echo 
    "<td>$content</td>";
    echo 
    "<td>$date</td>";
    echo 
    "</tr>";
    echo 
    "</table></td></tr></table>";
    }
    ?>

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are making this more complicated than it is. Why reverse search?

    Quote Originally Posted by chase636 View Post

    [PHP]<?php

    //connect info

    if (isset($_GET['q'])) {

    $p = (int)$_GET['q'];
    $sql = mysql_query('SELECT * FROM articles WHERE title = ' . $p) or die(mysql_error());
    $row = mysql_fetch_array($sql);

    Why are you converting $_GET['q'] to an int, it is submitted as a string and you're searching for a title which is presumably also a string. The (int) cast is what is making all the search return the same result as a string cast to an int will become a number!

    Replace (int)$_GET['q] with a simple mysql_real_escape_string($_GET['q']) and see what happens..

  5. #5
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thank you, old_iron you are good.

    PHP Code:
        $p mysql_real_escape_string($_GET['q']);
        
    $sql mysql_query("SELECT * FROM articles WHERE title = '".$p."'") or die(mysql_error()); 
    I had some errors with my query though but all is working now.

  6. #6
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still learning myself. I used to make mistakes like that all the time. Glad it worked out for you!


Bookmarks

Posting Permissions

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