0

Please see this example:

if (strlen($_SESSION['userDetails']['THISNAME']) == 0)
{
    $_SESSION['userDetails'][''.$THISNAME.'Error'] = "autofocus";
    include "$docRoot/html/forms/reg/user_info.html.php";
    exit();
}

How would i use the name of THISNAME as I have done to create a new variable that appends Error on the name so in this example the output would be THISNAMEError?

Thanks!

6
  • 1
    You cannot do it, you are approaching problem from wrong angle. Commented Jun 17, 2012 at 21:11
  • 2
    if you are accessing 'THISNAME' key in your conditional then you know what THISNAME is, so you can assign it to a variable $key = 'THISNAME'; and append error on to it using $key . Error`; Commented Jun 17, 2012 at 21:12
  • @dm03514 , what he want is this: $foo = 'bar'; echo magic_function($foo); outputs "foo". Commented Jun 17, 2012 at 21:13
  • @tereško haha magic_function :) yes I like that... Do you have it? Commented Jun 17, 2012 at 21:19
  • @BlackberryFan , no, you cannot do it. You will have to explain , what is the problem , which you are trying to solve this way. Commented Jun 17, 2012 at 21:20

3 Answers 3

2

You already know "THISNAME" in order to test its length. Why can't you just put it again where you want it?

As a more general thing, though, you can get the list of keys of an array with array_keys(), or loop through them with foreach($input as $key=>$value).

Sign up to request clarification or add additional context in comments.

6 Comments

to answer the first part of your question, because that would involve typing it out twice, and there are alot of fields, that seem to be constantly changing... I've found that appending it manually it making the task cumbersome so I thought, being php there must be a simpler way!? As for the second part... I'll have to think about that, thank you for your input though!
Is there a way to get the names of each field submitted from a form? Like say the form has 4 post fields... a, b, c, d... When you submit the form can you get a output like "a, b, c, d" or is that what array_keys() does?
Yup, just use array_keys($_POST). I frequently use that when getting an array of checkboxes.
I have given the answer to tereško as he has provided the code (with the addition of your input) that has solved my problem. Thanks very much!
@Kolink , thats an extremely bad idea, since you can bypass validation by removing the field.
|
1
//if(strlen($_SESSION['userDetails'][$THISNAME]) == 0)
//|
//|->if you are checking for $THISNAME,
//   why you then create a new varialbe $THISNAME.'Error' ?

//if(strlen($_SESSION['userDetails']["$THISNAME_Error"]) == 0) {
if( ! isset($_SESSION['userDetails']["$THISNAME_Error"])) {

   $_SESSION['userDetails'][$THISNAME.'_Error'] = "autofocus";
   //or
   $_SESSION['userDetails']["$THISNAME_Error"] = "autofocus";

}

Should work

2 Comments

That would get the variable content then append Error to it? so it could be something like w0rldartError? If the variable has the content of w0rldart I need THISNAMEError.
check my answer's question, it checks to see if (where $THISNAME = 'alex') $_SESSION['userDetails']["alex_Error"]'s length equals to 0.
0
$required_fields = array( 'name', 'email', 'password', 'password_verification' );
$errors = array();

foreach ( $required_fields as $field )
{
   if ( !isset($_POST[$field]) )
   {
      $errors[$field] = 'not set';
   }
}

if ( count($errors) > 0 )
{
    // there will be errors in thy forms
}

Is this what you are looking for ?

2 Comments

See the answer and comments by Kolink
@BlackberryFan , you cannot replace the $required_fields with just array_keys($_POST) because: it may not contain all the required field names(making possible to bypass validation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.