09sparky Posted August 23, 2012 Share Posted August 23, 2012 Hello, I am very new to php but am setting up a simple site for a basic login (username/password). I would like to capture the credentials to a file, etc. Additionally, I want to echo back on the screen if the user has forgotten to type in either the username and/or password. I have everything working (see below), but unfortunately, in my username form box (when the page loads each time) I get <?=$varUser;?> and in the password field I get <?=$varPass;?>. Can someone tell me what I am doing wrong and help me resolve this without having my code showing in the form field. .php file: <?php if($_POST['formSubmit'] == "Submit") { $errorMessage = ""; if(empty($_POST['formUser'])) { $errorMessage .= "<li>You forgot to enter a Username!</li>"; } if(empty($_POST['formPass'])) { $errorMessage .= "<li>You forgot to enter a Password!</li>"; } $varUser = $_POST['formUser']; $varPass = $_POST['formPass']; if(empty($errorMessage)) { $fs = fopen("/mydata.csv","a"); fwrite($fs,$varUser . ", " . $varPass . "\n"); fclose($fs); header("Location: thankyou.html"); exit; } } ?> <title>Title</title> </head> <body> { echo("<p>There was an error with your form:</p>\n"); echo("<ul>" . $errorMessage . "</ul>\n"); } ?> <form action="myform1.php" method="post"><font face="arial" size="-1"> </font> <p><font face="arial" size="-1"> Please Enter Your Username?<br> <input maxlength="50" name="formUser" value="<?=$varUser;?>"> <font face="arial" size="-1"> </font> </font></p> <p><font face="arial" size="-1"><font face="arial" size="-1">Please Enter Your Password?<br> <input name="formPass" maxlength="50" value="<?=$varPass;?>" type="password"> <font face="arial" size="-1"> <font face="arial" size="-1"> <input name="formSubmit" value="Submit" type="submit"> </font> </font> </font> </font></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/267456-need-help-with-code-showing-up-in-form-box/ Share on other sites More sharing options...
ManiacDan Posted August 23, 2012 Share Posted August 23, 2012 Welcome to the forum. Please use [ PHP ] and [ /PHP ] tags (no spaces) to wrap your code so it's easier for us to read. The <?=$var?> syntax is deprecated and no longer works in PHP. Whatever you're learning from is at least 10 years out of date and should be thrown away. You need to replace that with <?php echo $var; ?> You should also know that this code will throw a warning under normal operation, and if you're developing without all your errors turned on you'll never learn properly. At the top of your script, put: error_reporting(E_ALL); Then, the correct code for inside your box is: <?php echo (isset($var) ? $var : ""); ?> Where $var is whichever variable you were trying to use the short syntax for. Quote Link to comment https://forums.phpfreaks.com/topic/267456-need-help-with-code-showing-up-in-form-box/#findComment-1371786 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.