![]() |
![]() |
||||||
|
|||||||
| Tags: array, pass, url, variable |
![]() |
|
|||
|
Hi,
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. I have tried to use <form action="<?=$_SERVER['PHP_SELF']?>" 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. 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. 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? 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 ... Input Form php: $ERRORS = array(); $ERRORS = $_GET['errors'] if (sizeof($ERRORS) > 0) { echo "<p class=\"val_err_hdr\"> *** Validation error(s) - please correct the entries and try again *** </p>"; blah blah Validation php: $ERRORS=array(); if(!$loginFoundUser) { $ERRORS[] = "This e-mail address entered has already been registered on our database - if you have already registered you can"; } header("Location: input.form.php?errors=$ERRORS"); When I run this I get a syntax error 'unexpected T_IF' on the 'sizeof'' function condition. Any help much appreciated. |
| Sponsored Links |
|
|||
|
.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 |
|
|||
|
Hi Micha,
Thanks a lot - all good stuff. PS1. For my branching 'header' line I am just quoting the name of the php I want to go to and it's working ok for me .... what does this mean? PS2. Yes I'm sure I am missing a trick using two routines instead of one but I tried for a while and I couldn't see how to do it in one place... I'll continue to try. By screen I mean the page that the end user is presented with. There are effectively 2 pages (it's a multi part form); page one iterates until it passes validation checks and then page 2 kicks in. When that page is validated, I update a db with info from both pages. Regards. Patrick. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise