I have a form
<form action="_orders.php" method="get" autocomplete="off" name="cerca_db">
<tr>
<td class="main" width="170">Ricerca Cliente</td>
<td><input type="hidden" name="action" value="check_sped_data" /></td>
<td class="main"><p class="ciao" id="auto"><input id="searchField" name="searchField" type="text" onKeyPress="return disableEnterKey(event)"></p></td>
<td width="25"></td>
<td class="main"><input type="submit" value="Cerca"></td>
</tr>
</form>
This is used to search in a mysql db table. Once the button is clicked it autocompletes all fields of another form based on the database record just found.
Now my problem is with autocomplete part because of case sensitive. I have already managed to extract data in lower case but i'm not able to transform input searchField to lower case before send to java function to match db record.
I actually found javascipt function to have autocomplete stuff. I only changed html and php part to match my needs, I didn't touch anything on java itself because i'm totaly a noob with it.
So now the form send the input to the javascript like this
$(function(){
setAutoComplete("searchField", "results", "autocomplete.php?part=");
});
The automcomplete.php is need to extract data from db based on input and place in array to display under the input:
$result = mysql_query("SELECT a.customers_firstname, a.customers_lastname, a.customers_id, b.entry_company FROM customers a left join address_book b on a.customers_id = b.customers_id GROUP BY b.entry_cf");
while ($row = mysql_fetch_assoc($result))
{
$ricerca = array(strtolower($row['customers_firstname']) => strtolower($row['customers_lastname']));
foreach ($ricerca as $key=>$data)
{
$edata[]=$data . ' ' . $key;
$edata[].=$key .' ' . $data;
}
$edata[].=(strtolower($row['entry_company']));
}
Now we have the array of names and company all lower case ready to match what is entered in the field. If the input is lowercase everything works fine, but if input is uppercase it doesn't match anything at all.
Do you guys have any solution on this or any idea how i can workout the case sensitive problem?
Edited: this is how data are pushed from db to the java script:
if(isset($_GET['part']) and $_GET['part'] != '')
{
$results = array();
$count = 0;
foreach($extracted_data as $edata)
{
if( strpos($edata, $_GET['part']) === 0 )
{
$results[] = $edata;
$count++;
if ($count == 10) break;
}
}
echo json_encode($results);
}
Before submitting or sending information, call a javascript function which converts all characters to lowercase. If your element is named "YourInputID", Example:
function convert()
{
var a = document.getElementById("YourInputID").value;
a = a.toString().toLowerCase();
document.getElementById("YourInputID").value = a;
}
You could use javascript string function reference here str.toLowerCase() to convert the string characters to lower.
This should get you on the right track:
document.getElementById('foo').addEventListener('keypress', function(e) {
e.preventDefault();
this.value += String.fromCharCode(e.keyCode).toLowerCase();
});
OK, firstly your PHP is going to have some problems. Where you say:
$edata[]=$data . ' ' . $key;
$edata[].=$key .' ' . $data;
That's going to access two different items in the array, which I assume you don't want to do. That's because the syntax $array[]
is (sort of) short for using the array_push()
method - each time it is accessed it adds to the array. The syntax $array[].=$value
means the same as $array[]=$value
as you can't effectively concatenate to an empty array item.
$edata_this='';
foreach ($ricerca as $key=>$data)
{
$edata_this=$data . ' ' . $key;
$edata_this.=$key .' ' . $data;
}
$edata_this.=(strtolower($row['entry_company']));
$edata[]=$edata_this;
Now, if your issue is simply converting a JavaScript item to lower case, use the String.toLowerCase()
method.
However I'm not entirely sure in the question which part you're actually needing to change or do a comparison on, so some more explanation might be useful.
Additionally you should always avoid using inline JavaScript such as onKeypress; as you're using jQuery try using its $().keypress()
binding:
$('input[name="YOUR_NAME"]').keypress(function(){
// your actions go here
});