PHP Form Help!!

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

Locked
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

PHP Form Help!!

Post by aspekt9 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I Could use some help on a PHP project. I'm a bit new to PHP and I'm currently building a registration/login form. Now here's what I have to detect if the user as filled out all the necessary values:

Code: Select all

<?php
include 'english.php';

$username=$_POST['userid'];
$upassword=$_POST['upassword'];
$cpassword=$_POST['cpassword'];
$email=$_POST['email'];
$name=$_POST['name'];
$msg="";
$Status="True";

global $LANG01;

if (strlen($username) > 10) {
	  $msg = ($LANG01[1]);
} elseif (empty($username)) {
	  $msg = ($LANG01[2]);
}

if (strlen($upassword) < 6) {
	  $msg = ($LANG01[3]);
} elseif (empty($upassword)) {
	  $msg = ($LANG01[4]);
}

echo $msg;
?>
The array is located in an included file called english.php. Now here's what I'm having problems with, I want $msg to display all the errors stored to it. Meaning if a user doesn't enter a username and doesn't enter a password I want it to return both those values but the way it is now, it only stores the latter value. My question is how I can store and display 2 different variables. Or atleast point me in a direction on how to go about doing so. Thanks in advance.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
You could use $msg as an array.

Code: Select all

$msg[] = 'foo';
$msg[] = 'bar';
//etc...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Or you could just concatenate the messages, perhaps with a line-feed between messages.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You could do something like this...

Code: Select all

<?php
include 'english.php';

$username = $_POST['userid'];
$upassword = $_POST['upassword'];
$cpassword = $_POST['cpassword'];
$email = $_POST['email'];
$name = $_POST['name'];
$msg = '';
$Status = "True";

global $LANG01;

if (!empty($username))
{
    if (strlen($username) > 10)
    {
        $msg .= $LANG01[1] . '<br />';
    }
}
else
{
    $msg .= $LANG01[2] . '<br />';
}

if (!empty($upassword))
{
    if (strlen($upassword) < 6)
    {
        $msg .= $LANG01[3] . '<br />';
    }
}
else
{
    $msg .= $LANG01[4]'<br />';
}

echo $msg;
?>
Or if you really like your code better than mine, you could do this...

Code: Select all

<?php
include 'english.php';

$username = $_POST['userid'];
$upassword = $_POST['upassword'];
$cpassword = $_POST['cpassword'];
$email = $_POST['email'];
$name = $_POST['name'];
$msg = '';
$Status = "True";

global $LANG01;

if (strlen($username) > 10) {
          $msg .= $LANG01[1] . '<br />';
} elseif (empty($username)) {
          $msg .= $LANG01[2] . '<br />';
}

if (strlen($upassword) < 6) {
          $msg .= $LANG01[3] . '<br />';
} elseif (empty($upassword)) {
          $msg .= $LANG01[4]'<br />';
}

echo $msg;
?>
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

That worked exactly how I needed it to, thank you Everah. My formatting is horrible I know, is there a common site that states rules for php formatting?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, that is one of the 'joys' of PHP... how lose it is. There are several published standards out there, and I am not going to reignite the fires of the past flame wars on this subject :wink: by giving you any direction as to how you should go with it. I will say this, pick a standard and code by it. Find one you are comfortable programming in, and stick with it regardless of what anyone tells you (just so long as it works... and you are not developing for a team that has a standard different than yours).
Locked