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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.