jQuery doesn't work after an Ajax post

Posted by user1758979 on Stack Overflow See other posts from Stack Overflow or by user1758979
Published on 2012-10-19T10:34:24Z Indexed on 2012/10/19 11:02 UTC
Read the original article Hit count: 211

Filed under:
|

I'm using jQuery to sort a list of entries, between <LI></LI> tags, and then an Ajax post to validate the order and 'update' the page with the content returned.

$.ajax({url: "./test.php?id=<?php echo $id; ?>&action=modify",
    contenttype: "application/x-www-form-urlencoded;charset=utf-8",
    data: {myJson:  data},
    type: 'post',
    success: function(data) {
        $('html').html(data);
        OnloadFunction ();
        }
    });

Then, I lose the ability to sort the list (I'm not sure if clear...). I tried to move the content of the $(document).ready inside the OnloadFunction (), and call it with <script>OnloadFunction ();</script> inside the block dealing with the modifications to do :

$action= $_GET['action'];
if ($action == "modify") {
// Code here
}

but it doesn't work...

I can't figure out how to do that. Could anyone help ?

I stripped out the main part of the code to keep only the essential (filename: test.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="jquery-ui-1.9.0.custom.min.js"></script>
    <script>
    $(document).ready(function(){ 
        //alert("I am ready");
        OnloadFunction ();
        }); 

    function OnloadFunction () {
            $(function() {
            $("#SortColumn ul").sortable({ 
                opacity: 0.6, cursor: 'move',
                update: function() {}                                 
                });
            });
            //alert('OnloadFunction ends');
        }

    function valider(){
        var SortedId = new Array(); 
        SortIdNb = 0;
        $('#SortColumn ul li').each(function() {
            SortedId.push(this.id);
        });
            var data = {
            /* Real code contains an array with the <li> id */
            CheckedId: "CheckedId",
            SortedId: SortedId,
            };
        data = JSON.stringify(data);
        $.ajax({url: "./test.php?id=<?php echo $id; ?>&action=modify",
            contenttype: "application/x-www-form-urlencoded;charset=utf-8",
            data: {myJson:  data},
            type: 'post',
            success: function(data) {
                //alert(data);
                $('html').html(data);
                OnloadFunction ();
                }
            });
        }
    </script>
</head>
<body>
<?

$action= $_GET['action'];
$id = $_GET['id'];
if ($id == 0) {$id=1;}
$id += 1;
if ($action == "modify") {
    echo "action: modify<br>";
    echo "id (àvèc aççént$): ".$id."<br>"; // "(àvèc aççént$)" to check characters because character set is incorrect after the ajax post
    $data = json_decode($_POST['myJson'], true);
    // PHP code here to treat the new list send via the post and update the database
    print_r($data);
    }
?>
<!-- PHP code here to get the following list from the database -->
<div id="SortColumn">
    <ul>
        <li id="recordsArray_1">recordsArray_1</li>
        <li id="recordsArray_2">recordsArray_2</li>
        <li id="recordsArray_3">recordsArray_3</li>
        <li id="recordsArray_4">recordsArray_4</li>
        <li id="recordsArray_5">recordsArray_5</li>
    </ul>
</div>
<input type="button" value="Modifier" onclick="valider();">

</body>
</html>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX