Page 1 of 1

Checking form in PHP_SELF, then including the upload

Posted: Fri Apr 02, 2004 9:31 am
by Steveo31
Sooooo... yea. 2 problems. One, when I use $_SERVER['PHP_SELF'] for the form action, and it is submitted, the fields go blank. I would like them to stay put, so if there is an error, then the user can fix it without having to fill it ALL back in.

Two, if all the fields are correctly filled out, I would like to have a page that has all the mySQL inserting. I thought I could just use include() and all the variables would be magically passed to it, but no. What is a possibility of doing this?

:)

Posted: Fri Apr 02, 2004 9:46 am
by kettle_drum
ok here goes:

Code: Select all

if(!$_POST[submit]){ //the form hasnt been submitted - so show it
      //put your form here and you MUST call your submit button "submit" and set it to a value of 1, or create a hidden field called submit.

      //example of what the fields should look like
      echo "<input type="text" name="test" value="$_POST[test]">";

      //this will then place the value back in the field if we have to come back to the form
   }else{
      //the form has been submitted so do some checks on the data
      if($all_the_data_is_good){
         //...mysql_query(); stuff
      }else{
         //create another form - submit to the samepage, but make "submit" = 0 so that it will show the original form again. Then place all data you got from the form into hidden fields so its passed back to the original form. Display some error message.
      }
   }
Hope that makes sense. So when its run you get the form shown as $_POST[submit] is false. You then post the form and $_POST[submit] is true so it runs the next part.

If all the data is good - filled in, not fake emails etc - then you can submit the data to the database. If there is a problem, then you show an error message and a submit button - with a lot of hidden fields with the rest of the data in - then that sets $_POST[submit] to false - so the form is shown, but since there are $_POST[] values, these are put as the default value in the form.

Posted: Fri Apr 02, 2004 9:52 am
by magicrobotmonkey
yea but on first opening through he'll get errors because the $_POST vars aren't set - instead do

Code: Select all

<input type="text" name="test" <?if(isset($_POST[test])) echo "value="$_POST[test]""; ?>>

Posted: Fri Apr 02, 2004 9:59 am
by kettle_drum
Well you shouldnt get errors - at least not serious errors, but its always good pratice, you will also have to do something similar if your form has check boxs, dropdown menus etc, as if theres not a value, thena default needs to be set.

Posted: Fri Apr 02, 2004 10:02 am
by magicrobotmonkey
If the post vars arent set then your code will give array index undefined errors for $_POST[test]

Posted: Fri Apr 02, 2004 3:12 pm
by Steveo31
I could always use the @ symbol, yes?

Posted: Mon Apr 05, 2004 3:03 am
by twigletmac
Steveo31 wrote:I could always use the @ symbol, yes?
Not the best solution, better to use empty() or isset() to check the variables. @ should be used to suppress errors that you cannot prevent - e.g. the database server goes down and you don't want to show that kind of information to users.

Mac