views:

44

answers:

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...

+3  A: 

In PHP:

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

In HTML:

<input type="text" name="arr[]" />
<input type="text" name="arr[]" />
Sjoerd
But this replaces my last inserted value in array
OM The Eternity
No, it doesn't.
Ignacio Vazquez-Abrams
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
OM The Eternity
Are you sure you're adding to the session like this?$_SESSION['bannersize'][] = $_POST['bannersize'];That should definitely work.
Keeper
Yes Absolutely, it echos me with value latest inserted.. but not old ones..
OM The Eternity
Answer given by @faileN is what I am doing conceptually... but its not working , it overrides the values...
OM The Eternity
+2  A: 

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 :)

faileN
Thats what exactly i am doing... But its not workling
OM The Eternity
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>';
faileN
It outputs me with a single value which is inserted at last submit
OM The Eternity
I am sure it gets reseted somewhere, but i dont have any such initiallization or termination of session variable in my page
OM The Eternity
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).
faileN
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....?
OM The Eternity
Okay if the id changes, than it's no wonder, that this approach doesn't work. Another possibility is to store the data into cookies. But this is not safe enough, since the user can turn off cookies in the browser... Why do you have to create a new session-id? The session is useless, if you do so, isn't? I mean sessions were invented for persistence.
faileN
i cant help it its the need...
OM The Eternity
Alright since I am at work right now, I don't have the time to post a cookie example, but I will do so in the evening. I think beside the cookie and session solution there is unfortunately nothing else to manage your problem.
faileN
Thanx for ur great support failen Thanx again
OM The Eternity
Hey I just updated my given answer extending it with the cookie-solution. I hope it works for you. But I will keep an eye on your next comment ;)
faileN