如何将值从一个页面传递到另一个页面,然后再传递到第三页

时间:2018-04-08 21:16:10

标签: php mysql forms

我正在编写一个代码,其中一个php页面将值发送到第二页,而第二页用户再次添加一些值并将它们重定向到另一个将执行MySQL查询的页面。我使用require函数来包含第二个文件,但是当我尝试处理第三个页面时,它给了我未定义的索引错误。

我的代码如下:第一个名为display.php的文件

<html lang=en>
<head>
    <title> Booking</title>
</head>
<body>
    <div class="row">
        <div class="col-md-3">
        </div>
        <div class="col-md-6" align="center">
            <div class="model-content" align= "center">
                <form  method="POST" action="payment.php">
                      <input type="date" name="booking_start" required>
                      <input type="date" name="booking_end" required>
                      <input type="time" name="booking_start_time" required>
                      <input type="time" name="booking_end_time" required>


    <?php
        //we create a table
        echo "<table background='img/parkingimg.png'>";
        // create table th 
        echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
        $sql=" select ParkingSlotNo,Status from fleming_dwing  ";
        $st=$conn->prepare($sql);
        $st->execute();
        $total=$st->rowCount();//get the number of rows returned
        if($total < 1 )
        {//if no row was returned
            echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
            echo "<td> No Data: DataBase Empty </td> ";//print out error message
        }
        else
        {
            while($res = $st->fetchObject()){//loop through the returned rows
            echo "<tr>";
            if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
            {
                echo "<td> $res->ParkingSlotNo </td> ";
                echo "<td>   <img src='img/occupied.png'/> </td>";
                echo"<td><a href=''> </a> </td>";
            } 
            elseif($res->ParkingSlotNo and $res->Status=='RESERVED')
            {
                echo "<td> $res->ParkingSlotNo </td> ";
                echo "<td>   <img src='img/registered.png'/> </td>";
                echo"<td><a href=''> </a> </td>";
            }
            else
            {
                echo "<td> $res->ParkingSlotNo </td> ";
                echo "<td> <img src='img/vacant.png'> </td>";
                echo"<td><input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'></td>";
            }

            echo"</tr>";

        }
}


?>
</table>
  <input type="submit" value="Submit">

</form>
</div>
</div>
</body>
</html>

我的第二个文件如下文件名:payment.php

<?php

    require_once ('navbar.php');
    require_once ('dbconfigpdo.php');
    $parkingslot = $_POST['parkingslot'];
    $booking_start=$_POST['booking_start'];
    $booking_end=$_POST['booking_end'];
    $booking_start_time=$_POST['booking_start_time'];
    $booking_end_time=$_POST['booking_end_time'];
    session_start();
    $_SESSION['parkingslot'] = $parkingslot;
    $_SESSION['booking_start'] = $booking_start;
    $_SESSION['booking_end'] = $booking_end;
    $_SESSION['booking_start_time'] = $booking_start_time;
    $_SESSION['booking_end_time'] = $booking_end_time;
    //$_SESSION['price'] = $price;



    $a = new DateTime($booking_start_time);
    $b = new DateTime($booking_end_time);
    $interval = $a->diff($b);

    echo $duration=$interval->format("%H");
    echo $duration;
    $price = '3' * $duration;
    echo $price;
    echo $parkingslot;

?>
<html>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
//set your publishable key
Stripe.setPublishableKey('pk_test_ApEdDkstpR0xRuASqSbz0fn9');

//callback to handle the response from stripe
function stripeResponseHandler(status, response) {
    if (response.error) {
        //enable the submit button
        $('#payBtn').removeAttr("disabled");
        //display the errors on the form
        $(".payment-errors").html(response.error.message);
    } else {
        var form$ = $("#paymentFrm");
        //get token id
        var token = response['id'];
        //insert the token into the form
        form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
        //submit form to the server
        form$.get(0).submit();
    }
}
$(document).ready(function() 
{
    //on form submit
    $("#paymentFrm").submit(function(event) 
    {
        //disable the submit button to prevent repeated clicks
        $('#payBtn').attr("disabled", "disabled");

        //create single-use token to charge the user
        Stripe.createToken({
            number: $('.card-number').val(),
            cvc: $('.card-cvc').val(),
            exp_month: $('.card-expiry-month').val(),
            exp_year: $('.card-expiry-year').val()
        }, stripeResponseHandler);

        //submit from callback
        return false;
    });
});
</script>

<h1>Payment</h1>

<!-- display errors returned by createToken -->
<span class="payment-errors"></span>

<!-- stripe payment form -->
<form action="paymentsubmit.php" method="POST" id="paymentFrm">
    <p>
        <label>Name</label>
        <input type="text" name="name" size="50" />
    </p>
    <p>
        <label>Email</label>
        <input type="text" name="email" size="50" />
    </p>
    <p>
        <label>Card Number</label>
        <input type="text" name="card_num" size="20" autocomplete="off" class="card-number" />
    </p>
    <p>
        <label>CVC</label>
        <input type="text" name="cvc" size="4" autocomplete="off" class="card-cvc" />
    </p>
    <p>
        <label>Expiration (MM/YYYY)</label>
        <input type="text" name="exp_month" size="2" class="card-expiry-month"/>
        <span> / </span>
        <input type="text" name="exp_year" size="4" class="card-expiry-year"/>

    </p>

    <button type="submit" id="payBtn">Submit Payment</button>
</form><strong></strong>
</html>

最后第三个文件是paymentsubmit.php

    <?php
require_once ('payment.php');
require_once ('dbconfigpdo.php');


echo $parkingslot;
?>

请帮帮我

它给我带来了错误。

( ! ) Notice: Undefined index: parkingslot in C:\wamp64\www\PROJECT\payment.php on line 5
( ! ) Notice: Undefined index: booking_start in C:\wamp64\www\PROJECT\payment.php on line 6
( ! ) Notice: Undefined index: booking_end in C:\wamp64\www\PROJECT\payment.php on line 7
( ! ) Notice: Undefined index: booking_start_time in C:\wamp64\www\PROJECT\payment.php on line 8
( ! ) Notice: Undefined index: booking_end_time in C:\wamp64\www\PROJECT\payment.php on line 9

我可以在第二页(付款)上看到并打印会话变量,但是当我向paymentsubmit页面提交详细信息时,我无法获得值,我的意思是当我尝试打印会话变量时,它显示我在变量中为null并抛出我以上错误。

1 个答案:

答案 0 :(得分:0)

我认为paymentsubmit.php是您实际处理付款的地方,在这种情况下,您肯定不希望包含payment.php,因为这会创建新的付款提交表单。 paymentsubmit.php应该按照你在其中一条评论中的说法开始:

<?php
require_once ('dbconfigpdo.php');

session_start();
$parkingslot= $_SESSION['parkingslot']; 
$booking_start = $_SESSION['booking_start'];
$booking_end = $_SESSION['booking_end']; 
$booking_start_time = $_SESSION['booking_start_time'];
$booking_end_time = $_SESSION['booking_end_time']; 
$price = $_SESSION['price'];

// read new POST variables (save in $_SESSION if required)
$name = $_SESSION['name'] = $_POST['name'];
$email = $_SESSION['email'] = $_POST['email'];
$card_num = $_SESSION['card_num'] = $_POST['card_num'];
$cvc = $_SESSION['cvc'] = $_POST['cvc'];
$exp_month = $_SESSION['exp_month'] = $_POST['exp_month'];
$exp_year = $_SESSION['exp_year'] = $_POST['exp_year'];

// process data

echo $parkingslot;
?>

请注意,您可能应该检查session_start()payment.phppaymentsubmit.php的结果。