2

I have a an array I want, whenenever i submit my form the post value get inserted into that array, every time with new incremented index.. How can I do that? And yes submited page is redirected to itself...

2 Answers 2

3

In PHP:

$arr[] = value;
$arr[] = other value;

In HTML:

<input type="text" name="arr[]" />
<input type="text" name="arr[]" />
Sign up to request clarification or add additional context in comments.

4 Comments

But this replaces my last inserted value in array
I have a $_SESSION['bannersize'][] array, everytime form is submitted the value $_POST['bannersize'] should get appended in the array along with previous values, but this is not happening, every new value replacess the last values resulting in I am available with only one value at 0th index
Are you sure you're adding to the session like this? $_SESSION['bannersize'][] = $_POST['bannersize']; That should definitely work.
Answer given by @faileN is what I am doing conceptually... but its not working , it overrides the values...
2

You could solve this using the session-variables. This way the data won't be overridden. Here's an example:

$_SESSION['formdata'][] = $_POST['value1'];
$_SESSION['formdata'][] = $_POST['value2'];

After you reload the script the data will still be available an pushing other values into the array won't override the old ones. You just do it again:

$_SESSION['formdata'][] = $_POST['value3'];
$_SESSION['formdata'][] = $_POST['value4'];

Calling the $_SESSION['formdata']-Array you now have all 4 values stored in there.

** EDIT ** I am finally home, and as promised I'm offering another solution using cookies, since sessions don't work for you, as we've already discussed. So here we go...

First of all we need to think of the way we want to name the cookie. I would suggest we would do this by ip-address and any suffix, to ensure, that it is really the user who has already filled, the former forms.

So we could go like this:

$dataArray[] = $_POST['value1'];
$dataArray[] = $_POST['value2'];

Then we need to store the data into an cookie. We do this by serializing the array, since we don't want to save hundreds of cookies. This would work like this:

$cookievalue = serialize($dataArray);

// Here we actually generate a cookiename in the format of "IP_formdata"
setcookie($_SERVER['REMOTE_ADDR'] . '_formdata', $cookievalue);

So far so good. Within the next form we retrieve the cookie-data and unserialize the data, so we can extend this array:

$dataArray = unserialize($_COOKIE[$_SERVER['REMOTE_ADDR'] . '_formdata');

Now we can add other values to the array from the second form:

$dataArray[] = $_POST['value3'];
$dataArray[] = $_POST['value4'];

After all additional values have been put into this array we serialize it again and store it into the again again:

$cookievalue = serialize($dataArray);
setcookie($_SERVER['REMOTE_ADDR'] . '_formdata', $cookievalue);

Now we can repeat this steps for all further forms. Just remember that if you want to work with the data you first have to unserialize the data and store it into an array.

And don't forget as I have already stated in the comments: The user can turn off cookies, then the whole thing won't work. You could also add some additional checks to verify that this is the correct user or something. I haven't tested the code, but I think it should work in a way like this and I hope I could help you or at least give you a hint :)

9 Comments

check if you're unintentionally wrote a line like '$_SESSION['formdata'][] = array();' anywhere in your code. If so, than the array will be cleared. Just remove a line like that. What happens when you run this: echo '<pre>'; print_r($_SESSION['formdata']); echo '</pre>';
It outputs me with a single value which is inserted at last submit
I am sure it gets reseted somewhere, but i dont have any such initiallization or termination of session variable in my page
Okay this is really strange. Maybe the error is located in another place. Maybe there's something wrong with your session-handler and you always start a complete new session instead of taking the existing one. Try the following: Put 'echo session_id();' in your code. Call the form for the first time. copy/remember the session-id. Then use the form the second time and compare the session-ids. They must be the same. If they are not, than your page creates a complete new session for every request (which shouldn't be of course).
yes this is the case... id is changing...i cannot stop changing it, its the need of logic.... what could be the alternative way to accomplish my goal....?
|

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.