Detecting post versus first load

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
justo_tx
Forum Newbie
Posts: 1
Joined: Fri Apr 07, 2006 5:03 pm

Detecting post versus first load

Post by justo_tx »

Hi all,
Hopefully this is the right section for this. I did a search for postback but couldn't find a response pertaining to my question.

I'm just starting with PHP (coming from an asp.net background). I created a simple form that inserts a record into a MySQL database. It works fine but when the page first loads it will insert a record of blank values (since the POST variables don't exist yet). How do I keep this from happening? I considered using a hidden input called "firstload" with a value of true, when the page is loaded the PHP script first checks an if statement to see if the firstload value is true, if not then it is parsed for the record insert. So I would just switch the value of "firstload" to false so the if statement returns false and the insert script is ran. I guess I should mention that the page is posting back to itself. However this solution seems like a cludge, and posting to different page shouldn't be necessary, or is it?

Thanks,
Justin
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_SERVER['REQUEST_METHOD'] is your friend.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Or something with a conditional loop

Code: Select all

<?php
if (isset($_POST['somevar'])) 
{
// process POST vars

}
?>
<form >
<input ..>
</form>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or simple

Code: Select all

if(count($_POST)) {
//............ process it here
}
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Weirdan wrote:or simple

Code: Select all

if(count($_POST)) {
//............ process it here
}
And what about if you have some other $_POST[''] vars? let's say another form on this page?
This (as matthijs suggested) should be just fine:

Code: Select all

if (isset($_POST['somevar']))
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Oren wrote: And what about if you have some other $_POST[''] vars? let's say another form on this page?
Thread title wrote: Detecting post versus first load
I don't see any "multiple forms" mentioned. Do you?
Post Reply