Jump to content

Ajax + PHP Autosuggest


davidalwis

Recommended Posts

Dear Friends,

 

I would like to ask You about autosuggest here.

I already find the tutorial from any Website.

 

This case is :

 

1. Enter number of employee :

This case explain about how many people that will be do overtime.

 

<form name="form" action="submit-employee.php" method=post onSubmit="return validate(form);" onkeypress="return isNumberKey(event)" autocomplete="off">

<div class="padding">Enter number of employee :</div>

<input class="text sign" type="text" id="jum" name="jum" maxlength="2">

<div id="signbox_left_button"><input type="submit" name="submit" value="Submit" /></div>

</form>

 

2. Textbox out in accordance with the input number that we enter the last, I use table here.

<table border="1">

<tr><td>Badge ID</td><td>Name</td><td>Dept</td><td>OT Date</td><td>From</td><td>To</td><td>Remarks</td></tr>

<?php

$n = $_POST['jum'];

for ($i=1; $i<=$n; $i++)

{

echo "<tr>

<td><input type='text' size='7' name='badgeid".$i."' id='inputString' onkeyup='lookup(this.value);' onblur='fill();'></td>

<td><input type='text' size='15' name='name".$i."'></td>

<td><input type='text' size='8' name='dept".$i."'></td>

<td><input type='text' size='10' name='otdate".$i."'></td>

<td><input type='text' size='4' name='otfrom".$i."' id='time3' value=''></td>

<td><input type='text' size='4' name='otto".$i."' id='time4' value=''></td>

<td><input type='text' size='25' name='remarks".$i."'></td>

</tr>";

}

?>

</table>

 

3. Step 3 I want text "badgeid" use autosuggest. The first text box is ok. but in other textbox "badgeid" when I try to input clik and show suggest, it set to first text "badgeid".

This code for auto suggest :

<script type="text/javascript">

function lookup(inputString) {

if(inputString.length == 0) {

$('#suggestions').hide();

} else {

$.post("rpc.php", {queryString: ""+inputString+""}, function(data){

if(data.length >0) {

$('#suggestions').show();

$('#autoSuggestionsList').html(data);

}

});

}

}

function fill(thisValue) {

$('#inputString').val(thisValue);

setTimeout("$('#suggestions').hide();", 200);

}

</script>

 

and this is RPC.php code :

<?php

$db = new mysqli('localhost', 'root' ,'', 'sei');

if(!$db) {

echo 'ERROR: Could not connect to the database.';

} else {

 

if(isset($_POST['queryString'])) {

$queryString = $db->real_escape_string($_POST['queryString']);

 

if(strlen($queryString) >0) {

$query = $db->query("SELECT badgeid FROM employee WHERE badgeid LIKE '$queryString%' LIMIT 10");

if($query) {

while ($result = $query ->fetch_object()) {

        echo '<li onClick="fill(\''.$result->badgeid.'\');">'.$result->badgeid.'</li>';

        }

} else {

echo 'ERROR: There was a problem with the query.';

}

} else {

}

} else {

echo 'There should be no direct access to this script!';

}

}

?>

 

4. If I've selected the auto suggest, Can we do for script for automaticaly show name and department based on badge id that we select before ?

 

Please help me to solve this problem.

Because I'm pressed for time deadlines by college lecturers.

 

Appreciate with your helping.

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/252425-ajax-php-autosuggest/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.