Problems with AJAX

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Bennor
Forum Newbie
Posts: 2
Joined: Sun Mar 16, 2008 11:32 am

Problems with AJAX

Post by Bennor »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello,
I am trying to create a simple form where the user writes something in an input box, clicks on a button and the input box becomes what the user have inserted. I am pretty sure I am not doing something right with my ajax and js code so I am pasting it here. I will be happy to hear where am I wrong:

JS

Code: Select all

<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!
    
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.value = ajaxRequest.responseText;
        }
    }
    ajaxRequest.open("GET", "insert.php", true);
    ajaxRequest.send(null); 
 
function changeText2(){
    var userInput = document.getElementById('userInput').value;
    document.getElementById('title').innerHTML = userInput;
}
 
//-->

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Problems with AJAX

Post by pickle »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
Moving to Client-Side.

I always find using document.getElementById() is a simpler method than trying to refer to an element by it's name.

Also, you're not calling your function anywhere.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply