1

Am using the code below in order to print the values that am inserting on the boxes. However After I put the values and click submit nothing is printing out.

<html>
<body>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
 Name: <input type="text" name="name"><br>
 E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

<?php
if(isset($_POST["submit"])) {
$name = filter_input(INPUT_POST, 'name');
$email = $_POST['email'];
echo $name;
echo $email;
}

?>
</body>
</html>

About below I tried both cases because on the simple one $_POST['email'] I was getting a warning:

$name = filter_input(INPUT_POST, 'name');
$email = $_POST['email'];

Warning:

Do not Access Superglobal $_POST Array Directly

I am using Netbeans IDE 8.2.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Christos Michael
  • 1,003
  • 2
  • 10
  • 16
  • LoL. `Do not Access Superglobal $_POST Array Directly` - This is given by the stupid IDE. Ignore it please. – Praveen Kumar Purushothaman Nov 27 '16 at 16:55
  • NetBeans IDE is not something that stops you working from using PHP. And using `$_POST` or any other super-globals is never a bad idea. – Praveen Kumar Purushothaman Nov 27 '16 at 16:56
  • @Praveen Kumar so there is no security issue using this type of command? – Christos Michael Nov 27 '16 at 17:06
  • @Fred-ii- Although I agree this is related, but man, this is a bad dupe target! `:(` – Praveen Kumar Purushothaman Nov 27 '16 at 17:11
  • @PraveenKumar why bad? The posted code is clear; what am I missing here? I've seen this countless times and closed countless times without any conflict of interest. – Funk Forty Niner Nov 27 '16 at 17:12
  • @Fred-ii- Eh, if you say so. `:D` But the comments are great. Firing a lot of things. – Praveen Kumar Purushothaman Nov 27 '16 at 17:13
  • `if(isset($_POST["submit"])) {...}` that will never happen and error reporting would have clearly thrown an undefined submit index notice. If people are then chased down a deep rabbit hole, is beyond the scope of the question. – Funk Forty Niner Nov 27 '16 at 17:14
  • @Fred-ii- Appreciate your austere(strict) style. I know that stack overflow has to deal with more difficult things and more discussable topics. However I think Stack Overflow has proffessionals who can reply and give an excellent explanation to a beginner. Related issue would never give me a clear solution in my mind. Have a nice day – Christos Michael Nov 27 '16 at 17:16
  • @ChristosMichael Stack's ways of dealing with obvious code is clear. "They" implemented those voting closures for a reason. This type of question has been asked more times than you could count each popcorn you eat and by many more bowlfulls than you know, which is why the question was closed based on that; "asked too many times". If we were to continually allow answers that solve it to be posted, the Q&A's would just keep piling up too much. In having this type of system, helps to lower that amount. – Funk Forty Niner Nov 27 '16 at 17:21
  • 1
    @ChristosMichael Just a final note: Using error reporting (something that nobody addressed *(till now)* ) is crucial during testing/development. Follow this link on php.net http://php.net/manual/en/function.error-reporting.php and apply that to your code. Yes, I agree that when someone gives a good explanation as to why code fails is important, however the duplicate (in its own right) also does that. *Quick tip:* When using a conditional `if{...}`, use an accompanied `else{...}`; which yours would have fallen into. – Funk Forty Niner Nov 27 '16 at 17:30
  • @Fred-ii- thanks for you info and time – Christos Michael Nov 30 '16 at 07:49
  • @ChristosMichael You're welcome. – Funk Forty Niner Nov 30 '16 at 15:10

2 Answers2

1

This is never set:

if (isset($_POST["submit"])) {

Instead change it to:

if (count($_POST) > 0) {

You don't have any elements matching name="submit". Also, it is a bad practise to use isset($_POST["submit"]) as many of us, won't name it.

If you want to check for specific things set, like in your case, you need to do:

if (count($_POST) > 0 && isset($_POST["name"]) && isset($_POST['email'])) {

If still you wanna make your code work with the above set-up, kindly add a name and value here:

<input type="submit" name="submit" value="Submit" />
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Thanks for your answer. Is there a way to print out the values without an if case? – Christos Michael Nov 27 '16 at 16:59
  • @ChristosMichael I have explained that as well. You just need to make sure that they are set before printing right? Else it will throw a Reference Error or Undefined Index error. It's always better to have a check before you print it out. Also, you can just echo using `echo @$_POST["name"]` so that you don't get the error. But please, don't use it often. – Praveen Kumar Purushothaman Nov 27 '16 at 17:02
  • why not simply use `if ($_SERVER['REQUEST_METHOD'] == "POST") {` ? –  Nov 27 '16 at 17:04
  • @Hallur What if those values aren't passed? – Praveen Kumar Purushothaman Nov 27 '16 at 17:04
  • why are they not passed??? if post is set, then the server request method is always post.. Also sorry, I meant instead of using count() –  Nov 27 '16 at 17:06
  • @Hallur What if, you are using a `checkbox`, if you aren't selecting it, it never gets passed. – Praveen Kumar Purushothaman Nov 27 '16 at 17:07
  • if you send post data to the server, then the server will always detect that the server request method is post.. no matter what element is being submitted. if post is set, then request method = post. http://stackoverflow.com/questions/409351/post-vs-serverrequest-method-post –  Nov 27 '16 at 17:09
  • 1
    Maybe you aren't understanding what I'm trying to tell you. Don't count post, to check if post is set... doesn't make sense. specifcally referring to this part of your answer `count($_POST) > 0` –  Nov 27 '16 at 17:11
  • @Hallur Agreed with that part. Fine. I am saying, to check individual elements being set, I use the `isset`. That's fine right? `:)` – Praveen Kumar Purushothaman Nov 27 '16 at 17:12
  • 1
    @PraveenKumar Correct. –  Nov 27 '16 at 17:12
  • @PraveenKumar So in order to use isset() I should but on the a name and a value? I am just talking in general not only for this case – Christos Michael Nov 28 '16 at 10:58
  • @ChristosMichael Better to check the request method, isset on the required inputs. – Praveen Kumar Purushothaman Nov 28 '16 at 11:47
0

There is no HTML element with the attribute name 'submit' exists.

Replace <input type="submit"> with <input type="submit" name="submit">.

Don't ever use isset($_POST['submit']) as it is considered to be a bad practice.

Instead you can use !empty($_POST). Doing it this way is considered to be safe.

Wolverine
  • 1,712
  • 1
  • 15
  • 18