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.
Printable View
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.
That works for me also.PHP Code:if ($_POST)
{
// Post submited
}
But I usually do..PHP Code:if (empty($_POST) === false)
{
// Post submited
}
So yeah, look into empty and isset functions.PHP Code:if (isset($_POST['submit']))
{
// Post submited
}
Usually I use.
PHP Code:if (empty($var)) //Define your variable here
{
}
Use Iszaks or use
PHP Code:if( $_POST['variable'] != '' )
{
// posted
}
if($_POST) and if(isset($_POST['submit']) don't work for me... they just refresh the page
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:
That will check if they have posted the form. On the submit name just add 'form'.PHP Code:if(isset($_POST['form']))
{
}
This isn't the best method but it'll work.
Edit: Oh and add the method as POST.
Same problem. Page just refreshes.
For example:
same with issetPHP Code:<?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>
';
?>
Sounds like you're on a bad host, hearing all of the problems that you've had with PHP.
Try that. Does it refresh now. And what do you mean the page refreshes? Like when you load it it refreshes? Or what...?PHP Code:<?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>
");
?>
That is useless, horrible code.
Fine I will do it for you.
PHP Code:<?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;
}
?>
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?
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.
internet
talking about fagzone.