开发者

Livesearch using Php Ajax - Need Help!

开发者 https://www.devze.com 2023-02-16 19:28 出处:网络
Trying to get a live search (ajax) going using ajax/php. I have (2) files, index.php and searching.php. I\'ve written the initial queries to search database for given fields on typing. I need help imp

Trying to get a live search (ajax) going using ajax/php. I have (2) files, index.php and searching.php. I've written the initial queries to search database for given fields on typing. I need help implementing that with the php and ajax in order to perform ajax-ie searches. I've implemented the $query variable to search for the box when any of the 3 values are there (e.g. "Enter a name", etc). Anyone? I'm a bit desperate on this one :)

Index.php

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="global.css"  media="screen" />
    <link rel="stylesheet" href="design.css"  media="screen" />
    <style type="text/css">
    .hidden {
    display: none;
    }
    </style>
    <script type="text/javascript" src="inc/jquery.js"> </script>
    <script type="text/javascript">$(function() {$('input[type=text]').focus(function() {$(this).val('')});});</script>
    <script type="text/javascript">
        $(document).ready(function(){
        $("input[type=radio]").click(function(){
           $(".box").val(this.value);
        }); 
    });
    </script>

</head>

<body>
<div id="top_wrapper_bg">
    <div id="wrapper_top">
        <div id="header">
                <div id="logo">
                <a href="index.php"><img src="img/logo-02-01-11.png" alt="logo-02-01-11" width="300" height="100" border="0" /></a>
                </div><!-- /logo -->
                <div id="header_image">
                right
                </div><! -- /header_image -->
                <div id="clear"> </div><! -- /clear -->
                <div id="nav">
                nav
                </div><! -- /nav -->
        </div><!-- /h开发者_Python百科eader-->
    </div><!-- /wrapper_top -->
</div><!-- /top_wrapper_bg -->

<div id="header_search_break"> </div>

<div id="bot_wrapper_bg">       
    <div id="wrapper_bottom">
            <div id="search"> 
                    <span class="medfont">Search by:</span> 
                        <form id="searchSelect" action="searching.php">
                            <input type="radio" name="search_value" value="Enter Professor Name">Professor
                            <input type="radio" name="search_value" value="Enter Course (e.g. COM 310)">Course
                            <input type="radio" name="search_value" value="Enter Major (e.g. Business)">Major
                        </form>
                        <br>            
                    <input class="box" type="text" size="25" name="searchQuery" value="Select an option..." class="textField clearMeFocus" />
            </div><!-- /search -->
            <div id="latest_stats">
            <p>Stats</p>
            </div><!-- /latest_stats -->
            <div id="clear"> </div>
        <div id="contain_stats">
                <div id="latest_prof">
                latest prof
                </div><!-- /latest_prof -->

                <div id="top_prof">
                top prof
                </div><!-- /top_prof -->
                <div id="clear"> </div><! -- /clear -->
        </div><!-- /contain_stats -->
        <br><br><br><br><br><br><br><br>
    </div><!-- /wrapper_bottom -->
</div><!-- /bot_wrapper_bg -->
</body>
</html>

searching.php

<?php
require_once('dbconnec.php'); 
$query = $_POST['search_value'];
SELECT profID, fName, lName FROM professor WHERE fName OR lName LIKE $query; // search for professor ajax livesearch code
SELECT courseID, name, primaryCode, secondaryCode FROM course WHERE courseID OR name OR primaryCode or secondaryCode LIKE $query;  // search for course ajax livesearch code
SELECT name FROM department WHERE name LIKE $query; // search by dept ajax livesearch code
?>


header("Cache-Control: no-cache, no-store, must-revalidate");
 if(isset($_GET['nme'])) 

{
$nme = $_GET['nme']; $query = "select * from users where full_name like '%".Sanitize($_GET['nme'])."%'"; $result = mysql_query($query);
Sanitize() is my own function made to keep away sql injection

Then on your input you should have onchange="search(this.value)">

0

精彩评论

暂无评论...
验证码 换一张
取 消