隐藏第一个表单然后显示第二个表单。 Secnd表单执行多个提交

时间:2016-07-22 09:52:53

标签: javascript php jquery html forms

我有两个单输入字段的表单。当页面加载时我希望显示第一个表单,并且在提交第一个表单时,我想保留在php中的会话中输入的值以供以后使用。继续提交第一个表单我想用jquery取消隐藏第二个表单。第二种形式执行多个提交。我面临的问题是提交的第二个表单带回了第一个表单,我在提交这两个表单时遇到了麻烦。我希望第二种形式保持一致,并希望最初只打开第一种形式。

以下是我正在使用的代码:

JS

<script>
    $(document).ready(function() {
        $("#submitvendor").click(function() {

            $("#vendorform").hide(1000);

            $("#imeiform").css("display", "block");


        })
    });
</script>

HTML

<div id="vendorform" class="container" style = "  position: relative; top: 50px ;left: 20px ; width: 500px; border: 7px solid #c68c53; border-bottom-left-radius: 30px; border-top-right-radius: 30px;  padding: 25px;">
  <form id="vendor" class="form-inline" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" >
    <div class="form-group">
      <label >Vendor Reference:&nbsp&nbsp&nbsp&nbsp&nbsp</label>
      <input type="text" class="form-control" id="vendorreference" name="vendorreference" placeholder="Enter Vendor Reference here" required>&nbsp&nbsp&nbsp
    </div>
    <input type="submit" id="submitvendor" name="Submit" value="Submit">
  </form>
</div>
<div id="imeiform" class="container" style = " display: none;  position: relative; top: 70px ;left: 20px ; width: 500px; border: 7px solid #c68c53; border-bottom-left-radius: 30px; border-top-right-radius: 30px;  padding: 25px;">
  <form  class="form-inline" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" >
    <div class="form-group">
      <label >&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspIMEI:&nbsp&nbsp&nbsp&nbsp&nbsp</label>
      <input type="text" class="form-control" id="imei" name="imei" placeholder="Enter IMEI here" required >&nbsp&nbsp&nbsp
    </div>
    <input type="submit" id="imei" class="btn btn-default" name="Submit" value="SUBMIT">
  </form>
</div>

PHP

<?php
session_start();
include_once('config.php');
if (isset($_POST['Submit'])) {    
    if (!isset($_SESSION['vendor'])) {
        $_SESSION['vendor'] = $_POST['vendorreference'];
    }    
    echo "Submitted";
}

if (isset($_POST['SUBMIT'])) {
    $imei = $_POST['imei'];
    echo "Second form submitted";
}
?>

2 个答案:

答案 0 :(得分:1)

您只需使用PHP即可实现此目的,

<强> HTML

<div id="vendorform" class="container" style = "display:<?php echo ($top_form == "Y" ) ? "block": "none"; ?>; position: relative; top: 50px ;left: 20px ; width: 500px; border: 7px solid #c68c53; border-bottom-left-radius: 30px; border-top-right-radius: 30px;  padding: 25px;">
    <form id="vendor" class="form-inline" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" >
        <div class="form-group">
            <label >Vendor Reference:&nbsp&nbsp&nbsp&nbsp&nbsp</label>
            <input type="text" class="form-control" id="vendorreference" name="vendorreference" placeholder="Enter Vendor Reference here" required>&nbsp&nbsp&nbsp
        </div>

        <input type="submit" id="submitvendor" name="first_form" value="Submit">
    </form>
</div>


<div id="imeiform" class="container" style = "display:<?php echo ($bottom_form == "Y" ) ? "block": "none"; ?>;  position: relative; top: 70px ;left: 20px ; width: 500px; border: 7px solid #c68c53; border-bottom-left-radius: 30px; border-top-right-radius: 30px;  padding: 25px;">
    <form  class="form-inline" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" >
        <div class="form-group">
            <label >&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspIMEI:&nbsp&nbsp&nbsp&nbsp&nbsp</label>
            <input type="text" class="form-control" id="imei" name="imei" placeholder="Enter IMEI here" required >&nbsp&nbsp&nbsp                
        </div>

        <input type="submit" id="imei" class="btn btn-default" name="second_form" value="SUBMIT">
    </form>
</div>

<强> PHP

<?php
session_start();
include_once('config.php');
$top_form = "Y";
$bottom_form = "N";

if (isset($_POST['first_form'])) {    

    if (!isset($_SESSION['vendor'])) {
        $_SESSION['vendor'] = $_POST['vendorreference'];
    }    

    $top_form = "N";
    $bottom_form = "Y";
}

if (isset($_POST['second_form'])) {

    $imei = $_POST['imei'];
    $top_form = "N";
    $bottom_form = "Y";
    echo "Second form submitted";
}
?>

我在顶部设置了两个控制样式行为的标志,让我知道它是否适合你。

答案 1 :(得分:0)

隐藏第一个表单的最简单方法是在提交后将其css设置为display:none

将PHP逻辑放在顶部。在将任何HTML发送到浏览器之前,session_start()尤其应该在开头附近。由于PHP可以告诉表单何时提交,您可以使用它来设置相应的样式。不需要Javascript

<?php
session_start();
...
$vendor_style = isset($_POST['Submit'])? 'display:none;': 'display:block;';
$imei_style = isset($_POST['Submit'])? 'display:block;': 'display:none;';
...
?>

然后在HTML页面中,您可以将显示设置为无或阻止,具体取决于是否已提交第一个表单

<form id="vendor" style="<?= $vendor_style ?>" class="form-inline"...
...
<form id="imei" style="<?= $imei_style ?>" class="form-inline"...

显然你必须在PHP中进行一些表单验证,但是你明白了。