我在同一个php页面中有两个表单。我必须在第一个表单中保存值,之后保存在数据库中,我可以在下一个表单中使用这些值。
第一种形式的PHP代码:
<?php
include 'config.php';
if(isset($_POST['documentRequest']))
{
$date = $_POST['reqDate'];
$name = $_POST['name'];
$insertSql = mysqli_query($conn, "INSERT INTO documentrequest(Person_idPerson, docrequest_date) VALUES ('$name', '$date');");
//$sql = "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument) VALUE ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = 6), 2)";
header("Refresh: 2; url=doc_request.php");
mysqli_close($conn);
}
?>
这是我的第一张表格:
<form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate>
<div class = "first">
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">ID <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" placeholder ="Type in ID number " name="name" required="required" type="number">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button id="requestDiv" type="submit" class="btn btn-success" name="documentRequest" >Proceed</button>
<button type="submit" class="btn btn-primary">Cancel</button>
</div>
</div>
</div>
</form>
第二种形式(这是在另一个div内):
<form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate>
<div class = "second">
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12"> Document Request(s) <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class = "clearfix"></div>
<p>Minimum of 1:<p>
<input type="checkbox" name="docs[]" id="doc1" value="Certificate of Residency" /> Certificate of Residency
<br/>
<div class = "col-xs-3">
<input name="d1" class="form-control col-md-7 col-xs-12" required="required" type="number">
</div>
<div class = "clearfix"></div>
<input type="checkbox" name="docs[]" id="doc2" value="Barangay Clearance" /> Barangay Clearance
<br />
<div class = "col-xs-3">
<input name="d2" class="form-control col-md-7 col-xs-12" required="required" type="number">
</div>
<input id="a" class="form-control col-md-7 col-xs-12" name="a" value = "<?php echo isset($_POST['name'])? $_POST['name'] : "";?>" required="required" type="text">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button id="requestDiv" type="submit" class="btn btn-success" name="documentRequest" >Proceed</button>
<button type="submit" class="btn btn-primary">Cancel</button>
</div>
</div>
</form>
基本上,我需要在下一个表单中使用ID的值,以便它知道它属于哪个ID 。下一个表单基本上是关于用户的请求(一系列复选框)。我怎样才能做到这一点?请帮我。谢谢。
答案 0 :(得分:1)
我很确定当您致电header("Refresh: 2; url=doc_request.php");
时,您正在刷新页面并丢失提交的表单数据。
如果您从逻辑中移除它,这将有效:
在第二种形式中,您需要值,请执行以下操作:
<input id="name2" class="form-control col-md-7 col-xs-12" name="other-name" required="required" type="number" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '';?>">
如果发布的值存在,这将自动将发布的值添加到第二个表单的输入元素值
请注意,首先检查isset()
是必要的。只是回显该值会导致在值不存在时(在提交第一个表单之前)抛出错误
否则,您可以使用sessions
来达到此效果:
在php页面的最顶部添加:
session_start();
$_SESSION['name']=isset($_POST['name']) ? $_POST['name'] :'';
然后在你的第二个表格中这样做:
<input id="name2" class="form-control col-md-7 col-xs-12" name="other-name" required="required" type="number" value="<?php echo $_SESSION['name']">
答案 1 :(得分:-1)
如果您的表单操作返回到同一页面,您可以尝试:
<form id="form-two">
<input id="name" name="name" value="<?php echo $_POST['name']; ?>">
</form>