简体   繁体   English

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

[英]how to pass values from one page to another then further to third page

I am working on a code where one php page sends values to second page and on second page users again adds some values and redirects them to another page where MySQL queries will be executed. 我正在编写一个代码,其中一个php页将值发送到第二页,而在第二页上,用户再次添加一些值并将其重定向到将执行MySQL查询的另一页。 I used require function to include second file but when I try to process the third page it gives me undefined index error. 我使用了require函数来包含第二个文件,但是当我尝试处理第三页时,它给了我未定义的索引错误。

my code is as below: for the first file named display.php 我的代码如下:对于名为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>

my second file is as below file name: payment.php 我的第二个文件如下文件名: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>

and finally third file is paymentsubmit.php 最后第三个文件是paymentsubmit.php

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


echo $parkingslot;
?>

please help me out 请帮帮我

it gives me errors like. 它给我类似的错误。

( ! ) 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

I can see and print session variable on second page(payment) but when I submit details to paymentsubmit page I am not able to get values there I mean when I try to print session variable it shows me null in variables and throws me above errors. 我可以在第二页(付款)上看到并打印会话变量,但是当我向付款提交页面提交详细信息时,我无法在那里获取值,这意味着当我尝试打印会话变量时,它显示变量为null并抛出错误。

I presume paymentsubmit.php is where you actually process the payment, in which case you definitely don't want to include payment.php as that will create a new payment submit form. 我假设您实际上是在进行付款处理,在这里, paymentsubmit.php是您绝对不希望包含的payment.php因为这将创建一个新的付款提交表单。 paymentsubmit.php should start as you said in one of your comments: 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;
?>

Note that you probably should be checking the result of session_start() in both payment.php and paymentsubmit.php . 请注意,您可能应该同时在payment.phppaymentsubmit.php检查session_start()的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM