MyBB Community Forums

Full Version: A Little PHP Help Needed.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am starting to learn PHP and I have run into a little problem. I want to know how to position errors that I echo from my registration script. For example the default spot for them to echo is the top left (I guess).


Code example:



<?php
if($_POST['submit'])
{
variable...
variable...

//codessss

if(strlen($password)>15 || strlen($password)<5)
{
echo "Your password is too short!";
}
}
?>

/html codes here
/ i want error codes here
/reg box here

Do you know HTML? PHP sends HTML output to the browser.

echo '<div align="left">message</div>';
echo() allows html codes to be placed in it.

EDIT - Damn it Paul
I want them to appear in the same div. For example:


[Image: examplevp.jpg]


The box on the left is an example of what is happening now, I am aware that echo allows html and yes i am very knowledgeable in html but i want the error messages to show in my div, above the form, like the example on the right.
Do something like this:

<?php
if($_POST['submit'])
{
variable...
variable...

//codessss

$errors = '';

if(strlen($password)>15 || strlen($password)<5)
{
$errors .= "Your password is too short!<br />";
}
}

if(anothererror)
{
$errors .= "Another error!<br />";
}

//html codes here
echo '<div align="left">'.$errors.'</div>'; 
//reg box here

Doing $var .= 'foo'; will add 'foo' to the end of the variable.

Hope that helps Smile
Exactly! Thank you Paul! Also, if you don't mind me asking another question. How would I query my database to check if a username exists, and if it does throw an error.
(2012-10-10, 04:04 AM)CorysRevenge Wrote: [ -> ]Exactly! Thank you Paul! Also, if you don't mind me asking another question. How would I query my database to check if a username exists, and if it does throw an error.
Doing Googling about basic stuff should help you a lot, mostly everything is available.

Anyway, here's a simple query to check:


//We query db first

$query = mysql_query("SELECT username FROM example_users");

//Fetching list of all usernames from db now

while($list = mysql_fetch_array($query))
{
$users = $list['username'];
}

//We have now list of all existing usernames from db, now we check them against registration

$input_username = mysql_real_escape_string($_POST['username']);

if($input_username == $users)
{
die("This username already exists, please select a new one.") //eventually you can use echo here
}
(2012-10-10, 05:28 AM)crazy4cs Wrote: [ -> ]
(2012-10-10, 04:04 AM)CorysRevenge Wrote: [ -> ]Exactly! Thank you Paul! Also, if you don't mind me asking another question. How would I query my database to check if a username exists, and if it does throw an error.
Doing Googling about basic stuff should help you a lot, mostly everything is available.

Anyway, here's a simple query to check:


//We query db first

$query = mysql_query("SELECT username FROM example_users");

//Fetching list of all usernames from db now

while($list = mysql_fetch_array($query))
{
$users = $list['username'];
}

//We have now list of all existing usernames from db, now we check them against registration

$input_username = mysql_real_escape_string($_POST['username']);

if($input_username == $users)
{
die("This username already exists, please select a new one.") //eventually you can use echo here
}

There is truly no need to collect all usernames that are in the database for a simple check to see if a specific name exists. What I would do and is usually done from what I have done in the past and seen is below.

(Note: Code may not be entirely correct as I am typing it on the fly)

$username = mysql_real_escape_string($_POST['username']);
$sql = mysql_query("SELECT username FROM users_table WHERE username = '".$username."' LIMIT 1");
$exists = mysql_fetch_array($sql);
if(isset($exists['username'])) {
	// The username already exists error goes here.
} 
That's true. Myself typing in hurry didn't recognized to present it in that way. You're correct.