Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-10-31
3
Medium Priority
?
681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1500 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

730 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