ABor ABor - 1 year ago 123
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

function in the php file.

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

If I remove the
function (and hence the
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
, 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>
<button type="reset" class="btn btn-lg btn-warning"><i class="fa fa-undo" aria-hidden="true"></i>

My form-submit.php file:


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

$sql = "INSERT INTO tbl_details ".
"(name,email_id,mobile_number) ".

$return = mysql_query( $sql, $connect );

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

echo "Entered data successfully\n";

echo "Not Set\n";

Answer Source

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 (isset($_POST['name'], $_POST['email'], $_POST['mobile'])) …

The most reasonable thing would be:

    header('HTTP/1.0 405 Method Not Allowed');


    header('Location: myform.html');

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download