真正的基本AJAX

I'm just starting out with AJAX. I'm trying to create a test where I enter some text into a text input box, click a submit button on a form, and have that text display on my page. The immediate error that I am getting is a 404 error ( www.mysite.com/ajaxFunction() ), but I'm sure that there are others yet to be discovered. Can anyone please help me correct this to get me started out with AJAX? I'm spinning my wheels trying to get started. Also, please realize that I am calling a php script since this is what my ultimate goal will require, which is why I'm not just using JavaScript itself. Thanks!

Here is the html:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>

<body>

    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
        window.onload = function ajaxFunction() {
            document.myform.action = getFeedback();
        }

        function getFeedback() {
            var xmlhttp;
            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) {
                    document.getElementById("feedback").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","scripts/handle_feedback.php",true);
            xmlhttp.send();
        }
    //-->
    </script>

    <div id="leftsidebox"></div>

    <div id="textboxdiv">
        <form name="myform">
            Text Here: <input type="text" name="textbox" id="name" />
            <button type="submit">Submit</button>
        </form>
    </div>

    <div id="feedback">
    </div>

</body>

</html>

handle_feedback.php

<?php
    $mytext = $_REQUEST['textbox'];
    return $mytext;
?>

EDIT: Here is my latest html code. I made the change to the php (switching 'return' to 'echo')

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
    window.onload = function ajaxFunction() {
        document.myform.onsubmit = getFeedback;
    }

    function getFeedback() {
        var xmlhttp;
        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) {
                document.getElementById("feedback").innerHTML=xmlhttp.responseText;
            }
        }
        var textvalue = document.getElementById("name").value;
        xmlhttp.open("GET","scripts/handle_feedback.php?textbox="+textbox,true);
        xmlhttp.send();
    }
//-->
</script>

<div id="textboxdiv">
    <form name="myform">
        Text Here: <input type="text" name="textbox" id="name" />
        <button type="submit">Submit</button>
    </form>
</div>

<div id="feedback">
</div>

scripts/handle_feedback.php

<?php
    $mytext = $_REQUEST['textbox'];
    echo $mytext;
?>

It should be echo not return. return is used in function to return the data.

<?php
$mytext = $_REQUEST['textbox'];
echo $mytext;
?>

Also you have to send the parameter to php file

xmlhttp.open("GET","scripts/handle_feedback.php?textbox=your_value",true);

This won't do what you're expecting it to do:

document.myform.action = getFeedback();

You're probably expecting it to make it call getFeedback when the form is submitted. That's not actually what happens. When the browser tries to run that code, it will realize: “oh, hey, we're assigning action. But wait, on the right hand side, there's a function call! I've got to evaluate that function call in order to find the return value to set action to!” And so your browser dutifully calls getFeedback immediately. Since getFeedback doesn't return anything, it sets action to undefined. Boy, that was helpful.

If we want a JavaScript function to be called when the form is submitted, setting action is not the right way to do it. So what is the right way? An event listener. The most common way of attaching an event listener is using addEventListener, but since your use case is rather simple, we're going to use a simpler, often neglected way: setting onsubmit:

document.myform.onsubmit = getFeedback;

Note that we do not have parentheses after getFeedback. This is intentional. We want to set onsubmit to the getFeedback function itself, not its return value.


You're also using textbox without defining it. Sure, it exists in the document, but that doesn't mean it's a variable available in the script. To access it, you'll need to first get the element and then get that element's value:

document.getElementById('name').value

Another thing that might be getting you is the same origin policy. Rather than using a complete URL to open, just pass a relative URL:

xmlhttp.open("GET", "something.php" /* ... */, true);

First point : you missed request parameters from client end.

Send parameters in querystring for GET request.

var textvalue = document.getElementById("name").value;
xmlhttp.open("GET","scripts/handle_feedback.php?textbox="+textvalue ,true);

for more reference read here.

And second point is :

'return' statement is used with functions/methods. Since you don't have any function here, so instead of that use 'echo' or 'print' statement.

<?php
$mytext = $_REQUEST['textbox'];
echo $mytext;
?>