I'm trying to set cookies from a form on page 1 and echo the information to page 2 but page 2 just keeps showing a blank page when i try

Ale

I'm trying to set cookies from a form on page 1 and echo the information to page 2 but page 2 just keeps showing a blank page when i try.

I set the cookies on page1 and then called them on page 2. I expected the information to show on page2 but it did not.When i run the code as a whole on page1 it prints out the information under the form. This is the code when I put it together as a whole on page one it runs normally .

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSE3101 -Registration</title>
</head>
<body>

    <form action=""   method="POST">
      <Label for="Fname">Firstname:</Label><br>
      <input type="text" id="fname"  name="fname">
      <br>
      <Label for="Lname">Lastname:</Label><br>
      <input type="text" id="lname"  name="lname">
      <br>
      <Label for="Email">Email:</Label><br>
      <input type="text" id="Email"  name="email">
      <br>
      <Label for="DOB">DOB:</Label><br>
      <input type="date" id="DOB"  name="dob">
      <br>
      <Label for="Password">Password:</Label><br>
      <input type="password" id="password"  name="password">
      <br>
      <Label for="Confirm Password">Confirm Password:</Label><br>
      <input type="text" id="cpassword"  name="cpassword">
      <input type="submit" value="Submit" whenclicked= "page2.php">
    </form>

   <?php 
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST["fname"], $_POST["lname"], $_POST["email"], $_POST["dob"], $_POST["password"], $_POST["cpassword"])) {
            $fname = $_POST["fname"];
            $lname = $_POST["lname"];
            $email = $_POST["email"];
            $dob = $_POST["dob"];
            $password = $_POST["password"];
            $cpassword = $_POST["cpassword"];

            setcookie("fname", $fname, time() + (86400 * 30), "/");
            setcookie("lname", $lname, time() + (86400 * 30), "/");
            setcookie("email", $email, time() + (86400 * 30), "/");
            setcookie("dob", $dob, time() + (86400 * 30), "/");
         

                
                               

        if(isset($_COOKIE["fname"], $_COOKIE["lname"], $_COOKIE["email"], $_COOKIE["dob"])) {
            $fname = $_COOKIE["fname"];
            $lname = $_COOKIE["lname"];
            $email = $_COOKIE["email"];
            $dob = $_COOKIE["dob"];
            echo "First Name: $fname <br>";
            echo "Last Name: $lname <br>";
            echo "Email: $email <br>";
            echo "Date of Birth: $dob <br>";
        } else {
            echo "Cookies not set";
        }
                

            }



        }

    

    ?>

            
            


              

            

            </body>
</html>

















Pedro

Please refer to PHP manual

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

https://www.php.net/manual/en/function.setcookie.php

As per I can see you're creating the cookie after the HTML outoput.

This example is working for me:

     <?php 
      if($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST["fname"], $_POST["lname"], $_POST["email"], $_POST["dob"], $_POST["password"], $_POST["cpassword"])) {
      $fname = $_POST["fname"];
      $lname = $_POST["lname"];
      $email = $_POST["email"];
      $dob = $_POST["dob"];
      $password = $_POST["password"];
      $cpassword = $_POST["cpassword"];

      setcookie("fname", $fname, time() + (86400 * 30), "/");
      setcookie("lname", $lname, time() + (86400 * 30), "/");
      setcookie("email", $email, time() + (86400 * 30), "/");
      setcookie("dob", $dob, time() + (86400 * 30), "/");

      if(isset($_COOKIE["fname"], $_COOKIE["lname"], $_COOKIE["email"], $_COOKIE["dob"])) {
          $fname = $_COOKIE["fname"];
          $lname = $_COOKIE["lname"];
          $email = $_COOKIE["email"];
          $dob = $_COOKIE["dob"];
          echo "First Name: $fname <br>";
          echo "Last Name: $lname <br>";
          echo "Email: $email <br>";
          echo "Date of Birth: $dob <br>";
      } else {
          echo "Cookies not set";
      }
    }
  

    } else {
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>CSE3101 -Registration</title>
    </head>
    <body>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>"   method="POST">
      <Label for="Fname">Firstname:</Label><br>
      <input type="text" id="fname"  name="fname">
      <br>
      <Label for="Lname">Lastname:</Label><br>
      <input type="text" id="lname"  name="lname">
      <br>
      <Label for="Email">Email:</Label><br>
      <input type="text" id="Email"  name="email">
      <br>
      <Label for="DOB">DOB:</Label><br>
      <input type="date" id="DOB"  name="dob">
      <br>
      <Label for="Password">Password:</Label><br>
      <input type="password" id="password"  name="password">
      <br>
      <Label for="Confirm Password">Confirm Password:</Label><br>
      <input type="text" id="cpassword"  name="cpassword">
      <input type="submit" value="Submit" whenclicked= "page2.php">
    </form>        
  </body>
</html>
<?php
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I try to run my "form" but it stuck at the second page with a blank page and did not pass the data to my third page

when i am trying to call a servlet page from my jsp page using anchor tag it showing an error

I want clear the cookies of a page when loading from another page but not when the same page is reloaded

When redirect from page 1 to page 2, shows page 2 loading instead of page 1 loading

My page turns blank when I try to use a controller in angular

Angular keeps redirecting me to the login page even though I'm trying to access another page

htaccess showing error when I try to open page

jQuery script keeps redirecting me from a page I'm trying to edit

Skyepub on swift2 showing blank page

When I try to echo data from database using php my html page breaked

When i try to get data from data for edit page it's showing me error

What I'm getting blank page?

how can i transfer integer data from page1 to page2?

Web Form with Master Page keeps loading but not showing

Undefined index when trying to get information from a 2nd page

App crashes when i try to run this page

ASP.NET: I'm trying to set a label to show an image button but it isn't showing no the page, How do i do this?

Webmatrix showing blank page

Blank page when trying to set up Apache and Django together

Page is just being refreshed when i try to add data. mysqli_stmt_close() expects parameter 1 to be mysqli_stmt

How can I fetch information from database without using $ _GET and showing information in page

Set a Label text in 1st winform page to display on 2nd form page

PHP YII 2 why when i add new page, it's not showing nothing?

I'm trying to decode json array to php but it's returning a blank page

I am trying to create a javascript page to enter information into a form and it's not working

Blank page when trying to access phpmyadmin

I got blank page when I used Storybook for Angular?

Ruby on Rails, I try to create a form using the application, but when I get to the show page..everything is empty

I have a blank page after submitting a php mysqli update form,