Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Finding the id of a jquery autocomplete input from within the onItemSelect call

    i have this,

    Code:
    $(function() {
           $('#input').autocomplete('test.php', {json: true});
         });
    that bring back values from the database here is my php section

    Code:
    $result = mysqli_query($db,$query);
    
    while( $row = mysqli_fetch_array($result) )
    {	
    $aResults[] = array( "id"=>($row['s_id']) ,"value"=>($row['s_last']), "info"=>($row['s_first']) );
    }
    	
    $return = array();
    
    foreach ($aResults as $key => $res) {
    	 $return[] =$res['value'] . ' , ' . $res['info'];
    	 
    }
    
    
    echo json_encode($return);
    
    
    
    		}
    	}
    }
    i would like, when i select a name from the auto suggest to be able to have the id be filled in a input text area...

    uncle Google says this, but i am having a hard time implementing it.

    Code:
    myCallback = function(li, $input) {
        // I need to refer to the appropriate "myXxxInput" here
        alert($input.attr('id'));
    }
    
    setup = function() {
        setupInput($('#myFirstInput'));
        setupInput($('#mySecondInput'));
    }
    
    function setupInput($input) {
        $input.autocomplete('blah.php', {onItemSelect: function(li) {
            myCallback(li, $input);} });
    }

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I would use the result function of this plugin.

    http://docs.jquery.com/Plugins/Autoc...result#handler

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-19-2009)

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    how would i implement that into

    Code:
    	$(function() {
           $('#input').autocomplete('test.php', {json: true});
         });

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you look at the source code on the plugin's demo site:

    http://view.jquery.com/trunk/plugins/autocomplete/demo/

    Find the id #singleBirdRemote. It is using this result function. You should get a good idea of how to use it based on that code.

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-19-2009)

  • #5
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    k i will look at that

  • #6
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    exactly what i want! but still cant get it to work :-(


    KILL ME

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Well make sure you don't post any code, that would be too helpful for us who might be able to help

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-19-2009)

  • #8
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    ok i know its bad! please bare with me.
    i know it the php, its returning 0 , 1 (from the drop down, if i have 2 results)
    and in the hidden input i get "Array" when selected.

    firebug i get response

    GET http://

    0|array
    1|array


    A MESS I TELL, A MESS!!
    PHP Code:
    $result mysqli_query($db,$query);

    while( 
    $row mysqli_fetch_array($result) )
    {    
    $aResults[] = array( "id"=>($row['s_id']) ,"value"=>($row['s_last']), "info"=>($row['s_first']) );
    }

    foreach (
    $aResults as $key=>$value) {
        
            echo 
    "$key|$value\n";
        }
    }
    }
        } 
    here is the javascript

    Code:
    <script type="text/javascript">
    $().ready(function() {
    
    	function log(event, data, formatted) {
    		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
    	}
    	
    	function formatItem(row) {
    		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
    	}
    	function formatResult(row) {
    		return row[0].replace(/(<.+?>)/gi, '');
    	}
    	
    	
    	$("#singleBirdRemote").autocomplete("test.php", {
    		width: 260,
    		selectFirst: false
    	});
    	
    	$("#singleBirdRemote").result(function(event, data, formatted) {
    		if (data)
    			$(this).parent().next().find("input").val(data[1]);
    	});
    	
    	
    });
    </script>
    HTML

    Code:
    	<p>
    			<label>Single Bird (remote):</label>
    			<input type="text" id="singleBirdRemote" />
    			<input type="button" value="Get Value" />
    		</p>
    		<p>
    			<label>Hidden input</label>
    			<input />
    		</p>
    Last edited by codingForumsFTW; 11-19-2009 at 11:27 PM.

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Is that PHP code what populates the selectbox? There is a problem with that code-- you are adding elements to array $aResults, each element is an array. So you are 2 arrays deep, so you need to refer to an element in the 2nd array when generating the output. Something like:

    PHP Code:
    foreach ($aResults as $key=>$value) {    
        echo 
    "$key|{$value['id']}\n";    

    Last edited by Fumigator; 11-20-2009 at 05:36 PM.

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-20-2009)

  • #10
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    just parse errors.
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'

    can we take one step back? just really lost. my php code is just a mess!

    this is the php from the demo, i took out most of the result so its easier to see.

    PHP Code:

    <?php
    $q 
    strtolower($_GET["q"]);
    if (!
    $q) return;
    $items = array(
    "Great <em>Bittern</em>"=>"Botaurus stellaris",
    "Little <em>Grebe</em>"=>"Tachybaptus ruficollis",
    "Black-necked Grebe"=>"Podiceps nigricollis",
    "Little Bittern"=>"Ixobrychus minutus",
    "Black-crowned Night Heron"=>"Nycticorax nycticorax"
    );

    foreach (
    $items as $key=>$value) {
        if (
    strpos(strtolower($key), $q) !== false) {
            echo 
    "$key|$value\n";
        }
    }

    ?>
    this is my php (not running very well at all)
    how do i convert this to what is needed.
    PHP Code:
    <?php
    $aResults 
    = array();
    $db mysqli_connect("""""""") or die(mysqli_connect_error());
    if(!
    $db) {
        
    // Show error if we cannot connect.
        
    echo "ERROR: Could not connect to the database.";
    } else {
            if(isset(
    $_GET['q'])) {
                
    $queryString $db->real_escape_string($_GET['q']);
                if(
    strlen($queryString) >0) {



    $query "SELECT s_id,s_last,s_first FROM table_student WHERE s_last LIKE '".$queryString."%'"." GROUP BY s_last,s_first LIMIT 20;";    
        

    $result mysqli_query($db,$query);

    while( 
    $row mysqli_fetch_array($result) )
    {    
    $aResults[] = array( "id"=>($row['s_id']) ,"value"=>($row['s_last']), "info"=>($row['s_first']) );
    }
    foreach (
    $aResults as $key=>$value) {     
        echo 
    "$key|$value['id']\n";     
    }  
    }
            }
        }

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Sorry I forgot to enclose that variable in squigglies {}. I edited my code above so it should work.

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-20-2009)

  • #12
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    now it is returning

    i type a it drops down 0 click on it you get 20 in the hidden text area

    0|20 firebug says this

  • #13
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Alright! Looks like it's working!

    Now you just need to change it to echo the value from the database you want to put in the hidden input instead of the id.

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-20-2009)

  • #14
    New Coder
    Join Date
    Nov 2009
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts
    well thats the problem

    0|20

    the 20 is the id of the database row


    which is fine... the 0 is where need the last name to come in on...

  • #15
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That's easy to change. Look at the echo statement that creates the 0|20. The "0" is the $key of the top-level array. You just want to replace that with the value in the database you need. Something like $row['columnName'].

  • Users who have thanked Fumigator for this post:

    codingForumsFTW (11-20-2009)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •