Jump to content

array (validation) problem


Ninjakreborn

Recommended Posts

[code]<?php
// create array of all variables that have to be filled in
$empty = array($_POST['firstname'], $_POST['lastname'], $_POST['password'], $_POST['verifypassword'], $_POST['country'], $_POST['stateorprovince'], $_POST['county'], $_POST['zipcode'], $_POST['birthdate']);
foreach ($empty as $k=>$v) { // run them through to see if there empty
if ($k == "") { // if they are register error messages
$errorhandler .= $k . "was left blank, please correct this.<br />";
}
}
?>[/code]
That below produces
[quote]0was left blank, please correct this.[/quote]
I am 100% unsure what the hell Owas means, but it certainly isn't in my form.  I probably constructed the thing wrong, do you see anything that might have caused this.  The form is a form with all the name's, the same.  I am sure of that, I just don't understand why it's outputting that.
Link to comment
Share on other sites

You need to check if $v is empty, not $k. It's also a good idea to set the initial value of the $errorhandler to an empty string.

[code]<?php
// create array of all variables that have to be filled in
$empty = array($_POST['firstname'], $_POST['lastname'], $_POST['password'], $_POST['verifypassword'], $_POST['country'], $_POST['stateorprovince'], $_POST['county'], $_POST['zipcode'], $_POST['birthdate']);
$errorhandler = "";
foreach ($empty as $k=>$v) // run them through to see if there empty
{
if (empty($v)) // if they are register error messages
{
$errorhandler .= $k . " was left blank, please correct this.<br />";
}
}
?>[/code]

Orio.
Link to comment
Share on other sites

[quote]Array name/position 0 (zero) was left blank. [/quote]
Thanks I will look up on that.

[quote]You didn't leave a space between the variable and the statement.[/quote]
I don't understand what you meant by that part.

[quote]You need to check if $v is empty, not $k.[/quote]
Thank you I will try that.

[quote]It's also a good idea to set the initial value of the $errorhandler to an empty string[/quote]
it is, this is just an small excerp taking out of the whole code (to show you specifically where I was having the problem without revealing "all" the source code.

Thanks for the advice, I will try that.
Link to comment
Share on other sites

I made the correct change, now I am getting this error message

[quote]0was left blank, please correct this.
1was left blank, please correct this.
2was left blank, please correct this.
4was left blank, please correct this.
5was left blank, please correct this.
6was left blank, please correct this.
7was left blank, please correct this.
8was left blank, please correct this.[/quote]
Link to comment
Share on other sites

[quote]$errorhandler .= $k . "was left blank, please correct this.
";

Notice the Space missing after the "? Should be this:
$errorhandler .= $k . " was left blank, please correct this.
";[/quote]
Thank you I didn't notice that.

That however had nothing to do with the problem (did make it look a lot nicer, thank you for pointing that out.

I am understanding "what" is happening.  It's reading off the key's as 0-9.
What I wanted (and was hoping for), is since the name of the input's, were firstname, lastname (ex-cetera, I was hoping those names would be picked up as the key's instead it's making them keyed based on number.  Any advice, or in the future, does the key, have to be manually created in the array (Basically does it pick up the input name as the key in a php array)

Thank you for your help and patience.
Link to comment
Share on other sites

Well, if you're going to specifically make an array of your posted variables like that, then go ahead and specifically make it an associative array:

[code]
$empty = array('firstname' => $_POST['firstname'],
                      'lastname' => $_POST['lastname'],
                      'password' => $_POST['password'],
                      'verifypassword' => $_POST['verifypassword'],
                      'country' => $_POST['country'],
                      'stateorprovince' => $_POST['stateorprovince'],
                      'country' => $_POST['county'],
                      'zipcode' => $_POST['zipcode'],
                      'birthdate' => $_POST['birthdate']);
[/code]
Link to comment
Share on other sites

[quote]$empty = array('firstname' => $_POST['firstname'],
                      'lastname' => $_POST['lastname'],
                      'password' => $_POST['password'],
                      'verifypassword' => $_POST['verifypassword'],
                      'country' => $_POST['country'],
                      'stateorprovince' => $_POST['stateorprovince'],
                      'country' => $_POST['county'],
                      'zipcode' => $_POST['zipcode'],
                      'birthdate' => $_POST['birthdate']);[/quote]
Thanks for the advice, yes that is what I had in mind, if it wasn't going to pick them up automatically.  Thanks for all the help.

Actually I just noticed, you wrote it out for me, thank you.
Link to comment
Share on other sites

businessman, are you sure fixed what I told you?

You original script did:
[code]if ($k == "")[/code]

When it should be:
[code]if(empty($v))[/code]

Notice- it's $v that needs to be checked, not $k. And it's always better to use empty() rather than '==""'.
This way you won't need to name the keys (firstname, lastname ....)


Orio.
Link to comment
Share on other sites

Also, I have been alot with making my code cleaner, neater, better commented a lot lately.
Thank you for the example array, it helps me have a new, neater, cleaner way to setup arrays, I think I will format them like that from now until, until(if) I see another way that I like the looks of more, but it's a drastic improvement for now, thanks.


[quote] businessman, are you sure fixed what I told you?

You original script did:
Code:

if ($k == "")


When it should be:
Code:

if(empty($v))


Notice- it's $v that needs to be checked, not $k. And it's always better to use empty() rather than '==""'.
This way you won't need to name the keys (firstname, lastname ....)[/quote]
Can you re-explain what you mean.  Well I know what you mean, but can you re-explain why.
I can change it, it won't be that hard, but so far, I have always used == "";
or != "" for everything
like
$_POST['firstname'] != ""
I have done that for a long time.

What are the differences as far as functionality.  Which one is better, is there some kind of attached proof, or example of "how" it's better, so I don't make a blind change.  You might have a very valid point, I just wnat to be absolutely sure, before I make a change that affects my entire style.

Thanks for all the help.
Look forward to your answers.

So far I had changed it over to
[quote]<?php
      foreach ($empty as $k=>$v) { // run them through to see if there empty
if ($v == "") { // if they are register error messages
$errorhandler .= $k . " was left blank, please correct this.<br />";
}
}
?>[/quote]
I can also use the other, I just need to see some reasoning behind it, so I am sure.  Thanks.
Link to comment
Share on other sites

I believe what you're looking to do is to have $empty be an array of the key names that need to be filled and iterate through them in a fashion similar to what is shown below.

[code]
<?php
$empty = array('firstname', 'lastname', ....);
foreach ($empty as $key)
{
    if (empty($_POST[$key]))
    {
          echo "$key is empty";
    }

}
?>
[/code]

Also keep in mind that you should be validating that the fields not only have a value but also that the values submitted are valid. ie if someone said their birthdate was "@" you probably wouldn't want to accept that.
Link to comment
Share on other sites

I have all of that dealt with, with functions (Currently converting all my functions to OOP), for now, arrays I was weak at, lately regex and arrays I have been really getting into.  Forcing myself to use them a lot to help solve other problems.

[code]$empty = array('firstname', 'lastname', ....);
foreach ($empty as $key)
{
    if (empty($_POST[$key]))
    {
          echo "$key is empty";
    }

}[/code]
I will keep that in mind, if the current approach I am using doesn't work.
Link to comment
Share on other sites

there is no point in coding it how you are. $_POST in its self is an array and there is no point putting all your post data in an array. Well the are a few cases but from looking at your code it just looks pointless.

Also us isset() is what you need to use.

<?php error_reporting(E_ALL)?>

place that at the top of your script. as you will get a notice about an array key not existing.


[code]
if (isset($_POST['submit'])) {
$this->something = $_POST['submit'];
$this->value_1 = $_POST['value_1'];
}
[/code]
#then later in your code
#you just use $this->something as your value it allows you to sort of plug your script into any form you dont need to remember the array #element names when making the html and it is just easier from a design point of view

[code]
protected function _config() {
if (isset($_POST['login'])) {
$this->username = $_POST['username'];
$this->password = $_POST['password'];
$this->remember = $_POST['remember'];
$this->login = $_POST['login'];
}

if (isset($_GET['logout']))
$this->logout = $_GET['logout'];
}
[/code]
the above is the configuration of my login script. You can do the same for any kind of data not just $_POST. I use the same kind of abstraction to get the user data to compare against  and if the user is not in that data it opens an abstract method called failure where I can add errors to an error array

my class has an error object that pretty much just allows an interface to an array for example

[code]
framework_namespace::error('login', 'the username does not exist');
[/code]

The html is also 100% separate. I define constants so i can do something like

[code]
if (defined('USER_LOGGED')) {

}
[/code]
the reason I use constants is they are not restricted by scope.

The reason I am ranting is I just think you have taken he wrong approach to forms and I think there is a better way. Also converting functions to oop like you have said you are going to do is the wrong approach. what could strlen() possibly gain by being an object?? Am I saying to throw out all your code or leave it as a normal function? NO, what I am saying is there are different and much better ways to do some things in oop and if it the code was designed for a function there is probably not much point in making an exact copy that is oop.

You said you are going to try regex and arrays a lot but do you realize that regex is slow and there are times to use it and times to not use it. Email validation i a good use of it, checking for a set length, prefix, suffix is not a good use of regex. As for the arrays I know why you keep doing

$v = $_POST['v'];

I know why people do it and its stupid. $this->v = $_POST['v'] actually servers the purpose of abstraction but what does $v = $_POST['v'] do?? it only allows you using the ugly $_POST['v']. But there is a better way to do this

[code]
function call($_POST['username'], $_POST['password']);
[/code]

The code above allows for you to send the data to a function then you can name the values to whatever you want. It is clean and better for a large number of reasons.

Hopefully someone will find this rant useful.




Link to comment
Share on other sites

[quote]<?php error_reporting(E_ALL)?>[/quote]
I don't really like notices, for a variety of reasons, it conflicts with my programming style.

[quote]Also us isset() is what you need to use.[/quote]
I did that was just a small excerpt out of my code.

I got the array situation working.

I will keep what you said about the layer's next time.  I will attempt at what you said, and see how it goes, and how I like it.

[quote]The html is also 100% separate. I define constants so i can do something like[/quote]
I am moving into MVC as we speak, havn't gotten to that yet, I am working on moving my personal programmer to OOP/MVC

[quote]the reason I use constants is they are not restricted by scope.

The reason I am ranting is I just think you have taken he wrong approach to forms and I think there is a better way. Also converting functions to oop like you have said you are going to do is the wrong approach. what could strlen() possibly gain by being an object?? Am I saying to throw out all your code or leave it as a normal function? NO, what I am saying is there are different and much better ways to do some things in oop and if it the code was designed for a function there is probably not much point in making an exact copy that is oop.

You said you are going to try regex and arrays a lot but do you realize that regex is slow and there are times to use it and times to not use it. Email validation i a good use of it, checking for a set length, prefix, suffix is not a good use of regex. As for the arrays I know why you keep doing

$v = $_POST['v'];

I know why people do it and its stupid. $this->v = $_POST['v'] actually servers the purpose of abstraction but what does $v = $_POST['v'] do?? it only allows you using the ugly $_POST['v']. But there is a better way to do this

Code:

function call($_POST['username'], $_POST['password']);


The code above allows for you to send the data to a function then you can name the values to whatever you want. It is clean and better for a large number of reasons.

Hopefully someone will find this rant useful.
[/quote]
I want to see the validity in your point, but I have to hear atleast what a few other people say in response to it, to formulate an opinion.  I also want to see if you can re-explain some of it, I caught most of it, but some of it, I didn't understnad (I haven't done OOP/MVC very long, I am still studying them.
Link to comment
Share on other sites

[quote]
<?php error_reporting(E_ALL)?>
I don't really like notices, for a variety of reasons, it conflicts with my programming style.
[/quote]

the idea is your code should be setup so you get zero notices and in many cases you dont know they are there unless E_ALL is turned on

By separated the html I did not mean MVC I meant its essentially wireless. the form just has to have the corrasponding name="" on each input box and then it works the only thing you need to manage is the login state and errors and that best setup in a way that is wireless and not directly corrected to the login script.

I did not mean for this to get into a login script talk but I just thought it was the simplest example I could think of off the top of my head.

I will try to explain the above more clear for you. The general idea is you are coding a function or class that you want to work with a form. doing something like

login($_POST['username'], $_POST['password']);

is better than

function login () {
$username = $_POST;
$password = $_POST;
}

and better than

$username = $_POST
$password = $_POST

function login($username, $password)

The stuff I said about $this->username = $_POST['username'] is pretty much the same as the example above except as you should know $this->username is available everywhere within the extended class and the current class if you dont understand that you need to learn inheritance.

Some people may like my approach and some might not << in response to you saying you want others opinions.

But at the end of the day this code like this that allows me to place all my configuration in an object but at the same time it is in an object it is not hard coded. All the configuration is in one extended class and all the logic is in the abstract class. It makes for clean maintainable code.

But seriously turn E_ALL on, it throws notices for a reason.

Do you understand why not using isset() throws an error?? a better example would be do you understand the difference between


if ($variable) {

}

and

if (isset($variable)) {

}


the difference is that $variable gets value of the variable and checks if it is true where as isset just checks if it exists.

Another example would be do you know why the notice of

if ($array['key']) {

}

do you get why that throws a notice if the key does not exist?? Because it is trying to get a value of something that does not exist. It is faster and better to just check if it exists instead of getting the value and checking if its true.

About mvc I have seen some of your posts and replied to a few of them as Im sure you are aware. You seem to think mvc and all this stuff is the holy grail. To me it sounds like quite a narrow focus. There is so much you should be trying to learn I get the impression you barely know object oriented programing.

From reading around this entire forum it just sounds like people are obsessed with being able to say MY SITE IS MVC and there is way way way way more to good design than putting an mvc stamp on it


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.