index.php

// Даже пробовал добавлять суда $username=null; <? php include 'functions.php'; poligons($_POST["username"], "empty", "username:");?> 

functions.php

 <?php function poligons($post, $empty, $txt){ if(isset($post)){ $username="$txt <u>".$post."</u>"; echo $username;} else { $username=null; echo "$txt $empty<br />"; } }?> 

I started learning PHP, wrote input in html, and output windows of this input in php. And, to reduce the code I decided to write the output in a function and through it output the input. Error in Undefined index: username in index.php on line 47 .


All code: index.php

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div id="header"> <?php echo "<p>'Hello World!'</p>"; echo $_SERVER['HTTP_USER_AGENT']; $text_i="text"; $email="email"; $username="Name"; ?> <br><br><br> <form action="index.php" method="post"> <div class="main_input"> <div class="field"> <label for="username">Name:</label> <input type="text" name="username" placeholder="Username" /><br /> </div> <div class="field"> <label for="email">Email:</label> <input type="text" name="email" placeholder="Email" /><br /> </div> <div class="field"> <label for="text_i">Text:</label> <input id="text_i" type="text" name="text_i" placeholder="Your text" /><br /> </div> <input type="submit" name="submit" value="Enter" /> </div> </form> </div> <div class="poli"> <div class="poligon"> <?php echo "<h2>PHP variables</h2><hr>"; $firstString = "the First; "; $secondString = "the Second;"; $result = $firstString.$secondString; echo $result; ?> </div> <div class="poligon"> <?php $username=null; include 'functions.php'; poligons($_POST["username"], "empty", "username:"); // if(isset($_POST["username"])){ // $username="username: <u>".$_POST['username']."</u>"; // echo $username;} // else { // $username=null; // echo "username: empty<br />"; // } // if(isset($_POST["email"])){ // $email="<br>request?!WTF: <u>".$_POST['email']."</u>"; // echo $email;} // else { // $email=null; // echo "email: empty<br />"; // } ?> </div> <div class="poligon" id="text_i_opt"> <?php if (isset($_POST["text_i"])) { $text_i="Text: <br /><u>".$_POST['text_i']."</u>"; echo $text_i;} else { $text_i=null; echo "text_i: <br /><p align=center><tr vertical-align:50%>empty</p></tr>"; } ?> </div> </div> </body> </html> 
  • At the initial page loading, the $_POST array is empty and an attempt to access using any key will generate an error about the non-existence of it. - lolbas

1 answer 1

The fact is that the contents of the $_POST array appear only after the PHP script has received POST data, after sending them from the form. At the first access to the form, when the GET method is used, the contents of the $_POST array are not filled. To avoid making this remark, you better rework the handler's logic so that it is included only if data from the form is received

 include 'functions.php'; if(isset($_POST["username"])) { poligons($_POST["username"], "empty", "username:"); } 

In this case, the error is the following error

Error in Undefined index: username in index.php on line 47.

It will not be displayed.

  • array_key_exists('username', $_POST) n't it be more correct to check array_key_exists('username', $_POST) ? it is clear, cho in post value will not be NULL, but in other cases you can get caught - toxxxa
  • Thank! But why else is not displayed? - ALPHA
  • @ALPHA, should be displayed at all, look at the source code in the browser - is it not there? Perhaps the markup is so arranged that the value is hidden? - cheops
  • And if you can, please answer a couple of questions: what is => or -> (in the php code on different sites I saw how some variables point to others in this way - what is it?) And how to clear the values ​​in the post (so that when updating page it was empty)? - ALPHA
  • @toxxxa, as an option, but usually, if there is no need, they tend to make the code shorter (it's easier to understand and harder to make mistakes). For example, they often put such a check if (! Empty ($ _ POST)), assuming that if the POST-data came, then from a specific form. - cheops