0

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. I used require function to include second file but when I try to process the third page it gives me undefined index error.

my code is as below: for the first file named 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

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

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

Sachin611
  • 25
  • 6
  • sessions or hidden form fields on page 2 to pass variables from page 1 to page 3 –  Apr 08 '18 at 21:17
  • "but when I try to process the third page it gives me undefined index error" is this critical information or is this just a nice to know thing? – Scuzzy Apr 08 '18 at 21:32
  • @Scuzzy its a critical information. Its an error. I am looking for this solution – Sachin611 Apr 08 '18 at 21:36
  • Generally a PHP error will print which line it appears on, and with that error, what index you're trying to reference. Paste the error into the case and tell us which line represents the line number in the error. – Scuzzy Apr 08 '18 at 21:37
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Scuzzy Apr 08 '18 at 21:37
  • @smith I tried that its giving me an warning message: Warning: session_start(): Cannot send session cache limiter - headers already sent – Sachin611 Apr 08 '18 at 21:37
  • Scuzzy ( ! ) ( ! ) Notice: Undefined index: parkingslot in C:\wamp64\www\PROJECT\payment.php on line 4 ( ! ) Notice: Undefined index: booking_start in C:\wamp64\www\PROJECT\payment.php on line 5 ( ! ) Notice: Undefined index: booking_end in C:\wamp64\www\PROJECT\payment.php on line 6 ( ! ) Notice: Undefined index: booking_start_time in C:\wamp64\www\PROJECT\payment.php on line 7 ( ! ) Notice: Undefined index: booking_end_time in C:\wamp64\www\PROJECT\payment.php on line 8 – Sachin611 Apr 08 '18 at 21:39
  • tried what? cant read minds, need to see code and errors –  Apr 08 '18 at 21:49
  • I tried using session below is the code – Sachin611 Apr 08 '18 at 21:52
  • and on third page my code is like below. $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']; – Sachin611 Apr 08 '18 at 21:57
  • 1
    Please do not post this info in comments, update your question so it's readable, and in the context of your question. – Sloan Thrasher Apr 08 '18 at 22:09
  • @SloanThrasher extremely sorry about that. I have updated the code now and also errors which I am getting. – Sachin611 Apr 08 '18 at 22:21
  • @smith see the updated code now. – Sachin611 Apr 08 '18 at 22:40

1 Answers1

0

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 should start as you said in one of your comments:

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

Nick
  • 138,499
  • 22
  • 57
  • 95