SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with JQuery post function

    I'm trying to process a form with php via the jquery post function. The first part, with serializing the data is working. I'm alerting out the data and everything looks good. But I'm not getting a response back from php. To debug this I have tried just simply echoing back a text message and that is not even working. What am I doing wrong?

    Here is my query:
    Code:
    $(document).ready(function(){
         
      $(".vote_btn").click(function() {                          
       
        var formData = $('#form').serialize();
        alert(formData); // this is alerting the correct info
        $.post('scripts/proc_form2.php', formData, processResponse);
      });
    
      function processResponse(data){
        alert(data); // the alert never triggers
      }
              
    }); // end doc.ready
    Here is the simplified php file
    PHP Code:
    <?php
    echo "made it to php and back!!!!";

    ?>

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Is the URL okay? Depending on where you are on the website, you might have to change the URL of your script to /scripts/proc_form2.php

    Also, take a look at the Net(work) panel in Firebug / Chrome Development Tools to see what's happening with the request (request headers, response headers, etc).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried both with and without the '/' in front of scripts. The php file is in a script folder. The script folder is at the same level as the index file with the form. I think the path should be ok though because I can use the same path in the forms action attribute and the php runs.

    I'm not really sure what to look for in the network panel. I really only use the elements and console panels. I see a post method with status 200 / ok is that what you mean?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aaron4osu View Post
    I tried both with and without the '/' in front of scripts. The php file is in a script folder. The script folder is at the same level as the index file with the form. I think the path should be ok though because I can use the same path in the forms action attribute and the php runs.

    I'm not really sure what to look for in the network panel. I really only use the elements and console panels. I see a post method with status 200 / ok is that what you mean?
    That's what I meant, yes. So that part seems to be okay as well. To be honest, I'm kinda stumped. The only think I can think of is that normally you don't pass a function identifier but an anonymous function to $.post. I don't think it'll matter, but for arguments sake could you try the following?

    Code javascript:
    $(document).ready(function() {
      $(".vote_btn").click(function() {
        var formData = $('#form').serialize();
        alert(formData); // this is alerting the correct info
        $.post('scripts/proc_form2.php', formData, function(data) {
            alert(data); // the alert never triggers
        });
      });
    });

    PS. I don't know which jQuery version you're on, but if you're on >= 1.7 it's more efficient to use $(document).on('click', '.vote_btn', function() {...}) instead of $('.vote_btn').click(function() {...});. This is known as event delegation. See for example http://mark-story.com/posts/view/spe...n-and-bubbling for an explanation.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for the help. I tried ur snipped of code and still no luck. I'm using jquery 1.7.2

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Very weird. I'm clueless as to why that doesn't work
    Someone else maybe? @paul_wilkins ; maybe?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries. I ended up not using query to process the form. I am still curious as to why though. But no need to spend any more time on my part. Thanks for your efforts though.


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
  •