简体   繁体   中英

Undefined index in cookie in php

First file:-

<!doctype html>
<html>
 <head> 
 <link rel="stylesheet" href="mexp_css.css"> 
  <title>php_main3_feed</title> 
 </head> 
 <body> 
 <?php
    if(isset($_POST['submit']))
    {
        $a=$_POST['fname'];
        $b=$_POST['email'];
        $c=$_POST['cnum'];
        setcookie("c1",$a,time()+3600);
        setcookie("c2",$b,time()+3600);
        setcookie("c3",$c,time()+3600);
    }
  ?>
  <div> 
   <b> 
    <table border="1"> 
     <form method="POST" action="">
     <tbody> 
      <tr> 
       <td> <label> First Name </label> </td> 
       <td> <input type="text" name="fname" placeholder=" First name"> </td> 
      </tr> 
      <tr> 
       <td> <label>Email</label> </td> 
       <td> <input type="text" name="email" placeholder=" example@exampl.com"> </td> 
      </tr> 
      <tr> 
       <td> <label>Contact_no</label> </td> 
       <td> <input type="number" name="cnum" placeholder="9999999999"> </td> 
      </tr> 
      <tr> 
      <tr> 
       <td> <input type="submit" value="Submit" name="submit"> </td> 
       <td> <input type="reset" value="Reset" name="reset"> </td> 
      </tr> 
     </tbody> 
     </form>
    </table> </b> 
  </div> 
 </body>
</html>

**Second file**
    <?php
    echo "First name:-".$_COOKIE['c_fname']."<br>";
    echo "Email:-".$_COOKIE['c_email']."<br>";
    echo "Contact number:-".$_COOKIE['c_cnum']."<br>";
    ?>

Description:- I have set cookie in first file and I want to retrieve them in second file but it displays this error.

Error:-

Notice: Undefined index: c_fname in C:\\xampp\\htdocs\\My PHP\\Main\\18_2\\php1.php on line 2 First name:-

Notice: Undefined index: c_email in C:\\xampp\\htdocs\\My PHP\\Main\\18_2\\php1.php on line 3 Email:-

Notice: Undefined index: c_cnum in C:\\xampp\\htdocs\\My PHP\\Main\\18_2\\php1.php on line 4 Contact number:-

cooke at first has no value

<?php
 $fname = $_COOKIE['c_fname'] ? $_COOKIE['c_fname'] : "";
 $cemail= $_COOKIE['c_email'] ? $_COOKIE['c_email'] : "";
 $ccnum= $_COOKIE['c_cnum'] ? $_COOKIE['c_cnum'] : "";

 echo "First name:-".$fname ."<br>";
 echo "Email:-".$cemail."<br>";
 echo "Contact number:-".$ccnum."<br>";
 ?>

and used the same names c1 c2 c3

You haven't used the same names for your cookies. Change your code like below:

setcookie("c1",$a,time()+3600);
setcookie("c2",$b,time()+3600);
setcookie("c3",$c,time()+3600);
echo "First name:-".$_COOKIE['c1']."<br>";
echo "Email:-".$_COOKIE['c2']."<br>";
echo "Contact number:-".$_COOKIE['c3']."<br>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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