0

I have results from a mysqli query returned and displayed on page1. When the user clicks the "register now" link it sends them an HTML form on page2. I need to have certain data (ex: $row[1]) from page1 pass to the html form on page2. I am stuck. Any suggestions are appreciated.

----Here is the UPDATED CODE for 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>
...

I NEED .$row[1]. TO PASS TO A FORM FIELD (name=tripID) ON PAGE2

---Here is the UPDATED CODE for 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>


...
5
  • 2
    How do you access page 2? With an link? If yes.... you can pull your $row[1] to your second page with an $_GET - parameter or save it in an SESSION. Commented Jan 28, 2019 at 21:02
  • 1
    How is the user getting from page1 to page2? Are they submitting a form? If so, add a hidden input that submits that value as part of the form Commented Jan 28, 2019 at 21:02
  • 1
    A session variable is probably best. If you pass it as a URL parameter or hidden input, the user can change it. Commented Jan 28, 2019 at 21:04
  • Yes. sorry, the user clicks at the bottom of page to "register for the trip". I am not very familiar with $GET or using SESSION. Is that done on page1? Commented Jan 28, 2019 at 21:09
  • I would not use the session to pass or save variables as it is not very reliable. If the data is not sensitive and you have protections against malicious data then using $_GET (aka query string) would be the easiest Commented Jan 28, 2019 at 23:56

2 Answers 2

1

A couple of ways you could do this would be attaching a query string in the URL for page 2 of the form:

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

and just grab those on the second page using GET['name'] & GET['trip'].

Another option would be to set them inside a $_SESSION variable and retrieve them that way.

Either way would work.

Sign up to request clarification or add additional context in comments.

Comments

0

Another solution would be to pass the data you want to keep in the form using input type hidden. It will therefore use POST rather than GET, which is in general better as it doesn't pollute the URL and have a larger size restriction.

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

Then for your second page :

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


...

1 Comment

Yes Sir. That works. Thanks for your help. I was stumped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.