$ _POST数组未在表单提交中填充

时间:2013-11-19 11:04:04

标签: php forms session jquery-mobile post

我正在尝试创建一个跨越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而有所不同。

2 个答案:

答案 0 :(得分:0)

如果需要$ _POST

中的数据,请尝试使用此功能
foreach ($_POST as $post_key => $post_value) 
{
  ${$post_key} = $post_value;
}

答案 1 :(得分:0)

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

如果你添加

    data-ajax="false"

到你的开始表单标签,这可以防止Jquery mobile执行任何ajax方法。我唯一不确定的是为什么print_r($ _ POST)返回空,但whwn回显每个$ _POST项我可以获得所需的值。