Solved

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

Posted on 2013-10-31
3
639 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now