Php 将数组结果传递给html表单
我在第1页上返回并显示了mysqli查询的结果。当用户单击register now链接时,它会在第2页向他们发送一个HTML表单。我需要有一些数据,例如:$row[1]从第1页传递到第2页的html表单。我被卡住了。如有任何建议,我们将不胜感激 --以下是trip_1.php的更新代码:--Php 将数组结果传递给html表单,php,mysqli,forms,Php,Mysqli,Forms,我在第1页上返回并显示了mysqli查询的结果。当用户单击register now链接时,它会在第2页向他们发送一个HTML表单。我需要有一些数据,例如:$row[1]从第1页传递到第2页的html表单。我被卡住了。如有任何建议,我们将不胜感激 --以下是trip_1.php的更新代码:-- <?php include('includes/connection.php'); if(isset($_POST['clicked'])) { ## you can use GET Metho
<?php
include('includes/connection.php');
if(isset($_POST['clicked'])) {
## you can use GET Method or SESSIONS
## with get
header("Location : reserve-form.php?ID=".$row[1]."");
## or
}
$sql="SELECT * FROM trips WHERE id='1' AND active='1'";
$results=mysqli_query($connection, $sql);
if (mysqli_num_rows($results) < 1) {
echo "<br>We are working on this trip's details.<br><br><h3
style='color:#C8FE2E'>Check back soon!</h3>";
} else {
while($row=mysqli_fetch_array($results)) {
echo"Trip ID: ".$row[1].
"<br>Date: ".$row[3].
"<br>Departs From: ".$row[4].
"<br>Departure Time: ".$row[5]
;
echo "";
}
}
mysqli_close($connection);
?>
<form action="reserve-form.php" method="POST">
<button type="submit" name="clicked">RegisterNow</button>
</form>
...
我需要。$行[1]。在第2页上传递到表单字段名=tripID
--以下是reserve-form.php的更新代码:--
<div class='container' id='new-form'>
<div class='row'>
<div class='col-lg-12'>
<h2>Reserve your Seat</h2>
<hr/>
<form id="reservation_form" action="/insert-form.php" method="post" >
<div class="form-group">
<label>Trip ID#</label>
<input class="form-control" type="text" name="tripID" value="<?php echo
$_GET['ID']; ?>" disabled>
</div>
<div class="form-group">
<label>First Name</label>
<input class="form-control" type="text" name="firstName" required >
</div>
...
有两种方法可以做到这一点,即在表单第2页的URL中附加一个查询字符串:
form-page2.php?name=name&trip=tripID
只需使用GET['name']&GET['trip']在第二页抓取它们
另一个选项是在$\u会话变量中设置它们,并以这种方式检索它们
任何一种方法都可以。另一种解决方案是使用input-type-hidden将要保留的数据传递到表单中。 因此,它将使用POST而不是GET,这通常更好,因为它不会污染URL,并且具有更大的大小限制
<?php
include('includes/connection.php');
$sql="SELECT * FROM trips WHERE id='1' AND active='1'";
$results=mysqli_query($connection, $sql);
if (mysqli_num_rows($results) < 1) {
echo "<br>We are working on this trip's details.<br><br><h3
style='color:#C8FE2E'>Check back soon!</h3>";
} else {
# put trip ID in a temporary variable
$tripID = 0;
while($row=mysqli_fetch_array($results)) {
echo"Trip ID: ".$row[1].
"<br>Date: ".$row[3].
"<br>Departs From: ".$row[4].
"<br>Departure Time: ".$row[5]
;
$tripID = $row[1];
echo "";
}
}
mysqli_close($connection);
?>
<form action="reserve-form.php" method="POST">
# use tripID and pass it as an attribute of your form
<input type="hidden" name="tripID" value="<?php echo $tripID; ?>"/>
<button type="submit" name="clicked">RegisterNow</button>
</form>
第二页:
<div class='container' id='new-form'>
<div class='row'>
<div class='col-lg-12'>
<h2>Reserve your Seat</h2>
<hr/>
<form id="reservation_form" action="/insert-form.php" method="post" >
<div class="form-group">
<label>Trip ID#</label>
<input class="form-control" type="text" name="tripID" value="<?php echo
$_POST['tripID']; ?>" disabled>
</div>
<div class="form-group">
<label>First Name</label>
<input class="form-control" type="text" name="firstName" required >
</div>
...
如何访问第2页?有链接吗?如果是。。。。您可以使用$\u GET-参数将$行[1]拉到第二页,或将其保存在会话中。用户如何从第1页转到第2页?他们在提交表格吗?如果是这样,添加一个隐藏输入,将该值作为formA会话变量的一部分提交可能是最好的。如果将其作为URL参数或隐藏输入传递,则用户可以对其进行更改。是。抱歉,用户单击页面底部以注册旅行。我不太熟悉$GET或使用会话。这是在第1页完成的吗?我不会使用会话来传递或保存变量,因为它不太可靠。如果数据不敏感,并且您有针对恶意数据的保护措施,那么使用$\u GET aka查询字符串将是最简单的方法。这很有效。谢谢你的帮助。我被难住了。