dongzhent208577 2018-09-06 04:54
浏览 90

表单内部与POST数组交互

Okay my code is not broken, however it can be improved. I have a form element inside a table, which, according to this post here is illegal, but for me it is working to some extent. I am using PHP and the situation is as follows:

Inside that form I have two elements one is comment and another is order_date; when I inspect the form in chrome developer tools it shows the correct structure, so the point in the earlier linked post is not happening here. When the form gets submitted, I can never get the values of these fields from the post array unless I initialize them first, I am initializing them within a $_SESSION, what is weird is that when I initialize them using $_POST it also does not work. What is causing this error? Thanks

code for initializing them:

if(empty($_SESSION['comment'])) {
    $_SESSION['comment']='';
}
if(empty($_SESSION['order_date'])) {
    $_SESSION['order_date']='';
}

code for getting their value after form is submitted:

if(isset($_POST['comment']) && isset($_POST['order_date'])) {
    $_SESSION['comment'] = nl2br(filter_var($_POST['comment'],FILTER_SANITIZE_STRING));
    $_SESSION['order_date'] = mysqli_real_escape_string($con,$_POST['order_date']);
}

Trying to get their value in:

if(isset($_POST["place_order"])) {
    $comment = $_POST['comment'];
}

The above code doesn't work

This is the table row of the form:

<tr>  
    <td colspan="5" align="center">  
        <form method="POST" action="cart.php" class="form-group">
            <label for="comment">Order comments </label>
            <textarea name="comment" id="comment" class="form-control" 
                      placeholder="Please enter any special instructions for the order" required> 
                          <?php if (isset($_SESSION['comment'])) {
                              echo $_SESSION['comment'];
                          } ?> 
            </textarea> <br>
            <label  for="order_date">Delivery date</label>
            <input type="date" class="form-control" style="max-width: 20%;" 
                   name="order_date" id="date" required></input> <br />
            <input type="submit" name="place_order" id="place_order" 
                   class="btn logo-green" value="Place Order" />  
        </form> 
    </td> 
</tr>

Thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 易盾点选的cb参数怎么解啊
    • ¥15 MATLAB运行显示错误,如何解决?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?
    • ¥15 电磁场的matlab仿真
    • ¥15 mars2d在vue3中的引入问题
    • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面