SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Execute PHP Code on Form Submit (Ajax?)

    I have a small amount of PHP code that I want to execute when a user clicks on a form submit button. The form input goes to an external server using the POST method. I could have the submit button pop up the PHP page in a separate window which would then close itself, but I'd rather not do that if there's a better option.

    So I'm guessing Ajax is the way to go? I know how to use Ajax to retrieve content from another page, but I can't figure out how to get it to just run the page in the background without returning any output.

    Can anyone help? Thanks!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    http://bulletproofajax.com/code/

    The sample code for Chapter 4 should do you nicely.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. That's almost identical to what I've been using for another page. I still can't quite get this to work, though. Here's the code:

    Code:
    function getHTTPObject()
    {
        var xhr = false;
        if(window.XMLHttpRequest)
        {
            xhr = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            try
            {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)
                {
                    xhr = false;
                }
            }
        }
        return xhr;
    }
    function grabFile(file)
    {
        var request = getHTTPObject();
        if (request)
        {
            request.open("GET", file, true);
            request.onreadystatechange = function()
            {
                parseResponse(request);
            }
            request.send(null);
        }
    }
    function parseResponse(request)
    {
        if(request.readyState==4)
        {
            if (request.status == 200 || request.status == 304)
            {
                var details = document.getElementById("ajax_details");
                details.innerHTML = request.responseText;
            }
        }
    }
    Here's the onclick attribute for the button in question:

    Code:
    onclick="javascript:grabFile('http://www.domain.com/page.php');"
    And here's the Javascript error that I get:

    Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://www.domain.com/js/ajax_form_bounce.js :: parseResponse :: line 45" data: no]
    Source File: http://www.domain.com/js/ajax_form_bounce.js
    Line: 45

    Any ideas? Anyone? Bueller? Bueller?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    There is a possible solution at http://groups.google.com/group/JSMX/...%2F2008-01%3F&

    In short, insert an alert statement just before the request.responseText line to see what data that you're trying to work with.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •