Forum Moderators: coopster

Message Too Old, No Replies

Help positioning an echo or die where I want it?

         

Jamier101

3:23 pm on Aug 13, 2011 (gmt 0)

10+ Year Member



This might sound a like a silly question but how do I control where a PHP form prints and echo or die statement?

I currently have a block of code which checks for the existence of a username in my database, everything is working fine but I would like to error ""Username already exists to print below the submit button when there is an error instead of on a new page. I might be over thinking things but I can't seem to figure it out.

<?php
$submit = $_POST['submit'];

//form data
$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date("Y-m-d");

if ($submit)

{
// Connect to the database
$connect = mysql_connect("localhost","root","") or die ("Couldn't connect");
mysql_select_db("rentals") or die ("Couldn't find database");

$namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
$count = mysql_num_rows($namecheck);

if ($count!=0)

{
die("Username already exists!"); // This wants to be displayed further down
}


// Check for existance
if ($fullname&&$username&&$password&&$repeatpassword)
{

if ($password==$repeatpassword)

{
if (strlen($fullname)>25 || strlen($username)>25)
{
echo "The maximum length of fullname / username is 25 characters.";
}

else

{
// Check password length
if (strlen($password)>25 || strlen($password)<6)
{
echo "Password must be between 6 and 25 characters long";
}
else
{
// Register the user

// Encrypt password
$password = md5($password);
$repeatpassword = md5($repeatpassword);

$queryreg = mysql_query("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')");

header('location: registration_success.php');

}

}

}
else
echo "Your passwords do not match!";

}

else

echo "Please fill in all fields.";

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title></title>
</head>

<body>
<div id="wrapper">
<div id="container">
<div id="header">
<div id="user_box">
</div>
</div>
<div id="navigation">
<ul>
<li><a href="css/index.php">Home</a></li>
</ul>
</div>
<div id="spacer_horizontal"></div>
<div id="left_panel"><img src="images/feather-left-side-banner.jpg" width="152" height="495" /></div>
<div id="centre_panel">
<?php echo "<h1>Register</h1>"; ?>
<form action="register.php" method="POST">
<table>
<tr>
<td>
Your full name:
</td>
<td>
<input type="text" name="fullname" value="<?php echo $fullname; ?>">
</td>
</tr>
<tr>
<td>
Choose a username:
</td>
<td>
<input type="text" name="username" value="<?php echo $username; ?>">
</td>
</tr>
<tr>
<td>
Choose a password:
</td>
<td>
<input type="password" name="password">
</td>
<tr>
<td>
Repeat your password:
</td>
<td>
<input type="password" name="repeatpassword">
</td>
</tr>
</tr>
</table>
<p>
<input type="submit" name="submit" value="Register"/>
</div>
<div id="right_panel"><img src="images/feather-right-side-banner.jpg" width="152" height="495" /></div>
</div>
<div id="spacer_horizontal_bordered_top"></div>
<div id="bottom_panel">
<ul>
<li><a href="css/index.php">Home</a></li>
<li><a href="#">Search</a></li>
</div>
<div id="spacer_horizontal_bordered_bottom"></div>
<div id="footer">
Web Design Services<br />
<a href="http://jigsaw.w3.org/css-validator/check/referer" class="nav">W3C Compliant CSS</a> | <a href="http://validator.w3.org/check?uri=referer" target="_blank" class="nav">W3C Compliant XHTML</a><br />
<?php
echo date("l, dS F Y, h:ia" ,time());
?>
</div>
</div>
</body>
</html>

penders

4:51 pm on Aug 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Instead of calling die() (which will halt your script immediately) or echo() directly within your script, assign any errors to a variable instead and simply echo this wherever you like on your page.

If you use an array then you can output multiple validation errors in one go. To check for the 'error state' in your code just check to see whether anything has been assigned to your $errMsg variable.

Jamier101

11:17 am on Aug 15, 2011 (gmt 0)

10+ Year Member



So its as simple as opening the php tags further down the page and quoting the variable that I assigned the error message to?

penders

1:22 pm on Aug 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep, just as you are already doing with your form variables...
<?php echo $fullname; ?>
etc.

Just to add, regarding your form variables, you should be sanitizing these more before outputting in the form again, since they are user entered data... at the very least apply htmlentities()... eg.
<?php echo htmlentities($fullname); ?>