Page 1 of 1

Detecting post versus first load

Posted: Fri Apr 07, 2006 5:12 pm
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

Posted: Fri Apr 07, 2006 5:24 pm
by feyd
$_SERVER['REQUEST_METHOD'] is your friend.

Posted: Sat Apr 08, 2006 1:03 am
by matthijs
Or something with a conditional loop

Code: Select all

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

}
?>
<form >
<input ..>
</form>

Posted: Mon Apr 10, 2006 3:46 am
by Weirdan
or simple

Code: Select all

if(count($_POST)) {
//............ process it here
}

Posted: Mon Apr 10, 2006 4:22 am
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']))

Posted: Mon Apr 10, 2006 4:29 am
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?