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 10 of 10
  1. #1
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Get PHP contents from JQuery

    I can use the following JQuery function to return PHP results after a form submission:

    Code:
        $(".some_class").bind("submit", function () {
            $.ajax({
                type: "POST",
                cache: false,
                url: "some_file.php",
                data: $(this).serialize(),
                success: function (data) {
                    $('#php_results').html(data)
                }
            });
            return false;
        });
    Is there a way to get PHP results without submitting a form? Thanks ...

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,134
    Thanks
    12
    Thanked 332 Times in 328 Posts
    er, you’re already doing that.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for your reply. I'm trying to display the contents of a PHP file in a div (#results) without submitting a form. The above code uses a form submission.

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Quote Originally Posted by q1h View Post
    Thanks for your reply. I'm trying to display the contents of a PHP file in a div (#results) without submitting a form. The above code uses a form submission.
    how do you want to trigger it? on page load? on a click event? on a timer? also without the form your not submitting any query. if you just want to load a simple php file then:

    Code:
    $('#php_results').load("somefile.php");
    
    //or on document ready when the dom loads it will happen
    $(document).ready(function(){
    $('#php_results').load("somefile.php");
    });
    Last edited by DanInMa; 01-05-2012 at 04:58 AM.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,134
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by q1h View Post
    The above code uses a form submission.
    nope. though you hook in on the form’s submit event, you cancel the form submission on the last line.

    and if it were indeed submitting the form, the AJAX call would be pointless because when the response comes back, the handling JS code is gone due to the (re)load.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Yeah, I was trying to bind a link to the function. I tried this:

    Code:
    $(".refresh").click(function(){
            $('#results').load("data.php");
        });
    But it doesn't work. Did I do something wrong? Thanks ...

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    When do you trying binding to the click function? Before or after the page loads?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I'm trying to do it after the page loads (otherwise I would just reload the page via js). I've also used:

    Code:
    $(".refresh").live("click", function () {
            $('#results').load("data.php");
        });

  • #9
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    perhaps this then. also if data.php needs some information posted to it to return a result, this is not good enough becuase you are nto capturing and sending any data to data.php

    Code:
    $(document).ready(function(){
    $(".refresh").live("click", function () {
            $('#results').load("data.php");
        });
      });

  • #10
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Yea, I've tried that as well. My php file echoes lorem ipsum for testing - maybe I have another conflicting script running. Thanks for the replies ...


  •  

    Posting Permissions

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