Jump to content


Photo

Live Database search using PHP, MySQL, and JSON


  • Please log in to reply
5 replies to this topic

#1 phpnewbie34

phpnewbie34

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 04 May 2013 - 04:18 PM

Good afternoon, 

 

I am looking to add a live search to my site.

 

My JS / HTML code is:

 

 

       <script>
    var form = document.getElementById('keywordForm');
    form.addEventListener('submit',function(e) {
        var search = form.keyword.value;
        var xhr = new XMLHttpRequest();
        xhr.open("GET","keywordsearch.php?search="+search,true);
        xhr.send();
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4 && xhr.status == 200) {
                var response = xhr.response;
            }
        };
        e.preventDefault();
    });
</script> 
    <form id="keywordForm">
    <input id="keyword" type="text" />
    <input type="submit" value="Search"/>
</form> 
 
And my PHP for the other page is:
 
<?php
if(isset($_GET['search'])) { 
 $search =   $_GET['search'];
 
   include 'dbconnect.php';
//Include code that connects to the database
    $result = mysqli_query($con, "Select * FROM pixs WHERE search=$search"  );
    $keywords = array(); 
   while ($keywords = mysqli_fetch_array($result)) { 
        $keywords[] = $keyword['keyword']; 
    } 
    echo json_encode($keywords); 
}
?>

 

I can't even get it to refresh on the first when I click on the search button.

 

Any advice / tips / examples would be amazing!

 



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,377 posts
  • LocationCheshire, UK

Posted 04 May 2013 - 04:40 PM

Are you sure the query isn't failing?

$result = mysqli_query($con, "Select * FROM pixs WHERE search=$search"  );

if (!$result) die($con->error);

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 Lewwy

Lewwy

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 04 May 2013 - 04:53 PM

Sorry if I'm being dense, but what is the difference between using this method to using $_POST or $_GET? 

 

Is it that the actual page doesn't refresh?



#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 May 2013 - 05:11 PM

Is it that the actual page doesn't refresh?

Yes.
Google "Ajax"
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 phpnewbie34

phpnewbie34

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 04 May 2013 - 06:34 PM

I am not getting any error messages. I am wondering how I can alter the database live using search as well as making it work with a pagination script.

 

What is wrong with my SELECT by statement?



#6 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 04 May 2013 - 09:23 PM

Try,

<form id="keywordForm" onsubmit="return false;">
    <input id="keyword" type="text" />
    <input type="submit" value="Search" />
</form> 

<script>

    var e = document.getElementById('keywordForm');
    e.addEventListener("submit", function(){listener(e)}, false);

    function listener(e) {
        var search = e.keyword.value;
        var xhr = new XMLHttpRequest();
        xhr.open("GET","keywordsearch.php?search="+search,true);
        xhr.send();
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4 && xhr.status == 200) {
                var response = xhr.response;
            }
        };
        e.preventDefault();
    }    
</script>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com