Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 02:28 PM   PM User | #1
Cobb
New Coder

 
Join Date: Aug 2011
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
Cobb is an unknown quantity at this point
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.
Cobb is offline   Reply With Quote
Old 09-24-2012, 02:35 PM   PM User | #2
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
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/
nomanic is offline   Reply With Quote
Old 09-24-2012, 02:38 PM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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
devnull69 is offline   Reply With Quote
Old 09-24-2012, 02:43 PM   PM User | #4
Cobb
New Coder

 
Join Date: Aug 2011
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
Cobb is an unknown quantity at this point
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.'.
Cobb is offline   Reply With Quote
Old 09-24-2012, 02:45 PM   PM User | #5
nomanic
Regular Coder

 
nomanic's Avatar
 
Join Date: Feb 2009
Location: United Kingdom
Posts: 252
Thanks: 9
Thanked 33 Times in 33 Posts
nomanic is an unknown quantity at this point
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/
nomanic is offline   Reply With Quote
Old 09-24-2012, 07:36 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
If you want to support non-english characters using utf-8 you should use encodeURIComponent() instead of escape()
devnull69 is offline   Reply With Quote
Old 09-24-2012, 07:59 PM   PM User | #7
Cobb
New Coder

 
Join Date: Aug 2011
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
Cobb is an unknown quantity at this point
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.
Cobb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:15 AM.


Advertisement
Log in to turn off these ads.