为什么我的程序不在我的切换框中输入“deleteregistro”来发送和获取php中的$\u post?

hi3rlvi2  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(361)

我的问题:我不知道为什么,我的朋友 switch 案例不能正常工作,我不知道为什么如果我的ajax status == 'succcess' .
我的onclick功能在我的按钮中:

$data->acción = "<div class='text-center'><div class='btn-group'><button id='modificar_$data->id' class='btn btn-primary btn-sm btnEditar' value='edit'><i class='material-icons'>edit</i></button><button onclick='Delete($data->id, $tableName, $field)' class='btn btn-danger btn-sm btnBorrar'><i class='material-icons' value='delete'>delete</i></button></div></div>";

我的职能:

function Delete(id, tableName, field){
    if (confirm("¿Estás seguro que deseas borrar el registro con ID " + id + "?") == true) {
        $.post("<?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/", { 
            action: "deleteRegistro",
            id: id,
            tableName: tableName,
            field: field
        }, function (data, status){
                if (status === 'error') {
                    console.log("Not deleted");
                }
                else if (status === 'success') {
                    console.log("Deleted successfully");                                
                }
            });
    }
}

我的开关盒:

switch($_POST['action']){
    case 'deleteRegistro':

        $id = $_POST['id'];
        $tableName = $_POST['tableName'];
        $field = $_POST['field'];    
        echo "Hello!";
        ?>
            <script>alert("Hello!");</script>
        <?php 

        break;
}

如您所见,我的ajax status == 'succcess' :

如果您需要检查,这是我的完整代码:

<?php

    use GuzzleHttp\json_decode;
    include_once(DIR_PLUGINS.'/alexcrudgenerator/main.php');

    $test = new GenerateCrud($_POST['tableName'], $_POST['id'], $_POST['tableFields']);

    if ($_GET['action']){
        print_a($_GET['action']);
    }

    switch($_POST['action']){

        case 'datosTabla': // OK.

            //print_r($_POST['action']);
            $res = json_decode($_POST['datos']);
            echo json_encode($res, JSON_UNESCAPED_UNICODE);

            break;

        case 'deleteRegistro':

            $id = $_POST['id']; // Quiero obtener estas variables que he enviado desde la función Delete();
            $tableName = $_POST['tableName']; // Quiero obtener estas variables que he enviado desde la función Delete();
            $field = $_POST['field']; // Quiero obtener estas variables que he enviado desde la función Delete();

            echo "Hello!";
            ?>
                <script>alert("Hello!");</script>
            <?php 

            break;

        case 'showtable': // OK.

            $res = getEntireTable($_POST['tableName'], $_POST['id'], $_POST['tableFields']);
            $tableName = $_POST['tableName'];
            $tableName = json_encode($tableName);

            $field = json_decode($_POST['tableFields'],1)[0];
            //print_r($tableName);
            //print_r('<br>');
            //print_r($campo);

            foreach ($res as $data){                
                $data->acción = "<div class='text-center'><div class='btn-group'><button id='modificar_$data->id' class='btn btn-primary btn-sm btnEditar' value='edit'><i class='material-icons'>edit</i></button><button onclick='Delete($data->id, $tableName, $field)' class='btn btn-danger btn-sm btnBorrar'><i class='material-icons' value='delete'>delete</i></button></div></div>"; 
                $resultados['data'][] = $data;
            }           

            $resultados = json_encode($resultados); // 7 PROPIEDADES

            foreach(json_decode($_POST['tableFields']) as $columnsDB){
                $fields[] = array('data'=>$columnsDB);
            }

            $fields[]['data'] = 'acción';
            $fields = json_encode($fields);

?>
            <head>
                <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
            </head>

            <div class="container caja">
                <div class="row">
                    <div class="col-lg-12 col-sm-12">
                        <div>
                            <table id="tablaUsuarios" class="table table-striped table-bordered table-condensed hover" style="width:100%" >
                                <thead class="text-center">
                                    <tr>
                                        <?php
                                            foreach (json_decode($_POST['tableFields']) as $columnsTH){
                                                 echo '<th>' . strtoupper($columnsTH) . '</th>';
                                            }
                                            echo '<th>ACCIÓN</th>';
                                        ?>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <script>

                function Delete(id, tableName, field){
                    if (confirm("¿Estás seguro que deseas borrar el registro con ID " + id + "?") == true) {
                        $.post("<?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/", { 
                            action: "deleteRegistro",
                            id: id,
                            tableName: tableName,
                            field: field
                        }, function (data, status){
                                if (status === 'error') {
                                    console.log("Not deleted");
                                }
                                else if (status === 'success') {
                                    console.log("Deleted successfully");                                
                                }
                            });
                    }
                }

                $(document).ready(function() {
                    var datos= <?=$resultados?>;
                    var dynamicColumns = <?=$fields?>;
                    datos = JSON.stringify(datos);

                    $('#tablaUsuarios').DataTable({
                        "language": {"url": "https://cdn.datatables.net/plug-ins/1.10.25/i18n/Spanish.json"},
                        "paging": true,
                        "lengthChange": true,
                        "searching": true,
                        "info": true,
                        "autoWidth": true,
                        "scrollX": true,

                        "ajax":{
                            "url": '<?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/',
                            "method": 'POST',
                            "data":{action: "datosTabla", datos: datos}
                        },

                        "columns": dynamicColumns
                    });
                })
            </script>
<?php
        break;      
}
?>

有人能帮我一下吗?我真的不明白。
如果 status == 'success' ,这意味着post请求已成功发送,因此我应该进入我的 case switch 然后打印一个“你好!”。
我做错了什么?

u5rb5r59

u5rb5r591#

你在问题中没有提到 switch 案件正在审理中,我想它已经开始了 <?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/" index.php文件是否正常?
因此,您使用ajax将数据发布到php文件中,ajax成功地发布了数据,但是您怎么会期望这样呢 hello 要在屏幕上显示吗?
正如我在前面的回答中提到的,存在switch case的php文件的输出将不会显示, $.post 方法只是将数据发布到文件,而不会重定向到该文件。
出于您的需要,我建议您使用隐藏表单将数据发布到文件中。
修改后的删除函数将为。。

function Delete(id, tableName, field){
    if (confirm("¿Estás seguro que deseas borrar el registro con ID " + id + "?") == true) {
document.getElementById("postid").value = id;
document.getElementById("postTablename").value = tableName;
document.getElementById("postfield").value = field;
document.forms['deleteform'].submit();
         //Commenting old function
        /*$.post("<?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/", { 
            action: "deleteRegistro",
            id: id,
            tableName: tableName,
            field: field
        }, function (data, status){
                if (status === 'error') {
                    console.log("Not deleted");
                }
                else if (status === 'success') {
                    console.log("Deleted successfully");                                
                }
            });*/
    }
}

现在添加一个表单 deleteform 在html代码中注意:-在将此脚本用于函数之前添加 Delete() ```

相关问题