Forum Moderators: coopster

Message Too Old, No Replies

Communicating between scripts

...can it be done?

         

daosmith

1:50 pm on Nov 30, 2003 (gmt 0)

10+ Year Member



Hi all,

I'm trying to send some data between two scripts... well the same script, refreshed. Basically, the situation is this - the user is presented with some form inputs which contain preset values from the local db, which the user can override at his or her convenience. Obviously when s/he submits, their changes are available in the $_POST array. The same script that creates the form inputs also evaluates them after submission.

If there are errors in the input, the script refreshes with an error message. The problem is, the changes the user has made to the form inputs have been overriden with the original values from the database, and the $_POST array which contains their changes is no longer available. However, it is much easier for the user if his or changes are still there and s/he can simply amend the faulty entry, rather than rewrite all of the changes all over again.

So my question is, is there a way to somehow preserve the $_POST array for use in the refreshed page (preferably without adding text to the address string a la $_GET)?

Thanks

dmorison

2:11 pm on Nov 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your form generation and form handling are in the same script there shouldn't be anything to stop you overriding default values pulled from the database with anything in $_POST.

Something like:

if ($_POST["somevar"])
{
$somevar_default = $_POST["somevar"];
}
else
{
$somevar_default = get_somevar_from_database();
}

print "<input type='text' name='somevar' value='".$somevar_default."'>";

daosmith

10:53 pm on Nov 30, 2003 (gmt 0)

10+ Year Member



dmorison - This is in fact what I intend to do but the problem is that after the page refreshes (because of the error) the $_POST array is no longer available, and so the values go back to the database defaults. I'm hoping there is some way of continuing $_POST's lifespan past that of the the submit script.

Failing that, perhaps the data temporarily be stored in the $_SESSION array - is that possible?

Thanks

dmorison

7:06 am on Dec 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You _could_ copy $_POST into the session; but I wouldn't recommend it - at least not without some size checking etc.

I'm not sure exactly what you mean by refreshing. You say "refreshing because of the error"; but in a standard PHP form handling script there is no need to "refresh" anything because of invalid input; just continue on and output the form again - complete with saved values from the original input.

Can you post a small snippet of your code so we can see what you're doing...

daosmith

2:39 pm on Dec 1, 2003 (gmt 0)

10+ Year Member



Thanks dmorison - after considering what you said I figured that the way I was handling errors was just plain silly. I was calling the same script again, with an argument appended to the URL to denote the error type i.e. if an error occured I would reset the location using the header() function.

I've changed it now so there's no refresh and hence the $_POST array is still available; the error is stored in the $_SESSION array instead.