...

View Full Version : Predicting names entered in field messes up on sencond name (PHP based w/ javascript)



Jenny Dithe
11-06-2010, 06:12 PM
Hi,

I have a field where you can search members. if you start typing a name it comes up with suggestions, if you click on the suggestion it is populated into that field, then you can keep typing names in the same field to build an array of the members, the suggestion for the second name works fine but if you click on that name it replaces the first name. What I want is for the field to read "John Doe; Jenny Dithe" etc.

Now this is because I am using the reset function, but what I am trying to do is reset the function with existing array + new name and that is not working.

I also tried add text, but say the first person they found is John Doe; and then they search for Jenny Dithe, and type Je before the Suggestion box comes up then what is populated in the text field is "John Doe; Je JennyDithe" - so I went back to reset which should work!! In theory I think.

I also have a hidden field where the individuals listed ID numbers are being listed, to be used for something else. (This is added to through my add text function, my wipe function, gets rid of the suggestion box once it has been cleared.)

So my code is:


<script type="text/javascript">
function loadNames (NID,ThisValue,NewID,ID,DID){
Reset (NID,ThisValue);
addtext(NewID,ID);
Wipe(DID);
}
</script>

<script type="text/javascript">
function Reset(NID,ThisValue){
document.getElementById(NID).value=ThisValue;
}
</script>

<script type="text/javascript">
function addtext(NewID,ID){
var element = document.getElementById(ID).value;
var newtext = NewID;
document.getElementById(ID).value=element+newtext;
}
</script>

<script type="text/javascript">
function Wipe(DID){
document.getElementById(DID).innerHTML='&nbsp;';
}
</script>
</head>
<body>
<label class="two"><b>To:</b></label><input type="text" class="input" name="to" id="to" value=""><input type="text" class="input" name="recipient" id="recipient" value="" onkeyup="showHint(this.value, 'getName.php','txtHintTo')" size="70" /><br /><br />
<div id="txtHintTo"></div>

and my getName.php is:


<?php
session_start();
$_SESSION['Id'];
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);

$q=$HTTP_GET_VARS["q"];

$names = explode("; ",$q);
$to = end($names);
$content = array_pop($names);
$output = count($names);

$result = mysql_query("SELECT * FROM clients INNER JOIN members WHERE ((clients.invitee='{$_SESSION['Id']}' AND clients.invited=members.Id) OR
(clients.invited='{$_SESSION['Id']}' AND clients.invitee=members.Id))
AND client.status='accepted'
AND (members.fullname LIKE '".addslashes($to)."%') ") or die("\nError: (" . mysql_error() . ") " . mysql_error());

if(mysql_num_rows($result) > 0){
while($row= mysql_fetch_array($result)){
if ($output=1){
echo '<input type="button" class="button9" name="companion" id="' . $row['PId'] . '," value="' . $row['fullname'] . '" onclick="loadNames(\'recipient\',\' ' . $row['fullname'] . '; \',\' ' . $row['PId'] . '; \',\'to\',\'txtHintTo\');" />';
} else {
echo '<input type="button" class="button9" name="companion" id="' . $row['PId'] . '," value="' . $row['fullname'] . '" onclick="loadNames(\'recipient\',\'array_pop($q) ' . $row['fullname'] . '; \',\' ' . $row['PId'] . '; \',\'to\',\'txtHintTo\');" />';
}
}
}else {
echo 'no suggestion';
}




?>


Sorry this is so long winded, I hope it is clear.

abduraooft
11-06-2010, 06:19 PM
Where's your function definition of showHint() ?

poyzn
11-06-2010, 06:22 PM
Please post your ShowHint function.

showHint(this.value, 'getName.php','txtHintTo')

Jenny Dithe
11-06-2010, 07:27 PM
Sorry.

It is:


<script type="text/javascript">
function showHint(str,File,ID){
if (str.length==0){
document.getElementById(ID).innerHTML="";
return;
}
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("GET",File + '?q=' + encodeURIComponent(str),true);
xmlhttp.send();
}
</script>

Added in the head section as well

poyzn
11-06-2010, 09:37 PM
May be you should check this part


echo '<input type="button" class="button9" name="companion" id="' . $row['PId'] . '," value="' . $row['fullname'] . '" onclick="loadNames(\'recipient\',\'array_pop($q) ' . $row['fullname'] . '; \',\' ' . $row['PId'] . '; \',\'to\',\'txtHintTo\');" />';
of getNames.php
cos I can see that $q is not an array
or problem is that you just put instead of add new value to recipient input field in Reset function
you can simply check values of elements with "alert" function on every step of your script

Jenny Dithe
11-07-2010, 04:04 AM
Yes, I think that is where the error is coming in.

I tried addtext for the fullname too but that was where I had the problem of whatever letters the person had typed in being added as well, which is why the recipient value is now being set with Reset.

Originally I had $content ' . $row['fullname'] being added (instead of array_pop($q)) but $content wasn't picking up the firstname in the list.

So that is where I got stuck.

The problem is when I type the second name in it is not picking up the first name too I think, but I am not sure how to resolve this?

poyzn
11-07-2010, 07:51 AM
ok, try this:
change the Reset function to

function Reset(NID,ThisValue){
document.getElementById(NID).value=document.getElementById(NID).value + '; ' + ThisValue;
}

and in getName.php:


<?php
session_start();
$_SESSION['Id'];
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);

$q=$HTTP_GET_VARS["q"];

$names = explode("; ",$q);
$to = end($names);
//$content = array_pop($names);
//$output = count($names);

$result = mysql_query("SELECT * FROM clients INNER JOIN members WHERE ((clients.invitee='{$_SESSION['Id']}' AND clients.invited=members.Id) OR
(clients.invited='{$_SESSION['Id']}' AND clients.invitee=members.Id))
AND client.status='accepted'
AND (members.fullname LIKE '".addslashes($to)."%') ") or die("\nError: (" . mysql_error() . ") " . mysql_error());

if(mysql_num_rows($result) > 0){
while($row= mysql_fetch_array($result)){
if (sizeof($row)){
echo '<input type="button" class="button9" name="companion" id="' . $row['PId'] . '" value="' . $row['fullname'] . '" onclick="loadNames(\'recipient\',\'' . $row['fullname'] . '\',\' ' . $row['PId'] . '; \',\'to\',\'txtHintTo\');" />';
}
}
}else {
echo 'no suggestion';
}

Jenny Dithe
11-07-2010, 11:44 AM
The


function Reset(NID,ThisValue){
document.getElementById(NID).value=document.getElementById(NID).value + '; ' + ThisValue;
}


adds the two elements together. But I am still getting the output:

Jo; John Doe; Je; Jenny Dithe;

when I want:
John Doe; Jenny Dithe

Adding the if (sizeof($row)){
into my code, the computer timed out trying to process.

Your suggestion of changing the function Reset made me wonder if I should be looking at javascript, but I have merrily popped, spliced, split etc. to no avail.

???

poyzn
11-07-2010, 12:58 PM
ok, I see.
Could you return back your Reset function and try this:


<?php
session_start();
$_SESSION['Id'];
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);

$q=$HTTP_GET_VARS["q"];

$names = explode("; ",$q);
$to = array_pop($names);
if(sizeof($names)) { $prev_names = implode('; ', $names) . '; '; }

$result = mysql_query("SELECT * FROM clients INNER JOIN members WHERE ((clients.invitee='{$_SESSION['Id']}' AND clients.invited=members.Id) OR
(clients.invited='{$_SESSION['Id']}' AND clients.invitee=members.Id))
AND client.status='accepted'
AND (members.fullname LIKE '".addslashes($to)."%') ") or die("\nError: (" . mysql_error() . ") " . mysql_error());

if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)){
if ($row['fullname']) {
echo '<input type="button" class="button9" name="companion" id="' . $row['PId'] . '" value="' . $row['fullname'] . '" onclick="loadNames(\'recipient\',\'' . $prev_names . $row['fullname'] . '; \',\' ' . $row['PId'] . '; \',\'to\',\'txtHintTo\');" />';
}
}
}else {
echo 'no suggestion';
}

Jenny Dithe
11-08-2010, 06:14 AM
Wonderful. That works.

Thank you so much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum