SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    whats wrong? Simple ajax with prototype.

    Hello,

    I for some reason:

    Code:
    //JS
    ````
    function ajx(){
    	var url = 'getName.php';
    	//var param = 'name';
    	var target = 'n';
    	var ajax = new Ajax.Updater( "target", "getName.php", {method:'get',   onFailure: alert("Failure"), onSuccess: alert("success")} );
    }
    ////////
    
    //PHP
    ````
    <?php 
    echo("Jim");
    ?>
    /////
    
    //HTML
    ``````
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="javascript/prototype.js"></script>
    <script type="text/javascript" src="javascript/siteJs.js"></script>
    <title>Ajax Test</title>
    </head>
    
    <body>
    <span style="border: 1px solid #000" onclick="ajx()">Click me</span><br />
    Your name is: <div id"n">n/a</div>
    </body>
    </html>
    1st the onFailure evt. fires then onSuccess fires. When I check the firebug console, it says

    "GET file:///C:/ip/html/ajaxtest/getName.php"

    I have no idea what has gone wrong. Help?

    Thanks.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    var ajax = new Ajax.Updater("target", "getName.php",
                               { method:'get',
                                 onFailure: function(){alert("Failure");},
                                 onSuccess: function(){alert("success");}
                               });

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked!

    Why did I have to insert:

    function (){} into the statement? Also, my id=n div wasn't updated, why is this?
    Thanks.

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why did I have to insert:

    function (){} into the statement?
    You are passing an object literal to a function. That object literal gets evaluated (turned into an object) before it is passed to the function. During the evaluation the alert was being "called". By wrapping it in a function we only pass a reference to the function which will get called at a later time.

    Also, my id=n div wasn't updated, why is this?
    You have a typo there. You're leaving out the "=" character:
    Your name is: <div id="n">n/a</div>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •