An example being a thousand words ...

<?php
unset($err);
unset($errmsg);
if($submit) { // assumes  you've named your submit button 'submit'
        // just assume $name and $email in FORM for simplicity
        if(trim($name)=="") {
                $err=1;
                $errmsg="Hey, you have to give me your name!";
                }
        if(!ereg("^.+@.+\..+$",$email)) { // something resembling an email
                $err=1;
                $errmsg="Hmm, don't think that's a valid email";
                }
        if(!$err) {
                // I don't do the db setup, just a mock query
                mysql_query("insert into names values ('$name','$email)");
                header("location:mysuccesspage.php");
                exit();
                }
        }
?>
<HTML>
<BODY>
<?php
if($err) {
        echo $errmsg;
        }
?>
<form action="<?php echo $PHP_SELF;?>" method=POST>
Enter name:<br>
<INPUT TYPE=TEXT NAME=name value="<?php echo stripslashes($name);?>">
<br>
Enter email:<br>
<INPUT TYPE=TEXT NAME=email value="<?php echo stripslashes($email);?>">
<INPUT TYPE=submit name=submit>
</form>
</body>
</html>

Now I haven't actually tested it I just typed it, but directionally it's
pretty similar to lot's of pages I've written if not a little simplistic
... it also assumes that register_globals is on and does no sanity
checking on GET overrides or anything else nasty the user might try to
do:)

HTH

Hank
On Sun, Nov 25, 2001 at 07:03:08PM +0100, Daniel Als?n wrote:
> Hmm...either i don?t really get what you mean, or you missed the problem.
> 
> This is the situation:
> 
> I have a form which results is to be inserted in a db. When the form is
> submitted the page calls itself and does a db INSERT based on if a couple of
> statements is true - otherwise it displays a proper error message.
> 
> So far everything is fine and is working like a charm.
> But:
> 
> I want a redirection after a successfull db INSERT. After the INSERT is made
> (which is what the page is for) i set a variable, $done, to 1.
> Right now i only use $done to display a message at the bottom of the screen
> saying that the INSERT worked. But i want the page to redirect itself as
> soon as $done is set to 1.
> 
> The problem i have is: I can only send headers (wich i use for redirection -
> right??) at the top of the page - before all the other code. But since $done
> is set far down in the code, after my form and db-statements, the header
> will never know when to be sent.
> 
> Is there a way of calling a headerfunction in the top of the page?
> 
> - Daniel
> 
> > -----Original Message-----
> > From: Christian Dechery [mailto:[EMAIL PROTECTED]]
> > Sent: den 25 november 2001 18:41
> > To: Daniel Als?n; PHP; Miles Thompson
> > Subject: RE: [PHP] Redirect upon execution of script...
> >
> >
> > sorry if I'm not understanding correctly your problem... but it
> > seems quite
> > simple to me...
> > I've created like 10.000 php forms that does that... I don't see a prob...
> >
> > what do you want to do?
> >
> > process a form:
> > if something's wrong, print error message and go back
> > if everything is fine go to another page... that's it?
> >
> > well... database inserts has nothing to do with headers...
> >
> > you can easily do something like
> >
> > <?php
> > switch ($action)
> > {
> >          case "process-form":    if( ProcessForm() )
> >                                          header("Location:
> > next-page.php");
> >                                  else
> >                                  {
> >                                          ShowHeader(); // function to
> > display all the HTML you want
> >                                          printErrMessage();
> >                                          ShowFooter();
> >                                  }
> >                                  break;
> >          default:                        ShowForm();
> >                                  break;
> > }
> > ?>
> >
> > where ProcessForm returns true or false depending on the DB operation...
> >
> > of course there cannot be any output before the switch?
> >
> > did it help?
> >
> > At 18:24 25/11/01 +0100, Daniel Als?n wrote:
> > > > 2. User fills in a form, clicks "submit" which calls the same script,
> > > > passing itself the values. Depending on the value passed by the submit
> > > > button, the script processes the information (INSERT or
> > UPDATE) and sets
> > > > $done = 1 if successful.
> > > >
> > > > The second scenario is easier to handle.
> > > > Call the same script, passing it $done, and depending on
> > whether or not
> > > > $done is set you redirect.
> > > >
> > > > Juli Meloni has done an excellent tutorial on just this at
> > > > http://www.thickbook.com. look in the tutorials for something
> > like "Form
> > > > With Error Message". You just have to adapt the logic to suit
> > your needs.
> > >
> > >The second scenario is correct. I am actually already using the method in
> > >Melonis tutorial for error messages. But i can?t do a
> > redirection that way
> > >since $done isn?t set until after the db INSERT. My if-statement for the
> > >header is at the top of the page (wich is the only place i can
> > put it) and
> > >will never know if $done is set or not below.
> > >
> > >- Daniel
> > >
> > >
> > >--
> > >PHP General Mailing List (http://www.php.net/)
> > >To unsubscribe, e-mail: [EMAIL PROTECTED]
> > >For additional commands, e-mail: [EMAIL PROTECTED]
> > >To contact the list administrators, e-mail: [EMAIL PROTECTED]
> >
> >
> > _____________________________
> > . Christian Dechery
> > . . Gaita-L Owner / Web Developer
> > . . http://www.webstyle.com.br
> > . . http://www.tanamesa.com.br
> >
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
> 

-- 
Hank Marquardt <[EMAIL PROTECTED]>
http://web.yerpso.net
GPG Id: 2BB5E60C
Fingerprint: D807 61BC FD18 370A AC1D  3EDF 2BF9 8A2D 2BB5 E60C
*** Web Development: PHP, MySQL/PgSQL - Network Admin: Debian/FreeBSD
*** PHP Instructor - Intnl. Webmasters Assn./HTML Writers Guild 
*** Beginning PHP -- Starts January 7, 2002 
*** See http://www.hwg.org/services/classes

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to