Jump to content

Checking if a user has submitted a form


mark_aok

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/65626-checking-if-a-user-has-submitted-a-form/
Share on other sites

@ 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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.