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.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

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

    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:
    Code:
    <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 Code:
    <?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(
    $rowmysql_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.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Where's your function definition of showHint() ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Please post your ShowHint function.
    showHint(this.value, 'getName.php','txtHintTo')

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Sorry.

    It is:
    Code:
    <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

  • #5
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    May be you should check this part
    PHP Code:
    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
    Last edited by poyzn; 11-06-2010 at 08:50 PM.

  • #6
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    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?

  • #7
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    ok, try this:
    change the Reset function to
    Code:
    function Reset(NID,ThisValue){
        document.getElementById(NID).value=document.getElementById(NID).value + '; ' + ThisValue;
    }
    and in getName.php:
    PHP Code:
    <?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(
    $rowmysql_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';
    }

  • #8
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    The
    Code:
    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.

    ???

  • #9
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    ok, I see.
    Could you return back your Reset function and try this:
    PHP Code:
    <?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';
    }
    Last edited by poyzn; 11-07-2010 at 12:13 PM.

  • Users who have thanked poyzn for this post:

    Jenny Dithe (11-08-2010)

  • #10
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Wonderful. That works.

    Thank you so much.


  •  

    Posting Permissions

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