Jump to content

Help with error message causing form inputs to clear


thebigtuna

Recommended Posts

 

My problem is this: I built a simple form that connects to a sql database and inputs the information.  I made it so it puts the error message at the top of the page, when the user leaves a required field blank, password is too short and so on. The problem is that it clears all the fields when it displays these error messages. Is there a way to prevent this so it doesn't all have to be entered again. Am I just going about this the totally wrong way? I really haven't been working with php that long, about a week really, so excuse me if this is a total noob question.

 

Thanks a head of time.

 

 

The code:

 

 

 

 

 

 

<?php

 

 

  $errorMessage = '';

  if ($_POST['submit'] == "Submit")

  {

    //Determine if a Name was entered

   

$valid_form = true;

   

if ($_POST['username'] == "")

{

$errorMessage =  'Enter your name. ';

$valid_form = false;

}

 

else

{

$name = $_POST['usernamename'];

}

 

 

 

if ($_POST['password'] == "")

{

 

$errorMessage =  'Enter a password. ';

$valid_form = false;

 

}

 

elseif (strlen($_POST['password']) < 4)

{

 

$errorMessage =  'Password must contain at least 4 characters. ';

$valid_form = false;

}

 

else

{

$password = $_POST['password'];

}

 

if ($_POST['password2'] == "")

{

 

$errorMessage =  'Enter password a second time. ';

$valid_form = false;

 

}

 

elseif (strlen($_POST['password2']) < 4)

{

 

$errorMessage =  "Password must contain at least 4 characters. ";

$valid_form = false;

}

 

else

{

$password = $_POST['password2'];

}

 

 

//if all form fields were submitted properly, begin processing

 

if($valid_form == true)

{

 

 

$username=$_POST['username'];

$password=$_POST['password'];

$password2=$_POST['password2'];

$email=$_POST['email'];

$name=$_POST['name'];

$zip=$_POST['zip'];

$city=$_POST['city'];

$state=$POST['state'];

$country=$_POST['country'];

$adress=$_POST['adress'];

$phone=$_POST['phone'];

 

 

mysql_connect("localhost", "user", "pass") or die(mysql_error());

mysql_select_db(db) or die(mysql_error());

 

 

// checks if the username is in use

 

if (!get_magic_quotes_gpc()) {

$_POST['username'] = addslashes($_POST['username']);

}

$usercheck = $_POST['username'];

$check = mysql_query("SELECT username FROM test WHERE username = '$username'")

or die(mysql_error());

$check2 = mysql_num_rows($check);

 

//if the name exists it gives an error

 

if ($check2 != 0) {

$errorMessage = 'Sorry, the username '.$_POST['username'].' is already in use, please go back and create another user name. ';

}

 

//checks to make sure both passwords match

if ($_POST['password'] != $_POST['password2']) {

$errorMessage = 'Your passwords did not match. ';

}

 

 

 

mysql_query("INSERT INTO `test` VALUES ('$username', '$state','$county', '$adress', '$phone')") or die(mysql_error());

 

$to = $_POST['email'];

$subject = "Form Demo";

$headers = "Hello $name,

Username: $username

Password: $password

Zip Code: $zip

City:    $city

State:    $state

Country:  $country

Address:  $adress

Phone:    $phone

 

Thanks!";

$sent = mail($to, $subject, $headers);

if($sent)

header('Location: login.php');

else

$errorMessage =  'We encountered an error sending your mail. '; }

 

mysql_close();

 

}

  }

?>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml2/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 

<script type="text/javascript" src="js/emailcheck.js"></script>

  <title>Form Demo</title>

 

 

 

  </head>

 

  <body>

 

<?php

if ($errorMessage != '') {

?>

<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>

<?php

}

?>

<form id="demo" onsubmit="return validate_form(this);"  method="post">

 

 

<fieldset>

<legend>Account Information</legend>

<table width="100%" border="0">

<tr>

    <td>Username:</td>

 

    <td> <input type="text" name="username" size="32" /> * </td>

 

</tr>

<tr>

    <td>Password:</td>

 

    <td><input type="password" name="password" size="32" /> *</td>

</tr>

<tr>

    <td>Retype Password:</td>

    <td><input type="password" name="password2" size="32" /> *</td>

 

</tr>

<tr>

    <td>Your Email:</td>

    <td><input type="text" name="email" size="32" /> *</td>

 

</tr>

<tr>

    <td>Your Full Name:</td>

    <td><input type="text" name="name" size="32" /></td>

</tr>

 

<tr>

    <td>Your ZIP Code:</td>

    <td><input type="text" name="zip" size="5" /></td>

</tr>

<tr>

    <td>Your City:</td>

    <td><input type="text" name="city" size="32" /></td>

</tr>

<tr>

    <td>State/Province:</td>

    <td><input type="text" name="state" size="32" /></td>

</tr>

<tr>

    <td>Country:</td>

    <td><input type="text" name="country" size="32" /></td>

</tr>

<tr>

    <td>Address:</td>

    <td><input type="text" name="address" size="32" /></td>

</tr>

<tr>

    <td>Phone:</td>

    <td><input type="text" name="phone" size="32" /></td>

</tr>

 

<tr>

    <td><p class="bottom">Fields with a * next to them are required.</p></td>

    <td><input type="submit" value="Submit" name="submit" /></td>

</tr>

</table>

</fieldset>

 

</form>

 

 

  </body>

  </html>

 

Link to comment
Share on other sites

If you change:

 

<td> <input type="text" name="username" size="32" /> * </td>

 

to:

 

<td> <input type="text" name="username" size="32" value=" <?php echo $_POST['username'];?>" /> * </td>

 

and do like wise with the other inputs, then whatever was POSTed will apper in the 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.