Jump to content

Need help with code showing up in Form box


09sparky

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.