SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sending data to php w/ AJAX

    I'm having a weird problem, not sure if I'm overlooking something or leaving something out. I'm trying to send values of a form to through AJAX to a php script then return a value. It works fine, except the author field is always blank. I've created a simplified version and it still works weird.

    This is the javascript used to get the values and send them, when they come back the alert message shows Name as blank. I've looked in the PHP file to see if I made a mistake but I don't see anything.

    Code:
    function postComment() {
    var author = document.getElementById("author").value;
    var email = document.getElementById("email").value;
    var comment = document.getElementById("comment").value;
    var url = "includes/testComment.php";
    request.open("POST", url, true);
    request.onreadystatechange = showResult;
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send("author="+escape(author)+"&email="+escape(email)+"&comment="+escape(comment));	}
    
    function showResult() {
    if(request.readyState == 4) {
    	var message = request.responseText;
    	alert(message);	
    }
    }
    HTML Code:
    <form>Name:<br />
    <input type="text" id="author" name="poster" size="25" /><br />
    Email:<br />
    <input type="text" id="email" name="email" size="25" /><br />
    Comment:<br />
    <textarea cols="36" rows="7" id="comment" name="comment"></textarea>&nbsp;
    <input type="button" onclick="postComment();" name="submit" value="Comment" /></form>
    PHP Code:
    $author trim($_REQUEST['author']);
    $email trim($_REQUEST['email']);
    $comment trim($_REQUEST['comment']);
    $response "Name = ".$author." email = ".$email." comment = ".$comment;
    echo 
    $response
    Half way to nowhere

  2. #2
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your html code with the fomr instead of name="poster" you should put in name="author"

  3. #3
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't make a difference either, I thought it is grabbing the value based on id name. Last night it started working without me doing anything. Now again it isn't working, sometimes showing email blank too. But without refreshing the page I can try commenting and they all comeback fine. So not sure if the javascript is having problems or if it is when it is being sent to the php script. Just thought to test in IE and it is coming back blank and undefined.
    Half way to nowhere

  4. #4
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I always access the input variables through POST and then input name. In FF go tot he javascript console and see if its throwing any errors at you.

  5. #5
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the link so you can see my request function too, not sure if that is the problem, I'm really lost. Also, I know the code is a little sloppy, I'm wrote a mock-up to test the code. Again works usually the second try for FF never in IE.

    Code:
    www.wordlocker.com/poems/another-poem
    Half way to nowhere

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I had to add some code to make your script work, but when I did it worked fine in both IE6 and FF1.x.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the link so you can see my request function too, not sure if that is the problem
    That fails in both IE6 and FF1.x, and the IE6 error tells you exactly what's wrong.

  8. #8
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you possibly tell me what you added??? I was testing in FF2, but never got an error message in IE7.
    Half way to nowhere

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Copy and paste the makeRequest() function at that url and put it on an html page by itself. Then follow it with the line:

    makeRequest();

    Load the page and see what happens.

  10. #10
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    http://www.wordlocker.com/poems/test.html
    I get no error messages but on this machine I only have FF2 and IE7. But firebug doesn't even show errors.
    Half way to nowhere

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry! My error. I didn't realize that if statement at the bottom of makeRequest() was part of the function.

    Using that makeRequest() function along with the rest of the code you posted in this thread, I get no errors in IE6 or FF1.x. The alert shows the entered data just fine. I tried all kinds of escaped charcaters in the text and everything was displayed just as it was entered.
    Last edited by 7stud; Nov 18, 2006 at 03:27.

  12. #12
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the feed back, I'm baffled why it doesn't wok for me. Can anyone else test this using FF2 and IE7 then and see if they have problems?
    Half way to nowhere

  13. #13
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post a link or are you only doing this on your localhost?

  14. #14
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Link was above:
    Code:
    www.wordlocker.com/poems/another-poem
    I also tested in IE6 and it isn't working, so not sure why it worked for the above poster.
    Half way to nowhere

  15. #15
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that link is bad. I get a 404

  16. #16
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I not suppose to get help, lol. I'm looking at that address right now???
    Half way to nowhere

  17. #17
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm looking at it now.

  18. #18
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So did you figure it out, I'm still stumped!!
    Half way to nowhere

  19. #19
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry man my expertise although limited is in php and not ajax.

  20. #20
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, figured the problem, just didn't figure a fix. The problem is conflicting javascript it seems. The form is inside ibox and that seems to be the problem, I took the form out of it just to test and it works fine in IE and FF.
    Half way to nowhere

  21. #21
    SitePoint Addict
    Join Date
    May 2003
    Location
    sitepoint
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I figured it out, thanks for the help though.
    Half way to nowhere

  22. #22
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem glad we could be of some assitance to you.


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
  •