1

I wanted to make sure I am adding using these arrays correctly, could someone please go over my code and clarify. I have tried printing the array and nothing is displaying.

HTML

Start Date: <input type="text" name="start_date[]"/> 
End Date: <input type="text" name="end_date[]"/> 
Description:<textarea name="position[]"></textarea>

PHP

initializeArrays(); // Initialize arrays
$_SESSION['start_date_array'][] = $_GET['start_date[]']; // Add html input arrays to a session array.
$_SESSION['end_date_array'][] = $_GET['end_date[]'];
$_SESSION['position_array'][] = $_GET['position[]'];
$_SESSION['submit_employment_message'] = 'Thank you for the submission';

I want to set the array I am getting to my session array. Essentially I am expecting there to be multiple start dates. Being submitted to the PHP page. For example there could be multiple of start dates inputted. Please let me know if you need any clarification. Thank you for the help!

3
  • Did you try to print the output from the php page? Then you can verify for yourself. Commented Feb 6, 2013 at 1:01
  • Yes I tried and it is not printing. Commented Feb 6, 2013 at 1:01
  • I'm pretty sure you don;t need the brackets in $_GET['start_date[]']; just $_GET['start_date']; will give you an array Commented Feb 6, 2013 at 1:01

2 Answers 2

2

I think you want:

$_SESSION['start_date_array'] = $_GET['start_date'];
$_SESSION['end_date_array'] = $_GET['end_date'];
$_SESSION['position_array'] = $_GET['position'];

Form fields that have [] appended to their name are assumed to be arrays

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

2 Comments

But you know that you don't need them in your example? Because you only have one of each...
Yeah, there are going to be more, there will be a button where it will add more start dates.
1

You can make foreach on $_GET and retrieve key=value

foreach ($_GET as $key => $value) {
    $_SESSION[$key] = $value;
}

and retrieve the name of the sessions by the key of $_GET

Comments

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.