将数组结果传递给html表单

时间:2019-01-28 20:58:50

标签: php mysqli html-form

我有一个mysqli查询的结果返回并显示在page1上。当用户单击“立即注册”链接时,它将在第2页上向他们发送HTML表单。我需要从page1传递到page2上的html表单的某些数据(例如:$ row [1])。我被困住了。任何建议表示赞赏。

----这是trip_1.php的更新代码:---

<?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>
...

我需要。$ row [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 个答案:

答案 0 :(得分:1)

您可以通过以下两种方法在网址的第2页的表单中附加查询字符串:

form-page2.php?name=name&trip=tripID

,只需使用GET ['name']&GET ['trip']抓取第二页上的内容即可。

另一种选择是将它们设置在$ _SESSION变量中并以这种方式检索它们。

任何一种方法都可以。

答案 1 :(得分:0)

另一种解决方案是使用隐藏的输入类型传递要保留在表单中的数据。 因此,它将使用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>


...
相关问题