Jump to content

Echo with forms


ngreenwood6

Recommended Posts

I have a question about forms and php playing nice together. I am trying to do some error handling. If a user does not put in their username for it to say "please enter your username" next to the text box. I have been able to accomplish this in two ways. By submitting the form to itself and making an exact duplicate of the original page with the error next to the text box and if the user does not enter a username redirecting them to that page. I am thinking that there is an easier way to do this. I have to submit a form from one page ("index.php") to another page ("submit.php"). Does anyone know how I can accomplish this? If you have any questions feel free to ask.

 

Thanks

Link to comment
Share on other sites

I would rather stick with php to validate my forms because I will be using variables from mysql as well. I do not know a whole lot about javascript.

 

index.php

<form method="post" action="submit.php">
<table>
<tr>
<td>Username:</td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit"></td>
</tr>
</table>
</form>

 

submit.php

<?php
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username))
{
echo "You must enter a username";
}
elseif (empty($password))
{
echo "You must enter a password";
}
else
{
echo "You are logged in";
}
?>

 

When I submit this without entering a username or password it takes me to a blank page where it gives me the errors. I want it to just stay on the main page and give me the errors next to the boxes where you enter the information. I can add the following page and edit the submit page as follows:

 

username.php

<form method="post" action="submit.php">
<table>
<tr>
<td>Username:</td>
<td><input name="username" type="text"></td> You must enter a username!
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit"></td>
</tr>
</table>
</form>

 

submit.php

<?php
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username))
{
echo "You must enter a username";
}
elseif (empty($password))
{
include("username.php");
}
else
{
echo "You are logged in";
}
?>

 

When I do that it will show the error message next to the box. But if I do that with every type of error that would take me forever and there would be alot of pages. I know there has to be a better way. Please someone help.

Link to comment
Share on other sites

One way i do it

<?php
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username))
{
echo "You must enter a username";
}
elseif (empty($password))
{
echo "You must enter a password";
}
else
{
echo "You are logged in";
}
}
?>

<form method="post" action="">
<table>
<tr>
<td>Username:</td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit"></td>
</tr>
</table>
</form>

I think this will work

Link to comment
Share on other sites

one way i do it is to gather the errors in an array, and then display them using a foreach construct.  it'll give ya something to work with maybe.

<?php
if ($_POST['username']=='' || strlen($_POST['username'])==FALSE){
$errors[] = 'Please enter a username.';
}
if ($_POST['password']=='' || strlen($_POST['password'])==FALSE){
$errors[] = 'Please enter a password.';
}
//display errors if set;
if(is_array($errors)){
        echo "<form action='yourfile.php' method='POST'>";
        echo '<b>The following errors occured.</b><br />';
        while (list($key,$value) = each($errors)){
              echo $value;
        }
        echo "</form>";
}else{
        // if no errors, do something;
}

Link to comment
Share on other sites

JavaScript is a possibility but not the best one. Remember, people can disable javascript. It should not be the only form of error checking.

 

In fact, if you merged what mrMarcus and Blade280891 have, you will get what you want.

 

<?php
//Start the validation.
//Remember, the form posts to this page again!

if(isset($_POST['submit'])){
//The form has been submitted

$errors = array(); //make a new array called errors.

//now lets get all the post values
$username = $_POST['username'];
$password = $_POST['password'];

//First check if username has a value.
if($username == ""){
//They didn't enter a username. Add an error!
$errors[] = "You did not enter a username!"; //The $errors[] is making a new element in the array.
}

//Do the same for password
if($password == ""){
$errors[] = "You did not enter a password!";
}

//Now, the next line checks to see if there were any errors.
if(!empty($errors)){ //This is checking to see if $errors is NOT empty. So if we encountered an error, we will show them!

//We have some errors. Let's show them!
echo "Sorry but we found some errors!<ol>";

foreach($errors as $error){ //this foreach loop takes $errors and turns each element into the variable $error
echo "<li>$error</li>";
}

echo "</ol>";

//So now the errors are shown in an ordered list.
//Continue with the script, and show the form again!

}else{
//No error encountered. Log the user in then redirect!
$_SESSION['bleh'] = $username;

header("Location: control.php");
exit;
}
}

//Even if we got here and the form was submitted but it had errors. The errors will be shown above the form!
?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table>
<tr>
<td>Username:</td>
<td><input name="username" type="text"></td> You must enter a username!
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit"></td>
</tr>
</table>
</form>

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.