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 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    XMLHttp Request problem

    I've written a XMLHtttpRequest, but something is breaking down somewhere.

    PHP Code:
    function submit(){
         
    // declaring variables to be used
        
    var xhrtargetchangeListenerurldata;

        
    //setting url to the php code to add comments to the db
        
    url "createCampaign.php";
        
    data document.getElementById("source").innerHTML;
        
    console.log("Sending"data);

        
    // create a request object
        
    xhr = new XMLHttpRequest();

        
    changeListener = function () {
            if (
    xhr.readyState == 4) {
                if (
    xhr.status == 200) {
                    
    console.log("Response"this.responseText);
        
    //            result = JSON.parse(this.responseText);
                    //injectContent(result.id, form);
                
    }
            }
        };

        
    // initialise a request, specifying the HTTP method
        // to be used and the URL to be connected to.
        
    xhr.onreadystatechange changeListener;
        
    xhr.open('POST'urltrue);
        
    xhr.setRequestHeader("Content-type""application/x-www-form-urlencoded");
        
    xhr.send(data);

        return 
    false;

    I've tried changing the data variable so that it contains just a simple string but it still does not work.

    In the PHP script i'm trying to see if anything has been received by using this line 'echo $_REQUEST['data'];' but it appears that nothing has been sent.

    Any help will be greatly appreciated.
    Thank you.

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    it might be that xhr is private to the outer function, and will be unknown inside the inner function of changelistener? try making it a page variable that has scope right across all the functions
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Two mistakes

    1. data is just a plain string. It should be in the format "key1=value1&key2=value2&...." so that you can access the data using $_POST["key1"] and $_POST["key2"] in PHP

    2. Try to replace this.responseText with xhr.responseText

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yeah i just noticed the first mistake, but now I cant work out how to write that line.
    data = 'html=document.getElementById("source").innerHTML'; sends it a string instead of the contents of the innerHTML.

    The second line you pointed out works fine as 'this.'.

  • #5
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    Code:
    data = 'html='+escape(document.getElementById("source").innerHTML);
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    If you want to support non-english characters using utf-8 you should use encodeURIComponent() instead of escape()

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for all your help, it worked. Although i realised in the end that sending it through as a form was most functional for what I was doing.


  •  

    Posting Permissions

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