Simple Echo Problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Simple Echo Problems

Post by Trahb »

I'm creating a register page where it checks if the username already exists. If it does, I want it to echo out "This username exists" etc, etc.. but when I have it do so, it gets rid of all of the contents of that panel and just echoes the error. How would I go about keeping the rest and echoing the error above the register form?

I'm aware this could be done with AJAX, I just want to know another way to do it.
Sephern
Forum Commoner
Posts: 73
Joined: Sun Jan 04, 2009 4:44 pm

Re: Simple Echo Problems

Post by Sephern »

If I understand what you mean properly (and there's a good chance I don't), then what you basically want to do is display an error if the user enters an existing username, but still have the rest of the page the same?

In which case, have the form post to itself, then validate the information if $_POST['username'] or is set (using the isset function).
If the username function is set, then you can carry out your necessary checks (such as checking the username doesn't exist etc), and if it does, then set an $error variable.

In the 'else' clause of checking if $_POST['username'] is set, do an else if, and check if either $error is set, or $_POST['username'] isn't. If this validates as true, output the form.

Code: Select all

 
if (isset($_POST['username']))
{
    //validate to check if username exists, etc
    if (username exists/other error)
    {
          $error = "This username already exists";
     }
     //carry out code to insert user or w/e
}
else if ((!isset($_POST['username'])) || (isset($error)))
{
     if (isset($error))
     {
           echo $error;
     }
     //output form
}
}
 
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Simple Echo Problems

Post by Trahb »

Thanks for your response. I'll apply that real quick and see if it works ;D

Code: Select all

if (isset($_POST['username']))
{
    //validate to check if username exists, etc
    if (username exists/other error)
    {
          $error = "This username already exists";
     }
     //carry out code to insert user or w/e
}
else if ((!isset($_POST['username'])) || (isset($error)))
{
     if (isset($error))
     {
           echo $error;
     }
     //output form
}
}
Alright, there seems to be one problem with this; the only $_POST['username'] isn't set is when the page is initially loaded. Then you hit the button to register after filling out the form, and it is set.. which means it checks to see if the user is already a member and sets $error, which is useless at this point. It needs to be told what to do when $error is set, but inside of the isset($_POST['username']) condition.. if you understand what I mean?

Basically, if there is an error when you hit register, it needs to reload the page with the error or something. Still tryin to figure that out.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple Echo Problems

Post by califdon »

Some basics:

PHP is server-side, it does its stuff before a page is sent to the browser. It's all done before the browser gets the page.

Javascript is client-side, it does its stuff in the browser, so it can tell when a user does something, which PHP can't.

Your choices are:

1. Have your form submit to the server and if the username isn't valid, return a new page to the browser, with the form and all the rest, again.

2. Use Ajax, which sends the server request "asynchronously", meaning that it has a Javascript in the page that waits to receive the server data, and can then modify the page without reloading.

3. Use Javascript to do the checking. Of course, if you have to reference data on the server, to do the checking, then you're back to choice #2.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Simple Echo Problems

Post by Trahb »

Thanks for your input califdon.
I'd just like your opinion on my thoughts though.. I created a function in my register class, showForm(), which echoes the whole form. That way, in the actual register.php when ($_POST['formname']) is set, I check to see if the account exists or is legitimate, etc, and if it isn't, it echoes the error and uses the showForm() function.

I was wondering if this is a better method than AJAX, or if there's a better way of going about this?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Simple Echo Problems

Post by califdon »

For the kind of web sites that I create, I usually use Ajax because it presents the user with the smoothest display and is easier to program, if you know a little Javascript. There's only one downside--some minority of Internet users have Javascript turned off in their browsers (they've been scared off by hearing that they are more vulnerable to malware with Javascript enabled), so if it is important for your web site that absolutely EVERY last viewer can use it, then you shouldn't use Javascript at all.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Simple Echo Problems

Post by Trahb »

Thanks for your suggestion :)
Post Reply