jQuery update a Div with a PHP script

11,619

For simple ajax calls, I normally prefer using $.load as its grammar is extremely concise. Passing parameters as an object (key/value pairs) will cause it to use a POST request:

<a href="no-script.php" class="something">Click!</a>

$(document).ready(function() {
    $('a.something').click(function(e) {
        //prevent the href from being followed
        e.preventDefault();

        //inject div.classname with the output of send.php
        $('div.classname').load('send.php', {param1: 'foo', param2: 'blah'});
    });
});

If you don't need it to be a POST, you can just add your parameters as a query string:

$('div.classname').load('send.php?param1=' + param1 + '&param2=' + param2);
Share:
11,619
Johnny
Author by

Johnny

Updated on June 05, 2022

Comments

  • Johnny
    Johnny almost 2 years

    I have absolutely no idea how to do this, so I'm just gonna go ahead and ask.

    What I want to do is update the contents of a div with a PHP script I have in an external file, called send.php.

    So I have a div like this:

    <div class="classname">
    
    </div>
    

    And I want to post data to this send.php file, and then update that div with whatever the outcome of the PHP script is. Can this be done?