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
  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX MYSQL Search

    I am currently trying to put together a AJAX search that searches a MySQL database using PHP. I have created the below scripts. I cannot get it to function properly. Essentially I want someone to type in a partial search term and click submit and see the results. The purpose of this is for it to become part of a mobile app. Any help would be appreciated. Thank you!

    HTML:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>


    <style type="text/css">
    body{font-family:'Lucida Grande', Verdana, sans-serif;; font-size:14px; color:#666666;}
    h2{color:#000000; margin-bottom:20px;}
    h3{color:#000000; font-size:14px;}
    input{font-size:16px; color:#444444;}
    a:link, a:visited, a:hover{color:#0033CC;}
    a:hover{text-decoration:none;}
    div.searchInput{padding:8px; background:#DEDEDE; clear:both;}
    div.footer{padding:6px; border-top:solid 1px #DEDEDE; font-size:10px;}
    #msg{background:#FFFFCC; margin-bottom:10px; padding:4px; display:none;}
    </style>

    <script type="text/javascript">

    function showProducts(str)
    {
    if (str=="")
    * {
    * document.getElementById("search-result").innerHTML="";
    * return;
    * }
    if (window.XMLHttpRequest)
    * {// code for IE7+, Firefox, Chrome, Opera, Safari
    * xmlhttp=new XMLHttpRequest();
    * }
    else
    * {// code for IE6, IE5
    * xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    * }
    xmlhttp.onreadystatechange=function()
    * {
    * if (xmlhttp.readyState==4 && xmlhttp.status==200)
    *** {
    *** document.getElementById("search-result").innerHTML=xmlhttp.responseText;
    *** }
    * }
    xmlhttp.open("GET","search.php?q="+str,true);
    xmlhttp.send();
    }
    </script>


    </head>

    <body>

    <form id="searchForm" name="searchForm" method="post" action="javascript:searchNameq();">
    <div class="searchInput">
    <input name="searchq" type="text" id="searchq" size="30" onkeyup="javascript:showProducts()"/>
    <input type="button" name="submitSearch" id="submitSearch" value="Search" onclick="javascript:showProducts()"/>
    </div>
    </form>

    <h3>Search Results</h3>
    <div id="msg">Type something into the input field</div>
    <div id="search-result"></div>

    </body>

    </html>


    PHP:
    <?php

    $q=$_GET["q"];

    //connect to the database
    mysql_connect("christianbrogers.db.5646816.hostedresource.com","christianbrogers","GHbn123");
    mysql_select_db("christianbrogers");

    //explode our search term into separate words
    $search_exploded = explode(" ",$search);

    foreach($search_exploded as $search_each)
    {

    //construct the query
    $x++;
    if ($x==1)
    $construct .= "name LIKE '%$search_each%'";
    else
    $construct .= " OR name LIKE '%$search_each%'";
    }


    //echo out the constructed query

    $construct = "SELECT * FROM products WHERE $construct";

    $run = mysql_query($construct);

    $foundnum = mysql_num_rows($run);

    if ($foundnum==0)
    echo "No results found.";
    else
    {

    while ($runrows = mysql_fetch_assoc($run))
    {

    //get data
    $name = $runrows['name'];

    echo $name;
    ?>

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by christianbroger View Post
    I am currently trying to put together a AJAX search that searches a MySQL database using PHP. I have created the below scripts. I cannot get it to function properly.
    When you post code it should be enclosed in [code ] [/code] tags.
    Are you saying that your PHP isn't receiving the request or that your client script isn't returning the data that your PHP generates?

  • #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reply

    The AJAX is talking to PHP but is not returning

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Posts
    185
    Thanks
    3
    Thanked 42 Times in 42 Posts
    You need to define $construct before your for loop.

    PHP Code:
    $construct '';

    foreach(
    $search_exploded as $search_each)
    {

    //construct the query
    $x++;
    if (
    $x==1)
    $construct .= "name LIKE '%$search_each%'";
    else
    $construct .= " OR name LIKE '%$search_each%'";

    You were trying to concat an undefined variable.

  • #5
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Construct

    So I think I defined the construct (I'm fairly new to PHP). Below is my PHP code.

    PHP:

    PHP Code:
    (Connect to Database//explode our search term into separate words
                
    $search_exploded explode(" ",$search);
                
                
    $construct*=*'';
                
                foreach(
    $search_exploded as $search_each)
                {
                
                    
    //construct the query
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "name LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR name LIKE '%$search_each%'";
                        
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "it LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR it LIKE '%$search_each%'";
                        
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "description LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR description LIKE '%$search_each%'";
                        
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "keywords LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR keywords LIKE '%$search_each%'";
                        
                
                }
                
            
            
    //echo out the constructed query

            
    $construct "SELECT * FROM products WHERE $construct";

            
    $run mysql_query($construct);
            
            
    $foundnum mysql_num_rows($run);
            
            if (
    $foundnum==0)
                echo 
    "No results found.";
            else
            {
                
                while (
    $runrows mysql_fetch_assoc($run))
                {
                
                
    //get data
                
    $name $runrows['name'];
                
    $it $runrows['it'];
                
    $desc $runrows['description'];
                
    $image $runrows['image'];
                
    $page $runrows['page'];
                
                echo 
    "
                <ul class='spageitem'>

                
                    <li class='stextbox'>
                        <span class='simage'>
                            <img src='../images/labelrite/$image'><br>
                        </span>
                    
                    
                        <span class='sheader'>
                            $name
                        </span>

                        <span class='stext'>
                            $it<br>
                            $desc<br>
                            <a href='$page'\"><img src='../images/interface/info.jpg' /></a>
                        </span><br>
                    
                    </li>
                </ul>
                "
    ;
                }
            }
    ?> 
    I get the error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/16/5646816/html/search.php on line 14

    Line 14 is the $construct = '';

    Before I did this, I could click search and see everything in my database but it was not discriminatory, based on what was typed in the field. Confused

  • #6
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    80
    Thanks
    3
    Thanked 5 Times in 5 Posts
    You call

    onclick="javascript:showProducts()

    however your function has a parameter str

    Code:
    function showProducts(str)
    I assume you are sending an empty string over to the php script;

    Next in the php script you say

    Code:
    $q=$_GET["q"];
    to get the search string but you are not using $q anywhere in your script.

    Should this
    Code:
    $search_exploded = explode(" ",$search);
    not be

    Code:
    $search_exploded = explode(" ",$q);
    ????

  • #7
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Did I do this right?

    First of all, I really appreciate your help. Being knew at this, this is frustrating for me. Below is my code after the change. It is still loading the entire database.

    HTML
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    
    
    <head>
    <meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no" name="viewport" />
    <link href="../css/style.css" rel="stylesheet" type="text/css" />
    <script src="../javascript/functions.js" type="text/javascript"></script>
    <title>Kitchen iQ</title>
    <link href="../images/interface/iphone/startup.png" rel="apple-touchstartup-image" />
    
    <script type="text/javascript">
    function showProducts(str)
    {
    if (str=="")
    * {
    * document.getElementById("search-result").innerHTML="";
    * return;
    * } 
    if (window.XMLHttpRequest)
    * {// code for IE7+, Firefox, Chrome, Opera, Safari
    * xmlhttp=new XMLHttpRequest();
    * }
    else
    * {// code for IE6, IE5
    * xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    * }
    xmlhttp.onreadystatechange=function()
    * {
    * if (xmlhttp.readyState==4 && xmlhttp.status==200)
    *** {
    *** document.getElementById("search-result").innerHTML=xmlhttp.responseText;
    *** }
    * }
    xmlhttp.open("GET","http://www.daymarkpoints.com/search.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    
    </head>
    
    <body>         
    <div id="content">
    
        <div class="navigation">
    		<a href="../index.html">Home</a>:
    </div> 
    
    <ul class="pageitem">
      
    <form id="searchForm" name="searchForm" method="get">
    <div class="searchInput">
    	<input name="searchq" type="text" id="searchq" size="40"/>
    	<input type="button" name="submitSearch" id="submitSearch" value="Search" onclick="javascript:showProducts(this.value)"/>
    </div>
    </form>
    
    <h3>Search results listed below.</h3>
    <div id="search-result"></div>
    
    </ul>
    
    </body>
    
    </html>
    PHP Code:
    <?php

    //initial variables from the form
    $q $_GET['q'];
            
            
    //connect to the database
            
    mysql_connect("XXXXXXXXXXXX","XXXXXXXXXX","XXXXXXXXX");
            
    mysql_select_db("XXXXXXXXXXXXXX");
                
                
    //explode our search term into separate words
                
    $search_exploded explode(" ",$q);
                
                
    $construct "SELECT * FROM products WHERE ";
                
                {
                
                    
    //construct the query
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "name LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR name LIKE '%$search_each%'";
                        
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "it LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR it LIKE '%$search_each%'";
                        
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "description LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR description LIKE '%$search_each%'";
                        
                    
    $x++;
                    if (
    $x==1)
                        
    $construct .= "keywords LIKE '%$search_each%'";
                    else
                        
    $construct .= " OR keywords LIKE '%$search_each%'";
                        
                
                }
                
            
            
    //echo out the constructed query

            
    $run mysql_query($construct);
            
            
    $foundnum mysql_num_rows($run);
            
            if (
    $foundnum==0)
                echo 
    "No results found.";
            else
            {
                
                while (
    $runrows mysql_fetch_assoc($run))
                {
                
                
    //get data
                
    $name $runrows['name'];
                
    $it $runrows['it'];
                
    $desc $runrows['description'];
                
    $image $runrows['image'];
                
    $page $runrows['page'];
                
                echo 
    "
                <ul class='spageitem'>

                
                    <li class='stextbox'>
                        <span class='simage'>
                            <img src='../images/labelrite/$image'><br>
                        </span>
                    
                    
                        <span class='sheader'>
                            $name
                        </span>

                        <span class='stext'>
                            $it<br>
                            $desc<br>
                            <a href='$page'\"><img src='../images/interface/info.jpg' /></a>
                        </span><br>
                    
                    </li>
                </ul>
                "
    ;
                }
            }
    ?>


  •  

    Posting Permissions

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