2

***Here is the html input part of my code:

<input type="text" name="subject" id="subject" value="subject" />

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

***User will click fill that form multiple times to insert multiple 'subjects'.Each time the input field value will be stored inside an array.When the user adds another 'subject',it will be stored in the next index of the array.

***Here is what I have so far regarding my PHP end.

$i=0; //this is declared globally at the beginning of my page;before html tag                   

$array=array();
if(isset($_POST['submit']))
{
$subject=$_POST['subject'];
$array[$i]=$subject;
$i=$i+1;
}
2
  • Can you describe your question multiple html input field?? Commented Oct 8, 2017 at 21:34
  • I am using only one input field but user will post data multiple times through one input field.And every time it will be saved in the array Commented Oct 8, 2017 at 21:49

1 Answer 1

1

You can simply store your 'subjects' into the session, so you can easily put and get what you want.

<?php
    session_start();    // THIS IS FOR SUPERGLOBAL VARIABLE $_SESSION

    $i=0;     

    // THIS STORES YOU INDEX '$i'
    if(!isset($_SESSION['index']))
    {
        $_SESSION['index'] = 0;
    }else{
        $i = $_SESSION['index'];
    }       

    if(!isset($_SESSION['array']))
    {
        $_SESSION['array'] = array();
    }

    if(isset($_POST['submit']))
    {
        $subject = $_POST['subject'];
        $_SESSION['array'][$i] = $subject;
        $_SESSION['index'] = $i + 1;
    }
?>

This should work for you ;)

Sign up to request clarification or add additional context in comments.

2 Comments

had to add session_destroy(), otherwise it was storing value from each session! but works perfectly for me, so it's the way to go! thanks a lot man :)
You are welcome Muktadir. Just put session_destroy() where you want to delete the stored array values. For example with a button that makes a POST request to a page that handles the event and destroys the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.