Doing this is very simple, although a pain in the rear...

For those of you that have any confusion on this issue, pay very close 
attention to this script.  It effectivly demonstrates how to deal with a 
form, and display intellegent errors WITHOUT any of that "...Please click 
your browsers back button..." crap.

Have a hidden field in your form - ACTION=SAVE
This will tell your script when to look for input.

Write your script like this
****************************************************
<?

//Make sure your ID is of int type.
//If ID > 0 then you are in UPDATE mode.  if ID == 0 then you are in INSERT 
MODE
$ID = intval($ID);

//The main reason that I use a switch instead of an IF is that you can 
break out at any time.
switch($ACTION) case 'SAVE':
{
         //Validate all fields here
         if(empty($field1)) $oErr->field1 = 'Required';
         if(empty($field2)) $oErr->field2 = 'Required';

         //If the variable $oErr is set, then break
         if(isset($oErr)) break;

         //We are okay, add slashes to all fields...
         $field1 = addslashes($field1);
         $field2 = addslashes($field2);

         if($ID)
         {       //UPDATE MODE
                 $sQuery = "UPDATE table SET field1='field1', 
field2='field2 WHERE id=$ID";

                 //Do the UPDATE query here (however you do that)
         }
         else
         {       //INSERT MODE
                 $sQuery = "INSERT INTO table VALUES ('field1', 'field2');

                 //Do the INSERT query here (however you do that)

                 //Grab the newly inserted ID
                 $ID = mysql_insery_id();
         }

         //We have success, Do the redirect here
         header("Location: http://www.yourdomain.com/nextscript.php?ID=$ID";);
         exit;
}

?>

<html>
    <body>
       <form name="frmInput" action="<?= $PHP_SELF ?>" method="post">
          <input type="hidden" name="ACTION" value="SAVE">
          <input type="hidden" name="ID" value="<?= $ID ?>">

          Field 1: <? if(isset($oErr->field1)) echo($oErr->field1); ?><br>
          <input type="text" name="field1" value="<?= addslashes($field1) ?>">
          Field 2: <? if(isset($oErr->field2)) echo($oErr->field2); ?><br>
          <input type="text" name="field2" value="<?= addslashes($field2) ?>">

       </form>
    </body>
</html>

******************************************************

At 06:24 PM 11/25/2001 +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]


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