Jump to content

echoing form errors


squigs

Recommended Posts

Hello,

I'm having difficulty learning the most efficient method of echoing error messages on a form. The particular example I'm working on is a registration form which I have had functional but not perfected as of yet.

 

I have been able to make the form work by using die() to kill the process and display a message however I would very much like for it to kill the process and simply echo an error message, same page, no redirects...

 

I will post my code and hopefully someone with more knowledge than me can shed some light.

 

Cheers!

 

<?php

if (isset($_POST['submit'])) { 

//This is one error message I would like to display
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name already exists it gives an error here
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}
// this makes sure both passwords entered match and should display an error if false
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}

$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}

$insert = "INSERT INTO users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
?>

And here is the form it fits into

  <?php }  else {	 ?> 
<body>
<div id="container">
<div id="header"><?php include ("login_header.php") ?></div>
<div id="photoNav"><?php include ("mainNav.php") ?></div>
<div id="tableContent">
<div class="bold_16" style="margin-top:40px">Enter Your Registration Information Below</div>
<div class="padding_top"><div style="padding-top:10px; text-align:center;">
***THIS IS WHERE I WOULD LIKE MY ERRORS TO DISPLAY***
</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0" align="center"> <tr><td>Username:</td><td> <input name="username" type="text" size="23" maxlength="40"> </td></tr> <tr><td>Password:</td><td> <input name="pass" type="password" size="24" maxlength="12"> </td></tr> <tr><td>Confirm Password:</td><td> <input name="pass2" type="password" size="24" maxlength="12"> </td></tr> <tr><th colspan=2 style="text-align:right;"><input type="submit" name="submit" value="Register" style="margin-top:10px;"></th></tr> </table> </form> <?php } ?> 
</div></div>

p.s is it just me or is it a real pain getting a message into this text-field??

Link to comment
Share on other sites

I can't go through your code right now.  But I do something similar to what you're asking for with my contact page.  It loads the contact page and posts the form to the same php script.  If the user submitted the form, the variable  $_POST['submit'] will be set.

 

If the form HAS been set, then the script checks each field.  If there's an error in one of these fields, it appends a message about the error to the variable named $errorMsg.

 

Then if the form has been submitted OR the $errorMsg variable is not blank, then it shows the form again.  And if the $errorMsg is not blank, it displays it.  If the form HAS been submitted and the $errorMsg variable is blank, it sends the message to me and thanks the user for the message.

 

<?php
if (is_set($_POST['submit'])){
  	// Check for a first name.
if (eregi ("^[[:alpha:].' -]{2,50}$", stripslashes(trim($_POST['first_name'])))) {
	$fn = escape_data($_POST['first_name']);
} else {
	$fn = FALSE;
	$validForm = false;
	$errorMsg .= '<p><font color="red" size="+1">Please enter your name.</font></p>';
}


$email = true;
// Check for an email address.
if(!empty($_POST['email'])){
	if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", stripslashes(trim($_POST['email'])))){
		$e = escape_data($_POST['email']);
	} else {
		$validForm = false;
		$errorMsg .= '<p><font color="red" size="+1">Please enter a valid email address.</font></p>';
	}
}else{
	$email = false;
}


// Check for a subject.
if (!empty($_POST['subject'])) {
	$u = escape_data($_POST['subject']);
} else {
	$u = FALSE;
	$validForm = false;
	$errorMsg .= '<p><font color="red" size="+1">Please enter a valid subject.</font></p>';
}
  
}
?>

<?php if(!is_set($_POST['submit']) || $errorMsg <> ""){?>
<html>
  <title>Contact Page</title>
  <body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
  <?php if($errorMsg<>""){echo "<tr><td colspan=2>" . $errorMsg . "</td></tr>";?>
  <tr>
<td align="right"><b>Name*:</b></td><td><input type="text" name="first_name" size="50" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name'];?>" ></td>
  </tr>
  <tr>
<td align="right"><b>Email Address*:</b> </td><td><input type="text" name="email" size="50" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" > </td>
  </tr>
  <tr>
    <td align="right"><b>Subject*:<b></td><td><input type="text" name="subject" MAXLENGTH="50" SIZE="50" value="<?php

     if (isset($_POST['subject'])){
       echo $_POST['subject'];
     }elseif(isset($_GET['subject'])){
       echo $_GET['subject'];
     }
     ?>"></td>
  </tr>
  <tr>
    <td align="right" valign="top"><b>Comments:</b></td><td><TEXTAREA name="msgBody" ROWS=10 COLS=40><?php if (isset($_POST['msgBody'])) echo stripslashes($_POST['msgBody']); ?></TEXTAREA></td>
  </tr>
  <tr>
<td colspan="2"><div align="center"><input type="submit" name="submit" value="Submit">  <input type="reset" name="reset" value="Start Over" /></div></td>
  </tr>
</table>


</form><!-- End of Form -->

  </body>
</html>

<?php }else{
    //I have php code that sends the email message to me.  That goes here but I've left it  out because it doesn't pertain to this request.  After sending the message, it displays the thank you page based on the following.
?>
<html>
  <title>Thank you</title>
  <body>
    Thank you for taking the time to contact me.  I'll be in touch with you soon.  
  </body>
</html>

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.