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>
$_POSTarray is empty and an attempt to access using any key will generate an error about the non-existence of it. - lolbas