mark_aok Posted August 18, 2007 Share Posted August 18, 2007 Hi all, I have a form that validates itself by putting "<?php $_SERVER['PHP_SELF'] ?>" as the form's action. I have now written some simple validation, shown below. $name = $_POST['name']; if (!isset($name) || empty($name)) { echo "Error, please fill in the name textbox"; } The problem is, this code shows up, even if the user has never submitted the form. How do I only show the error AFTER the user has pressed my submit button?? Thanks, Mark Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/ Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 Just do: if($name){ //success }else { //failure } Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/#findComment-327723 Share on other sites More sharing options...
BlueSkyIS Posted August 18, 2007 Share Posted August 18, 2007 "How do I only show the error AFTER the user has pressed my submit button??" check to see if the form was POSTed. I use this: if ($_SERVER['REQUEST_METHOD'] == "POST") { // The form was posted } Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/#findComment-327781 Share on other sites More sharing options...
mark_aok Posted August 20, 2007 Author Share Posted August 20, 2007 @ mgallforever - The code you suggested never returns true. @BlueSkyIS - $_SERVER['REQUEST_METHOD'] is always equal to post, because the form in the page is set to "post". Even if the user never submits it. But that code works for you? How? For me it still shows the error stuff... Thanks for the suggestions so far though, I'm really not sure why I'm finding this so difficult to do. Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/#findComment-328582 Share on other sites More sharing options...
bache Posted August 20, 2007 Share Posted August 20, 2007 put an id on the submit button, for example: <input type="submit" value="Click me" id="sbmtBtn"> and check: if (isset($_POST['sbmtBtn'])) { .... } Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/#findComment-328639 Share on other sites More sharing options...
chocopi Posted August 20, 2007 Share Posted August 20, 2007 You dont have to use isset and can just use if($_POST) Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/#findComment-328784 Share on other sites More sharing options...
AndyB Posted August 20, 2007 Share Posted August 20, 2007 put an id on the submit button, for example: <input type="submit" value="Click me" id="sbmtBtn"> and check: if (isset($_POST['sbmtBtn'])) { .... } Not quite. You need to NAME the input ... so: <?php if (isset($_POST['sbmtBtn'])) { $name = trim(strip_tags($_POST['name'])); if (!isset($name) || empty($name)) { echo "Error, please fill in the name textbox"; } // and more validation as you see fit for other inputs ... } ?> ... your form here <input type="submit" value="Click me" name="sbmtBtn"></form> Quote Link to comment https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/#findComment-328785 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.