want to insert values from text fields into multidimensional array the code is as following:-
<body>
<form action="" method="post">
<h1>Task<h1>
<br/>
<h4>Date:<h4>
<input type="date" name="date" value=""/>
<br/>
<h4>Status:<h4>
<textarea name="status"></textarea>
<br/>
<br/>
<br/>
<input type="submit"name="submit" text="submit"/>
</form>
<?php
$datewise_status=array();
$int=0;
if (isset($_POST['submit']))
{
$datewise_status[$_POST['date']]="".$_POST['status']."";
}
print($int);
print_r($datewise_status);
?>
</body>
gives the following output:
Int value = 0
array = Array ( [2015-06-09] => sadfasdfas )
input date = -015-06-09
text field:- sadfasdfas
You can achieve this easy with jquery, if you change your input type
from submit
to button
. but if you want to to it with php, all you have to do is to change your array variable into session array:
<?php
session_start();//at the beginning of the file
if(!isset($_SESSION['datewise_status'])) //check if array was already created
$_SESSION['datewise_status']=array();
$int=0;
if (isset($_POST['submit']))
{
$_SESSION['datewise_status'][]= array($_POST['date'],$_POST['status']);
}
print($int);
print_r($_SESSION['datewise_status']);
?>
Your code is not working now because you are creating a new array variable every-time you click submit