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
Detecting post versus first load
Moderator: General Moderators
Or something with a conditional loop
Code: Select all
<?php
if (isset($_POST['somevar']))
{
// process POST vars
}
?>
<form >
<input ..>
</form>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?Weirdan wrote:or simpleCode: Select all
if(count($_POST)) { //............ process it here }
This (as matthijs suggested) should be just fine:
Code: Select all
if (isset($_POST['somevar']))