duanbei8904 2019-04-18 10:16
浏览 60

如何将HTML表单中的值解析为PHP值?

I want to update the order preparation time. I have this little HTML form, but I'cant pass it in a PHP variable right after this code. I've used Cookies, I've used POST method but none helped me about it.

<form>
            <input type="text" id="save_order_time" name="order_time<?php echo $order_id ?>" />
            <input type="submit" name="submit" value="Update Order Time" onclick="order_time_a();">
            </form>

<script>

            function order_time_a(){
                var input = $('.menu-order-detail').find('input[name="order_time<?php echo $order_id ?>"]').val();
                //var input = 25;
                    <?php 
                    //$order_time = '<script>var input = $(".menu-order-detail").find("input[name="order_time<?php echo $order_id "]").val();</script>';
                    //update_post_meta($order_id, 'foodbakery_order_time', $order_time); ?>

                console.log("Lets show something "+input);
                var d = new Date();
                d.setTime(d.getTime() + (1*24*60*60*1000));
                var expires = "expires="+ d.toGMTString();
                document.cookie = "order_time" + "=" + input + ";" + expires + ";path=/";
                console.log("Cookie was set. " + input);                    
            }

            </script>

            <?php

            echo ("<script>console.log('This code is executed first')</script>");
            //echo ('order_time'.$order_id);
            $order_time = $_COOKIE['order_time'];
            update_post_meta($order_id, 'foodbakery_order_time', $order_time);

When I use COOKIE solution the value that I've set in previous time is stored in the $order_time value. That's normal because I can't access COOKIE right after is set.

When I use POST method I get NULL in my database.

How can I implement the above?

  • 写回答

1条回答 默认 最新

  • dt2002 2019-04-18 11:40
    关注

    There are a few things unclear in your explanation. Why are you using these different concepts next to each other?

    • Form / POST
    • Javascript
    • COOKIE variable

    I think you should try to only use the Form / POST option, see explanation here: HTML Form POST to PHP page


    Besides that, in your javascript part I see this: var input = $('.menu-order-detail').find('input[name="order_time<?php echo $order_id ?>"]').val();

    You should create it like this to find the right object: var input = $('.menu-order-detail').find('input[name="order_time"]').val();

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)