Page 1 of 1

PHP Form Help!!

Posted: Wed Dec 06, 2006 5:09 pm
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]

Posted: Wed Dec 06, 2006 5:41 pm
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...

Posted: Wed Dec 06, 2006 6:19 pm
by califdon
Or you could just concatenate the messages, perhaps with a line-feed between messages.

Posted: Wed Dec 06, 2006 6:51 pm
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;
?>

Posted: Wed Dec 06, 2006 7:16 pm
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?

Posted: Wed Dec 06, 2006 7:31 pm
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).