0

I am making a form in html. When a person clicks on submit, it checks if certain fields are filled correctly, so pretty simple form so far.

However, i want to save the text which is typed into the fields, if a person refreshes the page. So if the page is refreshed, the text is still in the fields.

I am trying to achieve this using php and a cookie.

   // Cookie

   $saved_info = array();
   $saved_infos = isset($_COOKIE['offer_saved_info']) ? explode('][', 
   $_COOKIE['offer_saved_info']) : array();

   foreach($saved_infos as $info)
   {
      $info_ = trim($info, '[]');
      $parts = explode('|', $info_);

      $saved_info[$parts[0]] = $parts[1];
   } 

   if(isset($_SESSION['webhipster_ask']['headline']))
      $saved_info['headline'] = $_SESSION['webhipster_ask']['headline'];

    // End Cookie

and now for the form input field:

<div id="headlineinput"><input type="text" id="headline" 

value="<?php echo isset($_SESSION['webhipster_ask']['headline']) ? 
$_SESSION['webhipster_ask'] ['headline'] : ''; ?>" 

tabindex="1" size="20" name="headline" /></div>

I am new at using SESSION within php, so my quesiton is:

Is there a simpler way of achieving this without using a cookie like above? Or what have i done wrong in the above mentioned code?

Patrick
  • 166
  • 2
  • 14
  • save everything in $_SESSION. From here you can access everything. Or you try jquery cookie plugin and save everything in cookies and read data from cookies via js – brandelizer Nov 18 '14 at 16:53
  • And the best way to save such things in cookies is via a json string (Array to Json) – brandelizer Nov 18 '14 at 16:54
  • 1
    @brandelizer If you're saving in a session, you don't need to use JSON. You can put arrays directly into `$_SESSION`, PHP takes care of serializing it properly. – Barmar Nov 18 '14 at 16:58
  • yes you are right. But i meant for cookie saving (edited it) – brandelizer Nov 18 '14 at 16:59
  • 1
    Look here http://stackoverflow.com/questions/3791414/storing-form-data-as-a-session-variable – brandelizer Nov 18 '14 at 17:01

1 Answers1

1

First thing is I'm pretty sure you're echo should have round brackets around it like:

echo (isset($_SESSION['webhipster_ask']['headline']) ? value : value)

That's not really the only question your asking though I think.

If you're submitting the data via a form, why not validate using the form values, and use the form values in your html input value. I would only store them to my session once I had validated the data and moved on.

For example:

<?php
session_start();
$errors=array();

if($_POST['doSubmit']=='yes')
{
    //validate all $_POST values
    if(!empty($_POST['headline']))
    {
        $errors[]="Your headline is empty";
    }   
    if(!empty($_POST['something_else']))
    {
        $errors[]="Your other field is empty";
    }   

    if(empty($errors))
    {
        //everything is validated   
        $_SESSION['form_values']=$_POST; //put your entire validated post array into a session, you could do this another way, just for simplicity sake here
        header("Location: wherever.php");
    }
}
if(!empty($errors))
{
    foreach($errors as $val)
    {
        echo "<div style='color: red;'>".$val."</div>";
    }   
}
?>
<!-- This form submits to its own page //-->
<form name="whatever" id="whatever" method="post">
<input type="hidden" name="doSubmit" id="doSubmit" value="yes" />
<div id="headlineinput">
<input type="text" id="headline" value="<?php echo $_POST['headline'];?>" tabindex="1" size="20" name="headline" />
<!-- the line above does not need an isset, because if it is not set, it will simply not have anything in it //-->
</div>
<input type="submit" value="submit" />
</form>
Dale
  • 319
  • 2
  • 8