繁体   English   中英

Ajax成功函数触发数据

[英]Ajax success function trigger data

我的代码有问题。 可以说我在后台有PHP网站,并且该网站在做什么。 可以说,在此站点中,我有$status = "1"; 在我的ajax代码中,我有:

$.ajax({
    url: 'src/single.php',
    type: 'POST',
    data: {single:1, id:id},
    success: function(data){
        $('#live_data').val('');
        $('#live_data').html(data);
    }
});

我想知道是否有可能从我的PHP页面触发$ status。 就像是

success: function(data){
   if (data.status == '1') {
        $('#live_data').val('');
        $('#live_data').html(data);
   } if (data.status == '2') {
        $('#search_result').val('');
        $('#search_result').html(data);
   }

如果可以的话,谢谢您的帮助:)

编辑:

所以这是我的代码:

第一个用于从db获取数据,第二个用于从db获取搜索数据,第三个用于从db获取单个数据。 它工作正常,但问题是,当我单击行时,它首先从显示中显示结果2次,而从搜索中显示结果第二次,但是在搜索中它是正确的。

$('#live_data')用于show.php数据, $('#search_result')用于搜索数据。 当我单击show.php中的行时,它将显示结果2次。 首先来自show.php,其次来自search.php,但是当我单击search.php中的行时,它正确显示(只有一个结果)。 我知道是因为我有

$('#live_data').val('');
$('#live_data').html(data);

但是当我想隐藏来自show.php的重复结果时,我会执行$('#search_result').val(''); 但后来我从search.php隐藏了我的单个数据

这就是为什么我想用if语句来控制它

我在下面附加图片

用于单个数据获取的ajax:

//jeden zaznam
    $(document).on('click', '.clickable-row', function(){

        var id = $(this).data("id2");

        $.ajax({
            url: 'src/single.php',
            type: 'POST',
            data: {single:1, id:id},
            success: function(data){
                $('#live_data').val('');
                $('#live_data').html(data);
                $('#search_result').html(data);
            }
        });
    });

第一:

<?php 

    include("db.php");

    $output = "";
    $sql = "SELECT * FROM otk";

    $result = mysqli_query($conn, $sql);

    $output .= "<table class='table table-hover'>
                    <thead>
                        <tr>
                            <th>Číslo zákazky</th>
                            <th>Pozícia</th>
                            <th>Stav</th>
                            <th>Dátum</th>
                            <th>Operátor</th>
                        </tr>
                    </thead>";
    while ($row = mysqli_fetch_array($result))
    {
        $output .= "<tr class = 'clickable-row' data-id2 ='".$row['id_otk']."'>
                        <td>".$row['kod_otk']."</td>
                        <td>".$row['poz_otk']."</td>
                        <td>".$row['stav_otk']."</td>
                        <td>".$row['datum_otk']."</td>
                        <td>".$row['op_otk']."</td>
                    </tr>";
    }

    $output .= "</table>";

    echo $output;

?>

第二:

<?php 

if (isset($_POST['search']))
{
    include("db.php");

    $search_text = mysqli_real_escape_string($conn, $_POST['search_text']);
    $search = htmlspecialchars($search_text);

    $output = "";
    $output .= "
        <table class='table table-hover'>
                    <thead>
                        <tr>
                            <th>Číslo zákazky</th>
                            <th>Pozícia</th>
                            <th>Stav</th>
                            <th>Dátum</th>
                            <th>Operátor</th>
                        </tr>
                    </thead>";

    $sql = "SELECT * FROM otk WHERE kod_otk LIKE '%".$search."%'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0)
    {
        while($row = mysqli_fetch_array($result))
        {
            $output .= "
                <tr class = 'clickable-row' data-id2 ='".$row['id_otk']."'>
                    <td>".$row['kod_otk']."</td>
                    <td>".$row['poz_otk']."</td>
                    <td>".$row['stav_otk']."</td>
                    <td>".$row['datum_otk']."</td>
                    <td>".$row['op_otk']."</td>
                </tr>";
        }

        $output .= "</table>
                        <div class='d-flex justify-content-center'>
                        <button class='btn btn-primary' onclick='history.go();'>Späť</button>
                    </div>";

        echo $output;

    } else {
        echo "Žiadny záznam";
    }
}


?>

第三:

<?php

if (isset($_POST['single']))
{
    include("db.php");

    $id = mysqli_real_escape_string($conn, $_POST['id']);
    echo "id je: ".$id;

    $sql = "SELECT * FROM otk WHERE id_otk = '".$id."'";
    $result = mysqli_query($conn, $sql);

    $output = "<table class='table table-hovrt'>
                    <thead>
                        <tr>
                            <th>Číslo zákazky</th>
                            <th>Pozícia</th>
                            <th>Stav</th>
                            <th>Poradové číslo</th>
                            <th>Technológia</th>
                            <th>Dokument</th>
                            <th>Zariadenie</th>
                            <th>Operátor</th>
                            <th>Dátum</th>
                        </tr>
                    </thead>";

    if (mysqli_num_rows($result) > 0)
    {
        while ($row = mysqli_fetch_array($result))
        {
            $output .= "
                <tr>
                    <td>".$row['kod_otk']."</td>
                    <td>".$row['poz_otk']."</td>
                    <td>".$row['stav_otk']."</td>
                    <td>".$row['cislo_otk']."</td>
                    <td>".$row['tech_otk']."</td>
                    <td>".$row['dok_otk']."</td>
                    <td>".$row['zar_otk']."</td>
                    <td>".$row['op_otk']."</td>
                    <td>".$row['datum_otk']."</td>
                </tr>";
        }

        $output .= "</table>
                    <div class='d-flex justify-content-center'>
                        <button class='btn btn-primary' onclick='history.go(-2);'>Späť</button>
                    </div>";

        echo $output;
    } else {
        echo "Error: " .  mysqli_error($sql);
    }
}



?>

Image01:

在此处输入图片说明

Image02: 在此处输入图片说明

您必须使用以下方法获取single变量和id变量:

<?php
$single = $_POST['single'];
$id = $_POST['id'];

// do your logic and set it's status

echo json_encode(['status' => $status]);

然后,您将能够在成功数据中检索状态参数。

最好的选择是使用json_encode()

例:

$result = array();

$result = array('status' => '1');

echo json_encode(result);

通过以上操作,您将能够通过jquery访问status键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM