ABor ABor - 2 months ago 16
HTML Question

PHP isset('submit') is always returning FALSE

I'm using an external php file in the "action" field of my HTML form so that the actions mentioned in the php file are undertaken on clicking the Submit button.

For this I've used the

isset()
function in the php file.

However, I'm finding that the
isset
function is always returning
FALSE
, resulting in the execution of the else statement (as seen in the console log).

If I remove the
isset()
function (and hence the
if-else
statements), then the code is working wonderfully.

Can you please check the problem in my code?

Also I've seen in other posts that I need to use some other argument along with
isset()
, for example,

if(isset($_POST['submit']) && !empty($_POST["xyz"]))


is this at all required?

P.S.: I'm still in the initial stage of the page development and hence I request you to please ignore the security concerns of my code, which I acknowledge that it exists. :)

My Sub-codes:

My HTML Form:

<form id="info-form" method="POST" action="form-submit.php">
<label for="Name">What is your Name? </label>
<input required type="text" name="name" placeholder="Enter your full name here." />

<label for="Email">What is your email ID? </label>
<input required type="email" name="email" placeholder="your.name@email.com" />

<label for="mobile">What is your 10-Digit Mobile Number? </label>
<input required type="text" name="mobile" maxlength="10" />

<button name="submit-form" type="submit" class="btn btn-lg btn-success"><i class="fa fa-paper-plane" aria-hidden="true"></i>
  Submit
    </button>
<button type="reset" class="btn btn-lg btn-warning"><i class="fa fa-undo" aria-hidden="true"></i>
Reset
</button>
</form>


My form-submit.php file:

<?php
if(isset($_POST['submit-form']))
{
require("database-connect.php");

$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];

$sql = "INSERT INTO tbl_details ".
"(name,email_id,mobile_number) ".
"VALUES ".
"('$name','$email','$mobile')";

mysql_select_db('db_info');
$return = mysql_query( $sql, $connect );

if(! $return )
{
die('Could not enter data: ' . mysql_error());
}

echo "Entered data successfully\n";
mysql_close($connect);
}

else
{
echo "Not Set\n";
}
?>

Answer

You really shouldn't be checking for the existence of a submit button value to begin with. As you see, some slight cosmetic changes to the frontend, like what kind of button is used to submit a form, shouldn't have any repercussions on the backend. The submit button and its value are pretty irrelevant to processing a form.

What you really want to check on the server is either if the request was a POST request, or whether the values which you want to work with are set, or both:

if ($_SERVER['REQUEST_METHOD'] == 'POST') …
if (isset($_POST['name'], $_POST['email'], $_POST['mobile'])) …

The most reasonable thing would be:

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    header('HTTP/1.0 405 Method Not Allowed');
    exit;
}

or:

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    header('Location: myform.html');
    exit;
}

After you've asserted that you're dealing with the right HTTP method, process your data:

$data = filter_input_array(INPUT_POST, [
    'name'   => FILTER_DEFAULT,
    'email'  => FILTER_VALIDATE_EMAIL,
    'mobile' => FILTER_DEFAULT
]);

As you see, you don't even need to interact with $_POST directly at all. See http://php.net/filter_input_array.

Comments