PHP - 多种形式,在提交时从两者获取值?

时间:2016-11-29 09:28:21

标签: php html forms post

我现在有点陷入困境,可以请一些帮助。

我有一个日历,多个部门正在使用它来输入他们的日程安排。每个用户被要求完成1x选择和2x输入字段,分为FORM 1和FORM 2

表格1:

  • 部门代码(onclick提交事件)

表格2:

  • 名称
  • 日期
  • 提交按钮

用户选择了他/她的部门代码后,日历将刷新并仅获取该特定部门的条目(这意味着它将过滤部门代码的所有记录)。之后,用户必须输入用户名并选择日期,然后按提交按钮(表单2)。

现在,问题是为了使FORM 2正确提交,我需要知道表单1中的部门代码。此外,当按下提交按钮时,我遇到了阻止日历刷新(表单1)的麻烦表格2。

简而言之,在访问两个表单的数据时,如何清楚地区分表单1和表单2之间的($ _POST)?

PHP

<?php
// SHOULD BE EXECUTED AFTER THE SUBMIT BUTTON WAS CLICKED
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['type']) && ($_POST['type']=='new_entry')) {

    // include connection details
    include '../../plugins/MySQL/connect_db.php';

    // Open a new connection to the MySQL server
    $con = new mysqli($dbhost,$dbuser,$dbpass,$dbname);

    // Output any connection error
    if ($con->connect_error) {
        die('Error : ('. $con->connect_errno .') '. $con->connect_error);
    }

    // define variables
    $table = 'calendar';
    $type = mysqli_real_escape_string($con,$_POST['type']);
    $holidex = mysqli_real_escape_string($con,$_POST['holidex']);

    if($type == 'new_entry')
    {               
        // define variables and query
        $mod_property = mysqli_real_escape_string($con,$_POST['holidex']);
        $mod_name = mysqli_real_escape_string($con,$_POST['mod_name']);
        $mod_date = date('Y-m-d',strtotime($_POST['mod_date']));

        $sql = "INSERT INTO calendar (`title`, `startdate`, `enddate`, `allDay`, `color`, `holidex`) VALUES ('$mod_name','$mod_date','$mod_date','true','','$mod_property')";

        print($sql);

        $result = $con->query($sql) or die('<p>Could not submit new MOD record into database: ' . MYSQLI_ERROR() . '</p>');

        $result->free();
    }

    $con->close();
}
?>



        <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <!-- <form name="mod_form" action="../../plugins/MySQL/ajax_action.php?type=new_entry" method="POST"> -->

          <!-- Property -->
          <div class="col-md-4">
            <label>Property</label>
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-server"></i></span>
                <select name="holidex" id="holidex" class="form-control select2" style="width: 100%;" data-placeholder="Select your property" onchange="this.form.submit();" method="POST" <?php if($_SESSION['Access']=='User') { echo "disabled"; } ?>>

                    // get all my departments and their respective codes
                    <option value="1">Dept. 1</option>
                    <option value="2">Dept. 2</option>
                    <option value="3">Dept. 3</option>

                </select>
            </div>
            <!-- /btn-group -->
          </div>
          <!-- /.property -->

        </form>

        <form name="form2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
          <!-- MOD Name -->
          <div class="col-md-4">
            <label>MOD Name</label>
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                <select name="mod_name" id="mod_name" class="form-control select2" style="width: 100%;" data-placeholder="Select a name">
                    <option value=""></option>
                    <option value="1">User1</option>
                    <option value="2">User2</option>
                    <option value="3">User3</option>
                </select>
            </div>
            <!-- /.input-group -->
          </div>
          <!-- /.mod name -->

          <!-- MOD Date -->
          <div class="col-md-3">
            <label>MOD Date</label>
            <div class="input-group">
              <span class="input-group-addon"><i class="fa fa-server"></i></span>
              <input type="date" class="form-control" name="daterangepicker" id="daterangepicker" />
              <!-- <input type="date" id="mod_date" name="mod_date" class="form-control" style="width: 100%;"> -->
            </div>
          </div>
          <!-- /.mod date -->

            // hidden input field to determine the type and help differentiate the $_POST submissions
            <input type="hidden" class="form-control" name="type" id="type" value="new_entry"/>

          <!-- Submit button -->
          <div class="col-md-1 text-center">
            <label>&nbsp;</label>
            <button type="submit" name="btnSubmit" class="btn btn-primary btn-block" onclick="this.disabled=true; this.value = 'Wait...'; this.form.submit(); return true;">Submit</button>
          </div>
          <!-- /.submit button -->

        </form>
        <!-- /.form 2 -->

1 个答案:

答案 0 :(得分:1)

按照SiteThief的建议,我完全跳过<form>方法,并通过AJAX将结果提交到我的php处理程序文件。像没有讨厌的表格提交问题的魅力。

JS

<script>
    // SAVE NEW DEPARTMENT RECORD - SAVE BUTTON
    $("#SubmitButton").click(function() {

        // check that input fields are not empty
        if($("#holidex").val()!="" && $("#mod_name").val()!="" && $("#mod_date").val()!="") {

            $.ajax({
                url: "../../plugins/MySQL/ajax_action.php",
                type: "POST",
                async: true, 
                data: { action:"mod_calendar",type:"new_entry",holidex:$("#holidex").val(),mod_name:$("#mod_name").val(),mod_date:$("#mod_date").val()},
                dataType: "html",           

                success: function(data) {
                    $('#mod_output').html(data); 
                    drawVisualization();
                },  
            });
        } else {
            //notify the user they need to enter data
            alert("Please choose a hotel, staff name and MOD date from the list.");
            return;
        }

        // close modal and refresh page
        setTimeout(function(){location.reload()}, 1000);
        return;
    });
</script>