PHP - How to display this Server Error in a decent way
Here is my login PHP code.
<?php // rnlogin.php
include_once 'rnheader.php';
echo "<h3>Member Log in</h3>";
$error = $user = $pass = "";
if (isset($_POST['user']))
{
$user = sanitizeString($_POST['user']);
$pass = sanitizeString($_POST['pass']);
if ($user == "" || $pass == "")
{
$error = "Not all fields were entered<br />";
}
else
{
$query = "SELECT user,pass FROM rnmembers
WHERE user='$user' AND pass='$pass'";
if (mysql_num_rows(queryMysql($query)) == 0)
{
$error = "Username/Password invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
die("You are now logged in. Please
<a href='rnmembers.php?view=$user'>click here</a>.");
}
}
}
echo <<<_E开发者_如何学PythonND
<form method='post' action='rnlogin.php'>$error
Username <input type='text' maxlength='16' name='user'
value='$user' /><br />
Password <input type='password' maxlength='16' name='pass'
value='$pass' /><br />
<input type='submit' value='Login' />
</form>
_END;
?>
As you can see the error message is printed with echo. Is there a way that I can put this error message in a control and display it only if there is error?
It sounds like I have to combine PHP with JavaScript?
The following code is used by Gmail to display error message. It works even if JavaScript is turned off. In other words, they use some techniques independent of JS.
Any idea, how I can do similar thing here?
<code>
<td align="left">
<div id="errormsg_0_Passwd" class="errormsg">
The username or password you entered is incorrect.
[<a target="_blank" name="helpLink" href="http://www.google.com/support/accounts/bin/answer.py?answer=27444&hl=en&ctx=ch_ServiceLoginAuth&p=mail">?</a>]
</div>
</td>
</code>
Thank you
Is there a way that I can put this error message in a control and display it only if there is error?
in PHP you can do anything. Want to put this error message in a control? Put it into control
if ($error) $error = "<control>$error</control>";
You have to distinguish string operation from echoing.
echoing a string is not the only way of string manipulations.
you can prepare your string first and then echo it out.
So, you can prepare all parts of the string first, and then print them all together.
Also, there is another way to output HTML, a preferred one.
<?php
// your php code here
//before output we close PHP tag and use pure HTML
//with some PHP specks
?>
<form method="post" action="rnlogin.php">
<?php if($error): ?>
<code>
<td align="left">
<div id="errormsg_0_Passwd" class="errormsg">
<?php echo $error ?>
</div>
</td>
</code>
<? endif ?>
Username <input type='text' maxlength='16' name='user'
value='$user' /><br />
Password <input type='password' maxlength='16' name='pass'
value='$pass' /><br />
<input type='submit' value='Login' />
</form>
Your answer stays in your question!
You declared $error=""
make use of this,
check like this
if($error!="") echo $error;
anyone in my opinion do like this if the error exists then show it other wise do the rest.
for this you may need to change your code..
make the html part out of php tags and inside the html part use php tags again and display the error message.
in my observation you need to have bit knowledge on html also.
精彩评论