繁体   English   中英

如何在提交时返回第二份注册表?

[英]How to return second register form on submition?

我有一个页面,其中包含个人和企业类型的两个注册表单,个人类型的表单设置为默认值,另一个表单是隐藏的,它工作正常。 但是当我将它切换到第二个表单并单击提交按钮时,它会提交第二个表单,但在提交后返回第一个表单,即使出现错误也会返回第一个表单。

我希望它在错误和提交后保持在第二种形式。

这是我的 php :

if (isset($_POST["btnRegister"])) {
    echo "Done";
}elseif (isset($_POST["btnbusiness"])) {
    echo "Done";
}

我的页面中的 HTML 和 js 代码:

 function swapConfig(x) { var radioName = document.getElementsByName(x.name); for(i = 0 ; i < radioName.length; i++){ document.getElementById(radioName[i].id.concat("Settings")).style.display="none"; } document.getElementById(x.id.concat("Settings")).style.display="initial"; }
 <div class="col-10 clmiddle"> <label for="production"><b>Individual</b></label> <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="production" checked="checked" /> <label for="development"><b>&nbsp;&nbsp;Business</b></label> <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="development" /> </div> First Form : <div id="productionSettings" class="col-12"> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="col-6"> <input type="text" class="form-control" name="fname" placeholder="Name..." required> <button type="submit" name="btnRegister" class="btn btn-primary right">Send</button> </div> </form> </div> Second Form : <div id="developmentSettings" style="display:none" class="col-12"> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="col-6"> <input type="text" class="form-control" name="fname" placeholder="Name..." required> <button type="submit" name="btnbusiness" class="btn btn-primary right">Send</button> </div> </form> </div>

编辑:我将 JS 更改为 php,这是解决方案。 PHP 代码(获取 url):

$path = $_SERVER['REQUEST_URI'];
    $Aurl = explode(",",$path);
    for ($i=0; $i<count($Aurl);$i++){
      $Burl = str_replace("?", "/", trim($Aurl[$i]));
    }
$url = htmlspecialchars(basename($Burl));
$FormPostUrl = basename($path);

html部分:

 Checkbox : <div class="col-10 clmiddle" style="margin-top: 20px;"> <label for="production"><b>Individual</b></label> <input type="checkbox" value="<?php echo htmlspecialchars("register.php"); ?>" name="checket" onClick="if (this.checked) { window.location = this.value; }" <?php if($url === htmlspecialchars("register.php")){ echo 'checked="checked"';}?>> <label for="development"><b>Business</b></label> <input type="checkbox" value="<?php echo htmlspecialchars("register.php?business");?>" name="checket" onClick="if (this.checked) { window.location = this.value; }" <?php if($url === htmlspecialchars("business")){ echo 'checked="checked"';}?>> </div> First Form : <?php if($url === htmlspecialchars("register.php")){?> <div id="productionSettings" class="col-12"> <form action="<?php echo htmlspecialchars($FormPostUrl); ?>" method="post"> <div class="col-6"> <input type="text" class="form-control" name="fname" placeholder="Name..." required> <button type="submit" name="btnRegister" class="btn btn-primary right">Send</button> </div> </form> </div> Second form: <?php } elseif($url === htmlspecialchars("business")){ ?> <div id="developmentSettings" class="col-12"> <form action="<?php echo htmlspecialchars($FormPostUrl); ?>" method="post"> <div class="col-6"> <input type="text" class="form-control" name="fname" placeholder="Name..." required> <button type="submit" name="btnbusiness" class="btn btn-primary right">Send</button> </div> </form> </div> <?php } ?>

您可以使用 PHP 来控制display:none是否添加到您的 div 中。 使用if语句(或上下文中的三元运算符可能是更简洁的语法)来决定要回显什么。 由于默认是显示生产设置表单,我们只需要检查其他表单是否已提交,即可知道是否要更改。

例如这样的事情(未经测试):

<input type="radio" onchange="swapConfig(this)" name="urlOptions" id="production" <?php echo (isset($_POST["btnRegister"]) ? "" : 'checked="checked"'); ?> />

<input type="radio" onchange="swapConfig(this)" name="urlOptions" id="development" <?php echo (isset($_POST["btnRegister"]) ? 'checked="checked"' : ""); ?> />

<div id="productionSettings" class="col-12" <?php echo (isset($_POST["btnbusiness"]) ? "style='display:none'" : ""); ?>>

<div id="developmentSettings" class="col-12" <?php echo (isset($_POST["btnbusiness"]) ? "" : "style='display:none'"); ?>>

PS 除非您为了示例的目的对这些形式进行了大量简化,否则它们看起来基本相同。 您是否真的需要两个单独的表格是值得怀疑的。 唯一的区别似乎是“个人”和“企业”之间的选择 - 可以通过带有单选按钮的单个表单处理来选择类型,这也将简化您处理回发的方式,并减少数量重复的代码和 HTML。 当然,如果您实际上为这些表单捕获了比您显示的更多的不同字段,那么这些评论并不真正适用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM