Providing updating suggestions list with javascript, php and ajax

Posted by user1104854 on Stack Overflow See other posts from Stack Overflow or by user1104854
Published on 2012-11-21T22:58:51Z Indexed on 2012/11/21 22:59 UTC
Read the original article Hit count: 186

Filed under:
|
|

I'm trying to modify this example on making a live updating list to integrate it with my API. So, instead of using GET on the page with the form, I'd like to send it to that page via a function call.

So, here's my form

// message.php

//function to display the hint sent from gethint.php
function message_hint($hint){
    echo $hint;
}

//displays the form for sending messages
function send_message_form($to_user,$title,$message){
    include 'gethint.php';
    ?>
    <table>
    <form name = "send_message" method="post">
    <td>Send A Message</td>
    <tr><td>To:</td><td><input type = "text" size="50" name="to_user" id = "to_user" value ="<? echo $to_user; ?>" onkeyup="showHint(this.value)"></td></tr>
    <tr><td>Title:</td><td><input type = "text" size="50" name="message_title"></td></tr>
    <tr><td>Message:</td><td><textarea rows="4" cols="50" name="message_details"></textarea></td></tr>
    <tr><td><input type="submit" name="submit_message"></td></tr>
    </table>
    </form>
    <?
}

Here's the head of message.php

<head>
<script>
function showHint(str){
var to_user = document.getElementById("to_user").value //to_user is the id of the textbox
if (str.length==0){  
    to_user.innerHTML="";
    return;
}
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
            alert(to_user) //properly displays the name via alert box
        to_user.innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","gethint.php?q="+to_user,true);
xmlhttp.send();
}
</script>
</head>

The page gethint.php is exactly the same, aside from this at the bottom.

//echo $response //this was the original output
$message = new messages;
$message->message_hint($response);

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript