Jump to content

Hi, I'm a newbie to AJAX, pls help me with the form below ?


jd2007

Recommended Posts

ok, below is the page which contains ajax

<html>
<script type="text/javascript" language="javascript">
    function makeRequest(url) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up  Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
        httpRequest.open('POST', url, true);
        httpRequest.send('');

    }

    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                document.write(httpRequest.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
</script>

Name:<input type=text name=name />
<input type=submit value=Display onclick="makeRequest(data.php)" /> 

 

and this is the php script data.php:

 

<?php
if (empty($_POST["name"])
{
  echo "Please enter your first name.";
}
else
{
  echo $_POST["name"];
}
?>

 

when i type my name in text box and click Display...i want it to display my name but what i get is nothing, the page doesn't change...why...pls help ?

You're not even getting the value from the text field, the code above simply makes a request.

 

Why not take a look at this script/tutorial, it's going to lead you in the right direction:

http://www.captain.at/howto-ajax-form-post-request.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.