【问题标题】:$_POST via jquery-php [duplicate]$_POST 通过 jquery-php [重复]
【发布时间】:2018-07-10 07:43:02
【问题描述】:

当我尝试通过 jquery 将值 $_post 转发到 php 脚本时,我收到此通知: "PHP 通知:未定义索引:id"

我在开发者工具中看到了 id 值,fe。 : 'id: 8519/ 8520/ 8521/ 8522' 但在站点中,脚本停在 $_POST['id'] 位置。

这里是部分代码:

        <button class="btn btn-danger" id="export">export xls</button>

        <form action="baza_site.php" method="POST">
                        <input type="text" class="form-control" placeholder="Search" name="search">
                        <button class="btn btn-default" type="submit" name="submit" >
                        <i class="glyphicon glyphicon-search"></i>
                        </button>
                        </div>
            </div>          
        </form>

<?php

                    require_once 'config.php';

                    $output = '';
                    $page = isset($_GET['p'])? $_GET['p'] : '';
                    if($page == 'xls'){
                        $myid = $_POST['id'];
                        $id = str_replace(' ', ',', $myid);

                        $sql = "SELECT * FROM baza_site WHERE id IN($id)";
                        if($result = $mysqli->query($sql)){
                        if($result->num_rows > 0)
        {

         $output .= '
            <table border="1">  
              <tr>  
                <th>id</th>  
                <th>site</th>   
                <th>location type</th>  
                <th>city</th>
                <th>address</th>
                <th>access details</th>
                <th>service area</th>
              </tr>
                    ';
         while($row = mysqli_fetch_array($result))
        {
         $output .= '
                <tr>  
                  <td>'.$row["id"].'</td>  
                  <td>'.$row["site"].'</td>
                  <td>'.$row["locationType"].'</td>  
                  <td>'.$row["city"].'</td>  
                  <td>'.$row["address"].'</td>
                  <td>'.$row["accessDetails"].'</td>
                  <td>'.$row["ServiceArea"].'</td>

                </tr>
                    ';
        }
         $output .= '</table>';

  header('Content-Type: application/vnd.ms-excel');
  header("Content-Disposition: attachment; filename=".'woclosed'.date("Y-m-d_H_i_s").".xls");
  header("Pragma: no-cache");
  header("Expires: 0");
  echo $output;
 }
                    }}

                    require_once 'config.php';

                    if (isset($_POST["submit"])) {
                    $search = mysqli_real_escape_string($mysqli, trim($_POST['search']));
                    mysqli_set_charset( $mysqli, 'utf8');

                    $sql = "SELECT * FROM baza_site WHERE site LIKE '%$search%' OR city LIKE '%$search%' OR address LIKE '%$search%' OR accessDetails LIKE '%$search%' OR ServiceArea LIKE '%$search%' OR locationType LIKE '%$search%'";
                    if($result = $mysqli->query($sql)){
                        if($result->num_rows > 0){
                            $ilosc = mysqli_num_rows($result);
                            echo "<b>ilość rekordów : " .$ilosc."</b>";
                            echo "<table id='myTable' class='tablesorter-blackice'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th style='text-align:center;' class='filter-false'><input type='checkbox' id='checkall'/></th>";
                                        echo "<th>site</th>";
                                        echo "<th>location type</th>";
                                        echo "<th>city</th>";
                                        echo "<th>address</th>";
                                        echo "<th>access details</th>";
                                        echo "<th>service area</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                while($row = $result->fetch_array()){
                                    echo "<tr>";
                                        echo "<td class='text-center'>"."<input type='checkbox' name='checkboxlist' class='checkitem' value=". $row['id']."/> </td>";
                                        echo "<td>" . $row['site']. "</td>";
                                        echo "<td>" . $row['locationType']. "</td>";
                                        echo "<td>" . $row['city'] . "</td>";
                                        echo "<td>" . $row['address'] . "</td>";
                                        echo "<td>" . $row['accessDetails'] . "</td>";
                                        echo "<td>" . $row['ServiceArea'] . "</td>";


                                    echo "</tr>";
                                }
                                echo "</tbody>";                            
                            echo "</table>";
                            // Free result set
                            $result->free();
                        } else{
                            echo "<p class='lead'><em>Brak informacji w bazie.</em></p>";
                        }
                    } else{
                        echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
                    }
                    }
                    // Close connection
                    $mysqli->close();
                    ?>  



    </div>
    </div>
                        <script type="text/javascript">

            $("#myTable").tablesorter({
    headers: {
      0: { sorter: false, parser: false }
    }
});

<script type="text/javascript">

        $('#checkall').change(function(){
            $('.checkitem').prop("checked", $(this).prop("checked"))
        })

        $('#export').click(function(){
            var id = $('.checkitem:checked').map(function(){
                return $(this).val()
            }).get().join(' ')
            $.post('baza_site.php?p=xls', {id : id})
            });

</script>

【问题讨论】:

  • 请尽量缩短工作代码。请参考here
  • 问题不是很明显吗?您必须在$output 变量之上定义一个名为$id. 的变量类型$id='';

标签: php jquery mysql excel xls


【解决方案1】:

通过查看您的代码,我们注意到没有名称为“id”的输入字段。

使用表单值的正确格式是:$_POST['inputname']

结果你会发现'id'是未定义的。

假设您要处理搜索结果,您可能希望改用 $_POST['search'],因为这是您为字段命名的方式。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 2016-08-09
    • 2012-10-13
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2020-01-25
    • 2019-09-16
    相关资源
    最近更新 更多