phppaper Posted December 15, 2010 Share Posted December 15, 2010 Hello, how do you write a set of code, when the code is only run when there is some post item pass from a html form. If no post item, other code will run. Thanks! Link to comment https://forums.phpfreaks.com/topic/221726-if-is-post/ Share on other sites More sharing options...
scotmcc Posted December 15, 2010 Share Posted December 15, 2010 <?php if ($_REQUEST['test']=='post') { echo "The field did contain the text 'post'."; } else { echo "The field did not contain the text 'post'."; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>form test</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> </head> <body> <form action="<?php echo $PHP_SELF;?>" method="POST"> <input type="text" name="test" id="test" /> <input type="submit" value="submit" /> </form> </body> </html> Try playing around with this... It checks to see if something was submitted (which it sounds like what you are asking). Additionally, if you are asking specifically if something was posted from a variable called post, change the php code to look something like this: <?php if ($_REQUEST['test']=='post') { echo "The field did contain the text 'post'."; } else { echo "The field did not contain the text 'post'."; } ?> Link to comment https://forums.phpfreaks.com/topic/221726-if-is-post/#findComment-1147551 Share on other sites More sharing options...
mikecampbell Posted December 15, 2010 Share Posted December 15, 2010 You could also check to see if the $_POST array is empty. Link to comment https://forums.phpfreaks.com/topic/221726-if-is-post/#findComment-1147553 Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 You should use something like this <?php // Returns TRUE if request is post otherwise FALSE function isPost() { return (strtolower($_SERVER['REQUEST_METHOD']) === "post") ? TRUE : FALSE; } // Now you can do: if(isPost()) { // Do this if it was post } else { // Do this when it isn't post } ?> Link to comment https://forums.phpfreaks.com/topic/221726-if-is-post/#findComment-1148016 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.