Akhil
Akhil

Reputation: 159

How to store array text box in cookie or local storage?

I want to store an array of textbox values in cookie or local storage so next time when user logs in he can see his old values

include('connect.php');
$id = $_GET['ex_id'];

$query = "SELECT * FROM workout_exercise WHERE plan_id = $id";

$res = mysqli_query($con,$query);

$ic = 0;

while($row = mysqli_fetch_array($res))
{
?>

    <input type="text" name="weight[<?php echo $ic++; ?>]" 
           id="weight[<?php echo $ic++; ?>]" 
           placeholder="enter weight">

       ......

Above is the PHP code used to generate the array of textboxes.

Upvotes: 1

Views: 112

Answers (1)

Agam Banga
Agam Banga

Reputation: 2693

JSON encode the array, effectively producing a string like "{data:'value',data1:'value1'}" which you put this in a cookie, you can retrieve when needed and decode back into a JavaScript array/object.

Example - store array in a cookie:

var arr = ['foo', 'bar', 'baz'];
var json_str = JSON.stringify(arr);
createCookie('mycookie', json_str);
Later on, to retrieve the cookie's contents as an array:

var json_str = getCookie('mycookie');
var arr = JSON.parse(json_str);

You can get the method of cookies from here How to write & read from cookies

Upvotes: 1

Related Questions