PHP - 提交日期/时间/从foreach循环中选择输入值到$ _POST不返回任何内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP - 提交日期/时间/从foreach循环中选择输入值到$ _POST不返回任何内容相关的知识,希望对你有一定的参考价值。

我在使用post方法将页面中的输入值从页面提交到另一个页面时遇到问题;当我尝试使用$ _POST ['input_name']获取值时,它不会返回任何内容,并且我在发送数据的页面上收到“未定义的索引”通知。传递correclty的唯一值是['studentid']。表单位于foreach循环内部。我是php的新手,所以我可能会忽略一些非常基本的东西。

首页代码:

<?php
$aStudents = NULL;
$aStudents = $MobilityClass->GetFlowStudents($id_flow, $oConn);

if (count((array) $aStudents) > 0) {
    $k = 0;
    foreach ($aStudents as $aResult) {
        $k = $k + 1;
        ?>
        <tr>
            <td><?php echo($k); ?></td>
            <td><a class="" title="edit" href="student.php?id_student=<?php echo($aResult['id_student']); ?>"><?php
        echo($aResult['student_surname']);
        echo(' ');
        echo($aResult['student_name']);
        ?> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
                    <?php
                    $studentid = "";
                    $studentid = $aResult['id_student'];
                    ?>
            </td>
            <td>
                <?php
                $data_par = null;
                $datap = null;
                $datap = mysqli_query($oConn, "SELECT DATE_FORMAT(data_partenza, '%d-%m-%Y') as data_partenza from flight_details where id_student = " . $studentid . ";");
                $data_par = mysqli_fetch_array($datap);
                if ((string) $data_par[0] == '00-00-0000') {
                    $data_par[0] = null;
                    $formdatea = null;
                } else {
//                                       echo $data_par[0];
                    $formdatep = DateTime::createFromFormat('d-m-Y', $data_par[0])->format('Y-m-d');
                }
                echo $formdatep;
                ?>
                <input  form="update_form" value="<?php echo $formdatep; ?>"  class="form-control" type="date" name="data_partenza">
                <input form="update_form" type="hidden" value="<?php echo $url; ?>" name="url">

            </td>
            <td>
                <?php
                $data_arr = null;
                $dataa = null;
                $dataa = mysqli_query($oConn, "SELECT DATE_FORMAT(data_arrivo, '%d-%m-%Y') as data_arrivo from flight_details where id_student = " . $studentid . ";");
                $data_arr = mysqli_fetch_array($dataa);
                if ((string) $data_arr[0] == '00-00-0000') {
                    $data_arr[0] = null;
                    $formdatea = null;
                } else {
//                                       echo $data_par[0];
                    $formdatea = DateTime::createFromFormat('d-m-Y', $data_arr[0])->format('Y-m-d');
                }
                echo $formdatea;
                ?>
                <input form="update_form" type="date" name="data_arrivo" class="form-control" id="name" value="<?php echo $formdatea; ?>">
            </td>
            <td>
                <?php
                $orap = mysqli_query($oConn, "SELECT DATE_FORMAT(ora_partenza, '%H:%i') as ora_partenza from flight_details where id_student = " . $studentid . ";");
                $ora_par = mysqli_fetch_array($orap);
                ?>
                <input form="update_form" type="time" name="ora_partenza" class="form-control" id="name" value="<?php echo $ora_par[0]; ?>">
            </td>
            <td>
                <?php
                $oraa = mysqli_query($oConn, "SELECT DATE_FORMAT(ora_arrivo, '%H:%i') as ora_arrivo from flight_details where id_student = " . $studentid . ";");
                $ora_arr = mysqli_fetch_array($oraa);
                ?>
                <input form="update_form" type="time" name="ora_arrivo" class="form-control" id="name" value="<?php echo $ora_arr[0]; ?>">
            </td>
            <td>
                <select form="update_form" name="luogo_partenza" class=" form-control" >
                    <?php
                    $departure = mysqli_query($oConn, "select luogo_partenza from flight_details where id_student = " . $studentid . ";");
                    $loc_departure = mysqli_fetch_array($departure);
                    $countries = array();
                    $countries = mysqli_query($oConn, "select id,name from countries");

                    foreach ($countries as $country) {
                        if (trim($loc_departure[0]) == trim($country['name'])) {
                            $selected = 'selected';
                        } else {
                            $selected = '';
                        }
                        echo '<option value="' . $country['name'] . '"' . $selected . '>' . $country['name'] . '</option>';
                    }
                    ?>
                </select>
            </td>
            <td>
                <!--class="selectpicker"-->
                <select form="update_form" name="luogo_arrivo" class=" form-control" >
                    <?php
                    $arrival = mysqli_query($oConn, "select luogo_arrivo from flight_details where id_student = " . $studentid . ";");
                    $loc_arrival = mysqli_fetch_array($arrival);
//                                         echo $loc_arrival[0];
                    $countries = array();
                    $countries = mysqli_query($oConn, "select id,name from countries");

                    foreach ($countries as $country) {
                        if ($loc_arrival[0] == $country['name']) {
                            $selected = 'selected';
                        } else {
                            $selected = '';
                        }
                        echo '<option value="' . $country['name'] . '"' . $selected . '>' . $country['name'] . '</option>';
                    }
                    ?>
                </select>
            </td>
            <td style="align-content: center;">
                <form id="update_form" action="update-flight-details.php" method="post">
                    <?php echo $studentid ?>
                    <input type="hidden" value="<?php echo $studentid; ?>" name="studentid">
                    <button type="submit" name="update" class="btn-success btn btn-sm">Save</button>
                </form>
            </td>
        </tr>
    <?php }
} ?>

第二页代码:

<?php

if (isset($_POST["update"])) {
    $url = $_POST['url'];
    echo $_POST['studentid'];
    echo $_POST['data_partenza'];
    echo $_POST['luogo_arrivo'];

    if (!isset($_POST['data_partenza'])) {
        $_POST['data_partenza'] = null;
    }
    if (!isset($_POST['data_arrivo'])) {
        $_POST['data_arrivo'] = null;
    }
    if (!isset($_POST['ora_partenza'])) {
        $_POST['ora_partenza'] = null;
    }
    if (!isset($_POST['ora_arrivo'])) {
        $_POST['ora_arrivo'] = null;
    }
    if (!isset($_POST['luogo_partenza'])) {
        $_POST['luogo_partenza'] = null;
    }
    if (!isset($_POST['luogo_arrivo'])) {
        $_POST['luogo_arrivo'] = null;
    }


    mysqli_query($oConn, "
            UPDATE flight_details SET 
                    data_partenza = '" . $_POST['data_partenza'] . "',
                    data_arrivo = '" . $_POST['data_arrivo'] . "',
                    ora_partenza = '" . $_POST['ora_partenza'] . "',
                    ora_arrivo = '" . $_POST['ora_arrivo'] . "',
                    luogo_partenza = '" . $_POST['luogo_partenza'] . "',
                    luogo_arrivo = '" . $_POST['luogo_arrivo'] . "'
                    where id_student = " . $_POST['studentid'] . ";
        ");
}?>

我整天迷失了,请帮助:')

编辑:我也创建了代码来一起更新所有学生,我写的就像我上面写的那个,并且那个有效。这里是:

第1页:

<tr>
    <td></td>
    <td>All students</td>

    <td>
        <input form="update_all_form" type="date" name="data_partenza_all" class="form-control" id="name"   value="">
        <input form="update_all_form" type="hidden" value="<?php echo $url; ?>" name="urlall">
    </td>
    <td>
        <input form="update_all_form"type="date" name="data_arrivo_all" class="form-control" id="name"   value="">
    </td>
    <td>
        <input form="update_all_form" type="time" name="ora_partenza_all" class="form-control" id="name"   value="">
    </td>
    <td>
        <input form="update_all_form" type="time" name="ora_arrivo_all" class="form-control" id="name"  value="">
    </td>
    <td>
        <select form="update_all_form" name="luogo_partenza_all" class=" form-control">
            <?php
            $countries = array();
            $countries = mysqli_query($oConn, "select id,name from countries");
            foreach ($countries as $country) {
            echo '<option value="'.$country['name'].'">'.$country['name'].'</option>';
            }
            ?>
        </select>
    </td>
    <td>
        <select form="update_all_form" name="luogo_arrivo_all" class=" form-control">
            <?php
            $countries = array();
            $countries = mysqli_query($oConn, "select id,name from countries");
            foreach ($countries as $country) {
            echo '<option value="'.$country['name'].'">'.$country['name'].'</option>';
            }
            ?>
        </select>
    </td>

    <td style="align-content: center;">
        <form id="update_all_form" action="update-flight-details.php" method="post">
            <button type="submit" name="update_all" class="btn-success btn btn-sm">Save</button>
        </form>
    </td>

</tr>
</thead>
<tbody>

第2页:

else if (isset($_POST["update_all"])){
    $urlall=$_POST['urlall'];
    mysqli_query($oConn, 
        "
            UPDATE flight_details SET 
                    data_partenza = '".$_POST['data_partenza_all']."',
                    data_arrivo = '".$_POST['data_arrivo_all']."',
                    ora_partenza = '".$_POST['ora_partenza_all']."',
                    ora_arrivo = '".$_POST['ora_arrivo_all']."',
                    luogo_partenza = '".$_POST['luogo_partenza_all']."',
                    luogo_arrivo = '".$_POST['luogo_arrivo_all']."';
        ");
}

以上是关于PHP - 提交日期/时间/从foreach循环中选择输入值到$ _POST不返回任何内容的主要内容,如果未能解决你的问题,请参考以下文章

删除Foreach循环中的重复项

如何从foreach循环php中的对象数组中删除对象

从 PHP foreach 循环中获取 Ajax 变量(第二部分)

如何在 PHP/Eclipse 中对 foreach 循环中从数组中拉出的自定义对象进行智能感知?

在 PHP CodeIgniter foreach 循环中从数据库中提取和更新数据

将数据从 javascript forEach 循环中的 php 数组发送到 ajax 调用的 url