Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Ajax with Prototype

    I can't get the following simple ajax to work.

    I don't get any error messages in FF error console
    The div hides just like it's supposed to do onSuccess
    The sql code never gets executed

    index.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
    <
    head>
    <
    title>Ajax</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <
    script type="text/javascript" src="inc/prototype.js"></script>
    <script type="text/javascript">
    //<![CDATA[
    Event.observe(window, 'load', function() {
        var user = $('new_user');
        if (user) {
            user.href = "#";
            user.setAttribute('onclick', "removeAlert('alert_new_user_home');");
        }
    });
    function removeAlert(val) {
        var url = 'inc/removeAlert.php';
        new Ajax.Request(url, {
            method: 'get',
            parameters: { alert: val },
            onSuccess: function() {
                $(val).toggle();
            },
            onFailure: function() { 
                alert("There was an error with the connection"); 
            }
        });
    }
    //]]>
    </script></head>
    <body>
        <div id="alert_new_user_home" class="info-alert">
            <h2>My Alert</h2>
            <div class="close-alert"><a id="new_user" href="?alert=alert_new_user_home">Delete This Alert</a></div>
        </div>
    </body>
    </html> 
    inc/removeAlert.php
    PHP Code:
    <?php
    $sql 
    "INSERT INTO `alerts` (`alert`, `user`)
            VALUES ('"
    .escape_string($_GET['alert'])."', 1)";
    mysql_query($sql) or die();
    ?>

  • #2
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    1
    Thanked 7 Times in 7 Posts
    From what you provided, it doesn't seem that you're initiating the DB connection...
    http://us.php.net/manual/en/function.mysql-connect.php
    http://us.php.net/manual/en/function.mysql-query.php

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    GET requests get cached, so if the values remain the same on the querystring the browser will not send a response to the server.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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