Solved

JQuery - when click on an autocomplete results send the id to next page

Posted on 2013-10-31
3
660 Views
Last Modified: 2013-10-31
I have this JQuery mobile autocomplete script that is showing business names from a db with PHP.
Is working fine, showing the results, I now want to click on one result and show the field id on the url (so another script will show the business description)
This is what I have:
PHP:
<?php
header('Content-Type: application/json', true);
include 'config.php';
//connection to the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
  or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db($dbname,$conn)
  or die("Could not select directory2");
  
  	$search = $_GET['term'];
	$return = array();
	
    $query = mysql_query("SELECT * FROM directory2 WHERE business LIKE '%$search%'OR city LIKE '%$search%' OR description LIKE '%$search%' ORDER BY business ASC") or die('Something went wrong');

    while ($row = mysql_fetch_array($query)) {
    $return[]= $row['business'];
}
echo $_GET["callback"] . "(" . json_encode($return) . ")"; 
?>

Open in new window


THE SCRIPT:

<script type="text/javascript">
$( document ).on( "pageinit", "#searchPage", function() {
    $( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {
        var $ul = $( this ),
            $input = $( data.input ),
            value = $input.val(),
            html = "";
        $ul.html( "" );
        if ( value && value.length > 2 ) {
            $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
            $ul.listview( "refresh" );
			$.ajax({
                url: "file.php",
                dataType: "jsonp",
                crossDomain: true,
                data: {
                    term: $input.val()
                }, 
            })
			
.then( function ( response ) {
                $.each( response, function ( i, val ) {
                    html += "<li><a href='employeedetails.html?id=" +val['id']+ "'>" + val + "</a></li>";
                });
                $ul.html( html );
                $ul.listview( "refresh" );
                $ul.trigger( "updatelayout");
            });
        }
    });
});
</script>

Open in new window


This is showing on the url ?id=undefined

Thanks for your help.
0
Comment
Question by:acbv
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39614648
replace :
$return[]= $row['business'];
by :
$return[]= array( "id"=>$row['id'] , "value"=>$row['business'] );

and replace :
 html += "<li><a href='employeedetails.html?id=" +val['id']+ "'>" + val + "</a></li>";
by :
 html += "<li><a href='employeedetails.html?id=" +val['id']+ "'>" + val['value'] + "</a></li>"
0
 

Author Comment

by:acbv
ID: 39614875
It worked!! Thanks a lot!!!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39614881
why a "B" grade?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question