Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 02-28-2007, 01:00 AM Form input and Ajax
Extreme Talker

Posts: 196
Trades: 0
I'm trying to get this code to work dynamically. Basically i need the JS function to be generic per form and input textfield name. I believe my issue is in my form document declarations, my usage of the *dyn* variable.

FYI, If i make the text input name *dyn*, it works great.

Thanks. Cesar.

Javascript
Code:
<script language="JavaScript">
function getData(dyn)
{
    var req = null;

    document.this_form.dyn.value="Started...";
    if(window.XMLHttpRequest)
        req = new XMLHttpRequest();
    else if (window.ActiveXObject)
        req  = new ActiveXObject(Microsoft.XMLHTTP);

    req.onreadystatechange = function()
    {
        document.this_form.dyn.value="Wait server...";
        if(req.readyState == 4)
        {
            if(req.status == 200)
            {
                document.this_form.dyn.value="Received:" + req.responseText;
            }
            else
            {
                document.this_form.dyn.value="Error: returned status code " + req.status + " " + req.statusText;
            }
        }
    };
    req.open("POST", "get.php", true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(null);
}
</script>
HTML
Code:
<FORM name="this_form" method="POST" action="">
<INPUT type="BUTTON" value="Submit"  ONCLICK="getData('company_name')">
<input type="text" name="company_name" size="32" value="">
</FORM>
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-28-2007, 01:50 AM Re: Form input and Ajax
Extreme Talker

Posts: 196
Trades: 0
Here is working code to fill a text area with a value via ajax.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script language="JavaScript">

function getData(dyn)
{
    var req = null;
    var input = document.getElementById(dyn);
    input.value = "Started...";
    if(window.XMLHttpRequest)
        req = new XMLHttpRequest();
    else if (window.ActiveXObject)
        req  = new ActiveXObject(Microsoft.XMLHTTP);

    req.onreadystatechange = function()
    {
        input.value = "Wait server...";
        if(req.readyState == 4)
        {
            if(req.status == 200)
            {
                input.value = "Received:" + req.responseText;
            }
            else
            {
                input.value = "Error: returned status code " + req.status + " " + req.statusText;
            }
        }
    };
    req.open("POST", "get.php", true); // returns string "working"
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(null);
}

</script>
</head>
<body>

<FORM name="this_form" method="POST" action="">
<INPUT type="BUTTON" value="Submit"  ONCLICK="getData('company_name')">
<input type="text" id="company_name" name="company_name" size="32" value="">
</FORM>
</body>
</html>
empiresolutions is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Form input and Ajax
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14858 seconds with 12 queries