【问题标题】:$_POST Array not being populated on form submission表单提交时未填充 $_POST 数组
【发布时间】:2013-11-19 11:04:04
【问题描述】:

我正在尝试创建一个跨越 2 页的简单表单。我正在使用 PHP 的 $_SESSION 来存储从第一页发送到第二页的值。该表单最初位于一个页面上,并且运行良好。

这是第一页

<form action="jobsheet2.php" method="POST" class="jobform">

  <!--START ADD CLIENT-->
        <div class="client-type-wrapper full-width-centred">    
                            <a href="#" id="new_customer_btn" class="btn med-btn inline-row">New Customer</a>

                            <a href="#" id="existing_job_btn" class="btn med-btn inline-row">Existing Customer</a>
                        </div>
                    <!--END ADD CLIENT-->

                    <!--START ADD CLIENT-->
                        <p class="new_customer_field">
                            <label for="new_customer">Add New Customer</label>
                            <input type="text" name="new_customer" class="new_customer_input" id="new_customer">
                        </p>
                    <!--END ADD CLIENT-->

                    <!--START CHOOSE CLIENT-->
                    <?php if( $clients ): ?> 
                        <p class="select-container existing-customer">
                            <label for="chosen_client" class="select">Choose a Client:</label>
                            <select name="chosen_client" id="chosen_client">
                                <?php
                                    foreach ($clients as $client ) {
                                        echo "<option value={$client['client_name']}>{$client['client_name']}</option>";
                                    }
                                ?>
                            </select>
                        </p>

                    <?php else: ?>
                            echo "No rows returned";

                    <?php endif; ?>
                    <!--END CHOOSE CLIENT-->

                    <!--START CUSTOMER TYPE-->
                    <p class="select-container">
                        <label for="select-choice-1" class="select">Customer Type</label>

                        <select name="customer_type" id="customer_type">
                           <option value="Contract">Contract</option>
                           <option value="Billable">Billable</option>
                        </select>

                    </p>

                    <!--END CUSTOMER TYPE-->


                    <!--START CHOOSE DATE-->
                        <p class="date_picker">
                            <label for="date_input" class="select">Date:</label>

                            <div class="input_button">
                            <input type="date" name="date_input" id="date_input" class="date_select" data-role='datebox' data-icon="home" data-options='{"mode":"flipbox", "lockInput":true, "useFocus":true, "useNewStyle":true}'/>
                            <div>
                        </p>
                    <!--END CHOOSE DATE-->

                    <!--START CHOOSE TIME ON-->
                        <p>
                            <label for="select-choice-1" class="select">Time On:</label>
                            <input type="text" id="time_select_on" name="time_select_on" data-role="datebox" data-options='{"mode":"timeflipbox", "lockInput":true, "useFocus":true, "useNewStyle":true}'/>
                        </p>
                    <!--END CHOOSE TIME ON-->

                    <!--START CHOOSE TIME OFF-->
                        <p>
                            <label for="select-choice-1" class="select">Time Off:</label>
                            <input type="text" id="time_select_off" name="time_select_off" data-role="datebox" data-options='{"mode":"timeflipbox", "lockInput":true, "useFocus":true, "useNewStyle":true}'/>
                        </p>
                    <!--END CHOOSE TIME OFF-->

                    <!--START Job Details-->
                        <p>
                            <label for="textarea">Details of Work:</label>
                            <textarea  name="work_details" id="work_details"></textarea>
                        </p>
                    <!--END Job Details-->

                    <!--START CHOOSE Technician-->
                        <?php if( $technicians ): ?> 
                             <p class="select-container">
                                <label for="select-choice-1" class="select">Choose a Technician:</label>
                                <select name="chosen_tech" id="chosen_tech" data-theme="b">
                                    <?php
                                        foreach ($technicians as $technician ) {
                                            echo "<option value={$technician['user']}>{$technician['user']}</option>";
                                        }
                                    ?>
                                </select>
                            </p>


                        <?php else: ?>
                                echo "No rows returned";

                        <?php endif; ?>
                    <!--END CHOOSE Technician-->


                    <!--START PRINT NAME-->
                        <p class="print_name_field">
                            <label for="print_name">Print Name</label>
                            <input type="text" name="print_name" class="print_name_input" id="print_name">
                        </p>
                    <!--END PRINT NAME-->


                    <button id="submitJob" type="submit" class="btn save-job-btn">Next</button>

                </form>
        </div>

这是第二页

   <?php
   session_start();

if ( !isset($_SESSION['username']) ) {
    header('Location: index.php');
    die();
}

$_SESSION['chosen_client'] = $_POST['chosen_client'];
$_SESSION['customer_type'] = $_POST['customer_type'];
$_SESSION['date_input'] = $_POST['date_input'];
$_SESSION['time_select_on'] = $_POST['time_select_on'];
$_SESSION['time_select_off'] = $_POST['time_select_off'];
$_SESSION['work_details'] = $_POST['work_details'];
$_SESSION['chosen_tech'] = $_POST['chosen_tech'];
$_SESSION['print_name'] = $_POST['print_name'];

require 'config.php';

require 'functions.php';

$conn = connect($config);

if ( $conn ) {
    $clients = get('client', 'client_name', $conn);
    $technicians = get('technician', 'user', $conn );
}
else die('Could not connect to the database');

?>

<head>
    <meta charset="UTF-8">
    <title>jQuery Boilerplate</title>

    <link rel="stylesheet" href="css/styles.css">
    <link rel="stylesheet" href="css/jquery.mobile-1.3.2.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="js/jquery.mobile-1.3.2.js"></script>

    <link rel="stylesheet" href="css/jqm-datebox.min.css">
    <script src="js/jqm-datebox.core.min.js"></script>
    <script src="js/jqm-datebox.mode.datebox.min.js"></script>
    <script src="js/jqm-datebox.mode.flipbox.js"></script>

    <script src="js/jSignature.min.js"></script>
    <script src="js/jquery.validate.min.js"></script>

    <script src="js/customjs.js"></script>
</head>

<body>
    <div data-role="page" id="jobsheet2">

        <div data-role="header">
            <a href="#" data-icon="back" data-rel="back" title="Go back">Back</a>
            <h1>Sign Here</h1>
        </div>

        <div data-role="content" id="job2-main-content">

            <form action="send.php" method="POST" class="jobform">

                <!--START OF SIGNATURE-->
                <div id="signature_wrapper">
                    <button id="clearSig">Clear</button>
                    <div id="signature"></div>

                    <input name="sig_data" id="sig_data" class="signature_data" type="hidden">
                </div>
                <!--END OF SIGNATURE-->

                <button id="submitJob" type="submit" class="btn save-job-btn">Save Job</button>

            </form>

        <?php
            echo $_POST['chosen_client'];
            echo $_POST['customer_type'];
            echo $_POST['date_input'];
            echo $_POST['time_select_on'];
            echo $_POST['time_select_off'];
            echo $_POST['work_details'];
            echo $_POST['chosen_tech'];
            echo $_POST['print_name'];
            print_r($_POST);
            print_r($_SESSION);
        ?>
        </div>

我注意到一些非常奇怪的事情。当我回显每个单独的 $_POST 项目时,它会打印到第二页,但是当我 print_r($_POST) 时,当我在 chrome 中查看页面的源代码时,它会显示为一个空数组。

注意:我在 jQuery Mobile 应用程序中使用此表单我不确定是否会因为它使用 ajax 而有所不同。

【问题讨论】:

    标签: php forms session jquery-mobile post


    【解决方案1】:

    当您想要 $_POST 中的数据时尝试使用它

    foreach ($_POST as $post_key => $post_value) 
    {
      ${$post_key} = $post_value;
    }
    

    【讨论】:

      【解决方案2】:

      我认为问题在于 jQuery 及其在将表单发布到其他页面时使用 ajax。

      如果你添加

          data-ajax="false"
      

      到您的打开表单标记,这会阻止 Jquery mobile 执行任何 ajax 方法。我唯一不确定的是为什么 print_r($_POST) 返回空但回显每个单独的 $_POST 项目我可以获得所需的值。

      【讨论】:

        猜你喜欢
        • 2018-10-08
        • 2016-08-16
        • 1970-01-01
        • 2010-11-19
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-15
        相关资源
        最近更新 更多