Using If(Isset($_Post['Submit'])) to Not Display Echo When Script Is Open Is Not Working

Using if(isset($_POST['submit'])) to not display echo when script is open is not working

You need to give your submit <input> a name or it won't be available using $_POST['submit']:

<p><input type="submit" value="Submit" name="submit" /></p>

Submit button not hitting isset() call

This if (isset($submitted))... should be if (isset($_POST['submitted']))...

if(isset($_POST['submit'])) doesn't work

There are several possible reasons why posted data does not arrive at your PHP page.

One could be that the .htaccess file has rules that are responsible for this effect. But after the contents were published in the question, it became clear that this was not the case.

Another could be that the script sends a redirection to (eventually) the same page, thereby losing the posted data. Once you mentioned in comments that you noticed that the browser reloaded the page right after submission, it became clear that this was the candidate cause to look for.

Redirection is typically done with this kind of code:

header("location: some-URL");

... and certainly when the argument contains a self-reference, like this:

header('Location: '.$_SERVER['PHP_SELF']);

The code you posted did not contain such statement, at least not one that explains the behaviour, but there are some included scripts:

  • ../../database/db_connect.php
  • ../../chromephp-master/ChromePhp.php

and also some dots ..., where we don't know what happens (includes could be there as well).

If any of these parts performs such a redirection, it could be at the cause of the problem. We can ignore ChromePhp which is a publicly available library and does not do such things.

So, at least db_connect.php but also all other included files (either directly or via other included files), should be scanned for code that redirects.

If you find such redirection, check if it is reasonable to keep that code -- as not all redirection is bad. But certainly self-redirections should be analysed: are they really necessary, knowing that any posted data will thereby be ignored?

So replace the causing header() statement(s) by more appropriate code and your problem should be solved.

As a side note: it is better not to put HTML entities inside a value attribute of an HTML element, like you have here:

 <input type="submit" name="submit" value=" Log in " />

If you need the spacing, just enter plain spaces:

 <input type="submit" name="submit" value=" Log in " />

form data will not go into my database no errors are displayed

If not specified the method of <form> is GET so if (isset($_POST['submit'])) is false.

You should add method POST to your form tag

<form method="POST" action="index.php">

PHP isset('submit') is always returning FALSE

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.



Related Topics



Leave a reply



Submit