php - Retrieve data from MySQL database after jQuery autocomplete -



have implemented on site jquery autocomplete function works well. however, use result autocomplete retrieve selected person's telephone number database.

the database structure this;

id | name | type | tel | mobile | email | level =============================================== 1 | john smith | judge | 01234 567890 | 07812 345678 | jsmith@example.com | bs 2 star 

here updated code far

autocomplete function

<script> jquery(document).ready(function($) {     $('#inputchiefjudge').autocomplete({         source:'lookups/shows-sj-searchforjudge.php',         change: function (event, ui) {             $.ajax({                 type: post,                 url: 'lookups/shows-sj-findtel.php',                 data: 'id='+ id,                 success: function(data) {                     details = $.parsejson(data);                     $('#inputchiefjudge').text("hello");                     $('#chiefjudgetel').text(details);                 },             });         },         minlength:2}); }); </script>  


lookups/shows-sj-findtel.php

<?php include("config.php"); mysql_connect ($dbhost, $dbuser, $dbpass); mysql_select_db ("equilive_manager"); $id = $post["id"]; $result = mysql_query("select tel, mob officials id='{$id}'"); $judgerow = mysql_fetch_array($result, mysql_assoc);  $contactdetails[] = array( 'tel' => $row['tel'], 'mob' => $row['mob'], );  echo json_encode($data); flush(); ?> 


lookups/shows-sj-searchforjudge.php

<?php // if 'term' variable not sent request, exit if ( !isset($_request['term']) ) exit;  // connect database server , select appropriate database use include("../config.php"); mysql_connect ($dbhost, $dbuser, $dbpass) or die ('i cannot connect database because: ' . mysql_error()); mysql_select_db ("equilive_manager");  // query database table name match 'term' $term = mysql_real_escape_string($_request['term']); $rs = mysql_query("select id, name, level officials name '%{$term}%' order name limit 0,10");  // loop through each name returned , format response jquery $data = array(); if ( $rs && mysql_num_rows($rs) ) {     while( $row = mysql_fetch_array($rs, mysql_assoc) )     {         $data[] = array(             'label' => $row['name'] .', '. $row['level'],             'value' => $row['name'],             'id' => $row['id'],         );     } }  // jquery wants json data echo json_encode($data); flush(); 

thanks in advance, craig

you have 1 issue @ least in code, in getchiefjudgecontactdetails() you're mixing javascript php. mixing 2 works fine if it's first time output page , code on php page. if you're expecting javascript run php code every time change event triggered auto-complete, won't work.

use select event others have stated, inside that, make ajax request similar end point autocomplete send value of option (e.g. id value 2). use sql in php script fetch row id , return json object. parse result , update ui in jquery ajax call result handler.

update:

change autocomplete this

<script> jquery(document).ready(function($) {     $('#inputchiefjudge').autocomplete({         source:'lookups/shows-sj-searchforjudge.php',         select: function (event, ui) {             $.ajax({                 type: post,                 url: 'lookups/shows-sj-findtel.php',                 data: {id:id},                 success: function(data) {                     details = $.parsejson(data);                     $('#inputchiefjudge').text("hello");                     $('#chiefjudgetel').text(details);                 },             });         },         minlength:2}); }); </script>  

instead of using change option of autocomplete, use select (as stated other answers question). also, instead of using string ("id="+id) data, use js object ({id:id}). jquery handle serializing correctly before sending server, result being shows post variable in php script.

also, more of side note, suggest looking using pdo driver (http://www.php.net/manual/en/pdo.prepare.php) access database instead of using mysql_* commands. it's object oriented , automatically provides safety features not available in old commands, such prevention of sql injection attacks.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -