Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 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

26 Experts available now in Live!

Get 1:1 Help Now