Checking form in PHP_SELF, then including the upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Checking form in PHP_SELF, then including the upload

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

:)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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]""; ?>>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

If the post vars arent set then your code will give array index undefined errors for $_POST[test]
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I could always use the @ symbol, yes?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

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