0

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

  • How you add cookies? – Justinas Oct 27 '20 at 10:51
  • Does this answer your question? [Why are my cookies not setting?](https://stackoverflow.com/questions/6970754/why-are-my-cookies-not-setting) – Justinas Oct 27 '20 at 10:52
  • do a `var_dump($_COOKIE)` to view the contents of `$_COOKIE` array. What are the `name`'s you're using with `setcookie()` - use these to reference them. – jibsteroos Oct 27 '20 at 10:56
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase Oct 27 '20 at 13:01

2 Answers2

-1

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

Avat Rezaei
  • 143
  • 9
-1

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>";
Full Stop
  • 904
  • 11
  • 24
droopsnoot
  • 931
  • 1
  • 7
  • 11