Re: Can you pass an array URL variable
.oO(patricktr)
> Doing a form which I want to validate, then re-display with error messages and
>the keyed data still in place should there be errors, or go onto a second form
>if all ok.
OK, quite common.
> I have tried to use <form action="<?=$_SERVER['PHP_SELF']?>"
Avoid short open tags, they are unreliable. Use "<?php print " instead
of just "<?=" to be safe and independent from the server configuration.
>but as the
>various outcomes result in different screens I can't see how to do it without
>having reams of duplicate code - I couldn't make it work satisfactorily anyway.
What's a "screen" in this case? Just another part of the form or a
completely different page?
> So I decided to do it in two stages - form (user screen) + a separate
>validation routine which passes validation results back if there are errors by
>calling the first screen but with URL variables to trigger process variations
>or go onto screen 2 if all ok.
Don't use URL parameters in such a form processing scenario. Use a
session instead, that's what they are for. The length of URLs is limited
and there are things you simply don't want to pass between pages, but
keep on the server instead.
> But I'm struggling with this .. two questions:
>
> i) Ideally I would like to use a session variable to pass actual error
>messages back to screen one in the event of errors but if I undertand things
>correctly (which is by no means certain) $S_Session is already an associatve
>array so it wouldn't be so easy to just add a variable number of messages to it
>and then know what you are unpacking elsewhere ... do you know what I mean?
The $_SESSION array itself is strictly associative, the used indexes
must be strings or the serialization of the session data will fail.
But if course you can always do things like this:
$_SESSION['errors'] = array();
$_SESSION['errors'][] = 'something went wrong';
> Perhaps if I give you my second question it may help illustrate what I'm going
>on about in part 1
> ii) The way I have tried to do it is to set it up as an array ($ERRORS) in the
>validation module and then added a text string each time I hit a specific
>error. The hope was that I could then send this back via the URL for further
>process but I'm getting syntax problem so maybe this is not possible .... a
>brief example ...
As said above - use the session instead.
> Input Form php:
> $ERRORS = array();
> $ERRORS = $_GET['errors']
There's a ';' missing at the EOL (this causes the error you mentioned
below).
Just a naming hint: Variables should not be named all uppercase (except
for the predefined superglobal arrays). Such names should be reserved
for constants. The most common style looks like this:
myNiceFunction()
$myNiceVariable
MY_NICE_CONSTANT
> if (sizeof($ERRORS) > 0) {
if (!empty($_SESSION['errors'])) {
> header("Location: input.form.php?errors=$ERRORS");
The Location URI must be absolute including scheme and hostname. This is
required by the HTTP spec:
header("Location: http://$_SERVER[HTTP_HOST]/input.form.php ...");
But I'm still not sure why you would need this redirect. The entire
handling of a form (validation, processing) can be done on a single
page. Only if the processing succeeds, you might want to redirect to
some result page.
Micha
|