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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    $.post callback data is empty

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

    Code:
        <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:

    <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:

    <?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
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    I think your page is just refreshing because of the native form action. You can prevent this by adding return false to your click statement.

    Also, although I don't think this is relevant to your issue, you are calling document ready twice - $(function) is equivalent to $(document).ready().

    Have a try with:

    Code:
        <script>
         $(document).ready(function(){
    
            $( "#tabs" ).tabs();
    
            $("#submit").click(function(){
                var post = $('#myForm').serialize();
                $.post("functions.php", post, function(data) {
                    alert(data);
                });
                return false;
            });
        });
        </script>

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great, it worked with 'return false'!

    Thanks!


  •  

    Tags for this Thread

    Posting Permissions

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