0

I have created a form that contains about 20 lines of entry. When user user navigate away after starting filling the form he will start over after coming back. I want to save data on the form until user decided to reset or save. I red some docs about session and cookies but do not know haw to insert it on my form. Can some one hel me achieving this?

entry form

    <?php session_start(); 


?>

<form action="../action/subs/custompcorder.php/" method="post" id="ccomputer" >

   <div id="intel">
     <ul>
       <li >

        <input id="name" name="part_id[]" type="text"/> 

        <input id="quantity" name="quantity[]" type="text"/>  

        <input id="name-data" name="price[]" type="text" />

      </li>

      <li >

        <input id="name" name="part_id[]" type="text"/> 

        <input id="quantity" name="quantity[]" type="text"/>  

        <input id="name-data" name="price[]" type="text" />

      </li>

    </ul>

  </div>

  <input id="submit" type="submit" value="Submit Order" name="submission"/>

processing form to save data in mysql db

<?php

include '../db/connect.php';

// insert order details

foreach (array('part_id', 'quantity', 'price') as $pos) {
  foreach ($_POST[$pos] as $id => $row) {
    $_POST[$pos][$id] = mysqli_real_escape_string($con, $row);
  }
}

$ids = $_POST['part_id'];
$quantities = $_POST['quantity'];
$prices = $_POST['price'];

$items = array();

$size = count($ids);

for ($i = 0; $i < $size; $i++) {
  // Check for part id
  if (empty($ids[$i]) || empty($quantities[$i]) || empty($prices[$i])) {
    continue;
  }
  $items[] = array(
      "part_id" => $ids[$i],
      "quantity" => $quantities[$i],
      "price" => $prices[$i]
  );
}

if (!empty($items)) {
  $values = array();
  foreach ($items as $item) {
    $values[] = "('{$item['part_id']}', '{$item['quantity']}',"
    . "'{$item['price']}','$orderid')";
  }

  $values = implode(", ", $values);

  $sql = "INSERT INTO oz2ts_custompc_details (part_id, quantity, "
          . "price,order_id) VALUES   {$values}    ;
    ";
  $result = mysqli_query($con, $sql);
  if ($result) {
    echo 'Successful inserts: ' . mysqli_affected_rows($con);
  } else {
    echo 'query failed: ' . mysqli_error($con);
  }
}
?>
user3383794
  • 17
  • 1
  • 7
  • You should use prepared statements to prevent [Bobby Tables](http://bobby-tables.com) from messing up your data. If you want to save the info, you're going to need javascript and add event listeners for any change events, like text typed in. Personally, I'd use AJAX to send the changes to a script that added the info to session variables. – Matthew Johnson Apr 25 '14 at 17:55

2 Answers2

1

This probably won't be as simple as you think. You'll need to use AJAX to send the contents of the form to a script that will save it to the session. Whether you do this at regular intervals or whenever the content in the form changes is up to you.

This question here will help get you started: How to update SESSION variable with jQuery + AJAX, is it even possible?

Edit: Forgot to mention, the answer marked as correct there should pretty much do what you want, except only when the form is submitted. You'll want to do it, as I mentioned, at intervals or when the user changes the form. Look into setInterval for the former (probably the easiest option).

Instead of using $("#formid").submit(function(){CODE HERE} it will be more like setInterval(function(){CODE HERE}, 10000)...

If you need any help once you've given it a go, post your attempt and I can give you a hand.

Community
  • 1
  • 1
SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25
1

I think we need (in the first place) to realize what are sessions & cookies

  • Sessions are files saved on YOUR SERVER , carrying saved data.
  • However Cookies are data saved on the CLIENTS BROWSER mainly for temporary usage.

Regarding Sessions , you can do the following :

<?php
session_start();

$_SESSION['first_one'] = "DATA ENTERED BY USER";
// this will actually save the data to a session

& to retrieve data saved in session just use ( $_SESSION['first_one'] )

Regarding cookies , you can do the following ;

setcookie('first_one', "DATA ENTERED BY USER", time()+3600);
/* expire in 3600 seconds (1 hour) */

also to retrieve data from users cookie, just use ( $_COOKIE['first_one'] )

I suggest you read more :

Hossam
  • 1,126
  • 8
  • 19
  • in my case, is this code will be correct? `` – user3383794 Apr 25 '14 at 18:22
  • No, I didn't mean that, I was trying to put you on the beginning of the road. in your case , a solution that will save user's data AFTER pressing submit is as following : – Hossam Apr 25 '14 at 18:53
  • I don't want to save before process. I just want to keep data in a session or cookies – user3383794 Apr 25 '14 at 19:06
  • I am unsure on where to enter my session variables. Should it be in the form where user enters data or on the file that process the form. Can I keep my processing file as it and just define session variable? – user3383794 Apr 25 '14 at 19:10
  • Hi user3383794. I think you're slightly confused. You can still process your form as you usually would, but if you want the data to be stored in the session before the form is processed you'll need to use AJAX (as mentioned in my answer) to send the contents of the form to your server. This won't interfere at all with whatever happens when you process the form, but it is not possible to put the data into the session without sending it somehow (using AJAX it can be sent without the user having to press anything) to your server. – SharkofMirkwood Apr 25 '14 at 23:14