Post Ajax for Class PHP

Asked

Viewed 524 times

0

I have the following code that saves reordering a list.

INDEX.PHP

<?php

    require_once ("Class.Drag.php");

    $auth_task = new Drag();

    $list_order = strip_tags($_POST['list_order']); 

    $res=$auth_task->update($list_order);


?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<link rel="stylesheet" href="css/style.css" />
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="js/script.js"></script>
</head>

<body>
    <div class="container"> 
        <div class="content"> 

        <ul id="sortable">

            <?php 
                echo $auth_task->select();
            ?>
        </ul>

        </div><!-- content -->  
    </div><!-- container -->
</body>
</html>

CLASS.DRAP.PHP

public function update($list_order){

    $list = explode(',' , $list_order);
    $i = 1 ;
    foreach($list as $id) {
        try {

            $stmt = $this->conn->prepare('UPDATE items SET item_order = :item_order WHERE id = :id');
            $stmt->bindParam(':item_order', $i, PDO::PARAM_INT);
            $stmt->bindParam(':id', $id, PDO::PARAM_INT);
            $stmt->execute();

        }catch (PDOException $exception){

        header("Location: ./error.php?err=Cant-Order");
        echo 'Erro: '.$exception->getMessage();
        return null;

    }
        $i++ ;
    }

}

SCRIPTDRAG.JS

$(function() {
    $('#sortable').sortable({
        axis: 'y',
        opacity: 0.7,
        handle: 'span',
        update: function(event, ui) {
            var list_sortable = $(this).sortable('toArray').toString();         
            $.ajax({
                url: 'index.php',
                type: 'POST',                
                data: {list_order:list_sortable},
                success: function(data) {
                    success_message('Gravado');
                },
                error: function(data){
                    failure_message('Erro');
                }
            });
        }
    });
});

Only that my post submission is empty, as I do not use

$list_order = strip_tags($_POST['list_order']); 

$res=$auth_task->update($list_order);

Have some way to send $list_order directly to class through ajax, without having to post on index.

'Cause you’ll always show up Notice: Undefined index: list_order, by empty post.

  • $_POST to record is correct? where is sending his Ubmit? would not use $_GET?

  • Which code snippet yielded the tags <li> of the list?

2 answers

0


An exit would be to check if the method is POST.

//aqui verifica o metodo do envio, GET ou POST
if($_SERVER['REQUEST_METHOD']=='POST'){
    //verifica se o campo 'list_order' existe no $_POST
    if(isset($_POST['list_order'])){
        $list_order = strip_tags($_POST['list_order']); 
        $res=$auth_task->update($list_order);
    }
    //mata o script para nao executar o resto do html
    exit('fim');
}

Also, you can put this snippet of code in a separate index script, maybe a.php update_list, and call it instead of the index in js, url: 'atualizar_lista.php',

  • that’s exactly what I did, thank you.

0

In fact, I believe that colleagues did not understand the problem. According to the text:

... Only that my post submission is empty, as I do not use ...

Supposing that the method: <?php echo $auth_task->select(); ?> return to the following string:

<li>meu item</li>

The problem of sending the empty post, is because the list item does not have an id, and so should be the return of the method string:

<li id="123">meu item</li>

And the jQuery script should look something like this:

$(function() {
    $('#sortable').sortable({
        axis: 'y',
        opacity: 0.7,
        handle: 'span',
        update: function(event, ui) {
            var list_sortable = $(this).sortable('toArray');         
            $.ajax({
                url: 'index.php',
                type: 'POST',                
                data: {list_order: list_sortable},
                success: function(data) {
                    success_message('Gravado');
                },
                error: function(data){
                    failure_message('Erro');
                }
            });
        }
    });
});

And peacefully Voce could change the post’s target on: url: 'index.php', for any other file, ex: url: 'grava_ordenacao_items.php',

I think that’s it! I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.