PDA

View Full Version : [PHP] Proper Way To Check For POST?



Trigs
28-02-2009, 12:02 AM
What's the proper way to check to see if POST data was submitted?

if($_POST) doesnt work and if($_POST['submit']) doesnt work if I had a submit button named submit.

Iszak
28-02-2009, 02:04 AM
if ($_POST)
{
// Post submited
}
That works for me also.


if (empty($_POST) === false)
{
// Post submited
}
But I usually do..


if (isset($_POST['submit']))
{
// Post submited
}
So yeah, look into empty and isset functions.

Excellent2
28-02-2009, 11:13 AM
Usually I use.


if (empty($var)) //Define your variable here
{

}

Blob
28-02-2009, 11:44 AM
Use Iszaks or use



if( $_POST['variable'] != '' )
{
// posted
}

Trigs
28-02-2009, 09:43 PM
if($_POST) and if(isset($_POST['submit']) don't work for me... they just refresh the page

Excellent2
28-02-2009, 10:02 PM
if($_POST) and if(isset($_POST['submit']) don't work for me... they just refresh the pageif(!$_POST)

Trigs
28-02-2009, 11:16 PM
isn't

if($_POST) {
// process info
} else {
// error msg
}

and

if(!$_POST) {
// error
} else {
// process data
}

the same?

Excellent2
28-02-2009, 11:32 PM
isn't

if($_POST) {
// process info
} else {
// error msg
}

and

if(!$_POST) {
// error
} else {
// process data
}

the same?Yeah, just in reverse order. But what I was saying is if you're wanting to check if data is empty, use either the empty function or !$_POST['lol']. You need to make sure they're submitting the form by doing something like:


if(isset($_POST['form']))
{

}That will check if they have posted the form. On the submit name just add 'form'.

This isn't the best method but it'll work.

Edit: Oh and add the method as POST.

Trigs
28-02-2009, 11:47 PM
Same problem. Page just refreshes.

For example:


<?php
if(!$_POST) {
echo "hi $_POST['name']";
} else {
echo '
<form method="post" enctype="text/plain">
Name: <input type="text" name="name"> <br />
<input type="submit" value="Submit" name="submit">
</form>
';
?>

same with isset

DeejayMachoo$
01-03-2009, 09:04 AM
Same problem. Page just refreshes.

For example:


<?php
if(!$_POST) {
echo "hi $_POST['name']";
} else {
echo '
<form method="post" enctype="text/plain">
Name: <input type="text" name="name"> <br />
<input type="submit" value="Submit" name="submit">
</form>
';
?>same with isset



<?php
if($_POST) {
echo "hi $_POST['name']";
} else {
echo '
<form method="post" enctype="text/plain">
Name: <input type="text" name="name"> <br />
<input type="submit" value="Submit" name="submit">
</form>
';
?>

Calgon
01-03-2009, 09:30 AM
Sounds like you're on a bad host, hearing all of the problems that you've had with PHP.

Blob
01-03-2009, 01:00 PM
Sounds like you're on a bad host, hearing all of the problems that you've had with PHP.

Or he just can't code PHP...

Trigs
01-03-2009, 06:22 PM
<?php
if($_POST) {
echo "hi $_POST['name']";
} else {
echo '
<form method="post" enctype="text/plain">
Name: <input type="text" name="name"> <br />
<input type="submit" value="Submit" name="submit">
</form>
';
?>


oh yer that ! was a typo. that still doesn't work.

@blob: if you're so good why don't you help me by telling me what I did wrong instead of insulting me?

Agnostic Bear
01-03-2009, 06:24 PM
oh yer that ! was a typo. that still doesn't work.

@blob: if you're so good why don't you help me by telling me what I did wrong instead of insulting me?

He wasn't insulting you, he was just making a point.

Also use this instead of !$_POST:

empty( $_POST[ 'name' ] ) === false

also get rid of that enctype there is no need for it

Decoma
02-03-2009, 08:17 PM
<?php
if(isset($_POST['name'])){
echo("hi ".$_POST['name']."");
} else {
echo ("
<form method=\"post\" enctype=\"text/plain\" action=\"$_SERVER['PHP_SELF']\">
Name: <input type=\"text\" name=\"name\"> <br />
<input type=\"submit\" value=\"Submit\" name=\"submit\">
</form>
");
?>

Try that. Does it refresh now. And what do you mean the page refreshes? Like when you load it it refreshes? Or what...?

Agnostic Bear
03-03-2009, 11:05 AM
<?php
if(isset($_POST['name'])){
echo("hi ".$_POST['name']."");
} else {
echo ("
<form method=\"post\" enctype=\"text/plain\" action=\"$_SERVER['PHP_SELF']\">
Name: <input type=\"text\" name=\"name\"> <br />
<input type=\"submit\" value=\"Submit\" name=\"submit\">
</form>
");
?>Try that. Does it refresh now. And what do you mean the page refreshes? Like when you load it it refreshes? Or what...?

That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>

Trigs
03-03-2009, 10:15 PM
I have been trying to figure this out but I can't...

Why on earth does pressing the Submit button just clear the boxes/refresh the page? Could it be that I messed up the action="" value?

Invent
04-03-2009, 12:35 AM
I have been trying to figure this out but I can't...

Why on earth does pressing the Submit button just clear the boxes/refresh the page? Could it be that I messed up the action="" value?

You do have method="post" in the <form> tag don't you?

Trigs
04-03-2009, 04:40 AM
No, I thought you didn't need it if it's on the same page. I've also tried echo '<form action="'.$HTTP_SERVER_VARS['PHP_SELF'].'"; and so on but same problem.

Agnostic Bear
04-03-2009, 05:35 AM
No, I thought you didn't need it if it's on the same page. I've also tried echo '<form action="'.$HTTP_SERVER_VARS['PHP_SELF'].'"; and so on but same problem.


That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>



That is useless, horrible code.

Fine I will do it for you.



<?php
if( empty( $_POST[ 'name' ] ) === false ) {
echo 'Hi, ' . $_POST[ 'name' ];
} else {
echo <<<HTML
<form action="" method="post">
Name: <input type="text" name="name" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
HTML;
}
?>


internet

Blinger1
04-03-2009, 06:24 AM
internet
this guy is an idiot seriously.

Agnostic Bear
04-03-2009, 06:27 AM
this guy is an idiot seriously.

are we talking about me or him here, cause i can't even tell

Blinger1
04-03-2009, 06:29 AM
talking about fagzone.

Invent
04-03-2009, 09:23 AM
No, I thought you didn't need it if it's on the same page. I've also tried echo '<form action="'.$HTTP_SERVER_VARS['PHP_SELF'].'"; and so on but same problem.

What? Of course you need the method attribute o_o.

Want to hide these adverts? Register an account for free!