SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery - $.post callback data is empty

    I have the following in the header if index.php:

    Code JavaScript:
    <script type="text/javascript" src="/jquery/jqueryui/js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="/jquery/jqueryui/js/jquery-ui-1.8.6.custom.min.js"></script>
        <script type="text/javascript" src="/jquery/jqueryui/development-bundle/ui/jquery.ui.tabs.js"></script>
        <script>
        $(document).ready(function(){
            $(function() {
                $( "#tabs" ).tabs();
            });
            $("#submit").click(function(){
                var post = $('#myForm').serialize();
                $.post("functions.php", post, function(data) {
                    alert(data);
                });
            });
        });
        </script>

    The relevant body of index.php is:

    Code HTML4Strict:
    <form action="" method="post" enctype="multipart/form-data" name="myForm" id="myForm">
                  <label for="keyword">Keyword</label>
                  <input type="text" name="keyword" id="keyword" />
                  <input type="submit" name="submit" id="submit" value="Submit" />
              </form>

    Functions.php has the following:

    Code PHP:
    <?php require_once('../Connections/cms.php'); ?>
        <?php
        $myFile = "testFile.txt";
        $fh = fopen($myFile, 'w');
     
        // And then the code to run a query with the keyword and print results in a table


    When I refresh index.php and click on 'Submit', it DOES create testFile.txt - meaning it is correctly referring to functions.php, but the alert is empty.

    I then delete testFile.txt and click on 'Submit' without refreshing, and this time, it does NOT create testFile.txt - and hence, my guess is that it is not calling on functions.php, and of course, alert is empty as usual.

    What I want to do is this:
    I want to be able to click on the Submit button - without refreshing the page - and return data from functions.php and display it within a div tag on index.php

    What am I doing wrong to get an empty alert?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by m.johnson View Post
    What am I doing wrong to get an empty alert?
    First of all check that functions.php is returning some data.

    using no javascript, have a form post data to Functions.php

    Do you see anything after posting? If not, no data is capable of making it back to the script.

    Once you know that Functions.php is providing data, you can move in with troubleshooting the scripting.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, the php file, functions.php has the following right in the beginning:

    print_r($_POST);

    The alert should show all posted values, at least, but it doesn't. The script obviously does.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by m.johnson View Post
    Hi, the php file, functions.php has the following right in the beginning:

    print_r($_POST);

    The alert should show all posted values, at least, but it doesn't. The script obviously does.
    Let's try and rule out the PHP code from being the cause of the problem.

    Does the ajax script work when you entirely replace functions.php with some static content?

    Functions.php
    Code php:
    <?php
    echo 'Some static test content.';
    ?>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, it doesn't work...

    You can replace php script with some static .html file and even that wouldn't be shown.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by m.johnson View Post
    Nope, it doesn't work...

    You can replace php script with some static .html file and even that wouldn't be shown.
    That's good to confirm. In several hours I'll be able to dedicate some time towards looking into this.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

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
  •