Using jQuery, AJAX and PHP

I am creating a PHP site and faced the following problem: I would like to be able to click the hyperlink on the page, send a small data item to the php script, execute the script and then return the results.

I use AJAX with this site to load each page into the div section of the site, all pages, data and responses are loaded into this central div, for example:

<html>
    <body>
        <div id="topMenuBar">
            <div id="contents">
            //ALL DATA IS LOADED HERE...//
            </div>
        </div>
    </body>
</html>

So, when the page is selected from the top menu, I just run the following code:

$('#topMenuBar a').click(function(e)
{
    e.preventDefault();
    $('#contents').load($(this).attr('href'), function()
    {
    });
});

, , "results.php", , HTML . , PHP script, div div script (getInfo.php). , PHP, :

<label class="moreInfo"><a name="test01" onClick="getInfoFromPHP(<?php echo $data[$id]; ?> )">Get Info</a></label>

, PHP, JS.

PHP script, , div "contents". .

function getInfoFromPHP(myVar){
    var netID = myVar;
    $.ajax({
        url: "getInfo.php",
        type: "POST",     
        data: { 
            networkID: netID
        }, 
        success: function(html) {
            $('#contents').empty();
            $('#contents').load(html);
        }      
    });
};

, , , script, firebug:

POST http://127.0.0.1/private/networks/includes/leave_a_network.php - 200 OK -15ms
GET http://127.0.0.1/%3Ch2%3EHello 403 21 "NetworkError: 403 Forbidden - http://127.0.0.1/%3Ch2%3EHello"

PHP :

<?php
    session_start();
    $networkID = $_POST['networkID'];
    echo "<h2>Hello World</h2>";
?>

PHP script div?

.

+3
3

$('# contents'). html (html); .

+9
success: function(html) {

html , , -

$('#contents').load(html);

$('#contents').html(html);
+4

All Articles