Post Ajax for PHP Class

0

I have the following code that saves the reordering of 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 my posting is empty, as I do not use it

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

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

There is some way to send the $ list_order directly to the class by ajax, without having to do the post in the index.

Notice: Undefined index: list_order , for the empty post.

    
asked by anonymous 23.08.2016 / 15:34

2 answers

0

One output 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');
}

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

    
23.08.2016 / 17:58
0

In fact, I do not think the colleagues understood the problem. According to the text:

  

... But my posting is empty, as I do not use ...

Assuming the method: <?php echo $auth_task->select(); ?> returns 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 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 you could quietly change the post's target in: url: 'index.php', to any other file, eg: url: 'grava_ordenacao_items.php',

I think that's it! I hope I have helped.

    
29.11.2017 / 02:21