-1
<?php   
$_SESSION['name'] = array($_POST['name']) ;
$n = $_SESSION['name'][0];
setcookie('name[0]',$n,time()+(60*30));
?>
<html>
    <form class="contact100-form validate-form" action="step-3.php" >
        <input class="input100" type="text" name="name[]" placeholder="Enter Your First Name " value="<?php echo $_COOKIE['name[0]']; ?>"  />
        <button class="contact100-form-btn" type="submit" formmethod="post" onclick="valid()">
            Next
        </button>
    </form>
</html>

I need to set COOKIE so as to retain the data in the form in case the user clicks the browser's back button from the the action page.

But somehow the cookie is unable to set. The error:

Notice: Undefined index: name[0]

What am I doing wrong?

P.S. I need to save the data as an array also suggest if there is a better way to do his. maybe using session

1 Answers1

0

Your session is not started, thus the session array is not accessible.

Add session_start(); as the line below the PHP tag and it should work.

Andreas
  • 23,610
  • 6
  • 30
  • 62