Hi everyone,

I have an errors function that I need to dynamically apply the name from a series of checkboxes and I am a little but stuck, nesting/escaping the code inside a foreach loop.

Here is my foreach loop before I nest it.

if(isset($_POST['Rating']))
{
	foreach($_POST['Rating'] as $key => $value)
	{
	   $_SESSION['Rating'][$key] = $value;
	}
}

And a sample of the errors function looks like this....

$rate = errors('Rating', 'This is a required field.');

But since these are an array of dynamic checkboxes (and are required) they will be named as such.

Rating[0]
Rating[1]
Rating[2]
and so on.

So I tried to enclose the to code snippets like so...

if(isset($_POST['Rating']))
{
	foreach($_POST['Rating'] as $key => $value)
	{
	  $_SESSION['Rating'][$key] = $value;
	  $rate[$key] = errors('Rating[' . $key . ']', 'This is a required field.');
	}
} 

But when I print_r the results set is an empty array.

So I am not coding it correctly within the loop.

Where did I go wrong?

As always, I appreciate the guidance,

Regards,
Don

    It looks like it should work. I suggest debugging with say:

    if (isset($_POST['Rating']) && is_array($_POST['Rating'])) {
        $rate = array();
        foreach ($_POST['Rating'] as $key => $value) {
            $_SESSION['Rating'][$key] = $value;
            $rate[$key] = errors('Rating[' . $key . ']', 'This is a required field.');
            echo 'Rating[' . $key . ']<br />';
        }
        print_r($rate);
    }

      My bad on a couple of items.

      1) I meant radio buttons, (but that has no impact on this really, just a point I wanted to make).

      2) I was nesting this in the wrong place and when I tried your print_r I got no results, (I put a print_r in a different location, not in with the actual code snippet) and now I realize that I applied the code to the wrong location in my forms.

      3) After putting it in the correct location and using your print_r I am getting the following from this code usage:

      if(isset($_SESSION['products'])
      {
      	foreach($_SESSION['products'] as $key => $value)
      	{
      	   //-------------Make errors for ratings on form 2 ---------------------------//
      	  echo '$key=' .$key . ' $value=' . $value . '<br>';
      	  $rate[$key] = errors('Rating[' . $key . ']', 'This is a required field.');
      	  echo '<pre>--$rate--<br>';
      	  print_r($rate);
      	  echo '</pre>';
      	  //-------------Make errors for ratings on form 2 ---------------------------//
      	  //Blah blah the rest of this loop does not apply to this....
      	}
      }
      

      which gives me this.....

      $key=0 $value=UC-GD

      --$rate--
      Array
      (
      [0] =>
      )

      $key=1 $value=UR-ED

      --$rate--
      Array
      (
      [0] =>
      [1] =>
      )

      $key=2 $value=UR-FS

      --$rate--
      Array
      (
      [0] =>
      [1] =>
      [2] =>
      )

      So it is basically, not giving me anything. And worse, its nesting inside itself.

      Where did I go wrong in my logic?

      Thanks again,

      Don

        So how does the errors() function work? It sounds like that is where your problem is.

        If for example it's just taking its first argument as an index into $POST you'll be in trouble because you'd be looking for

        $_POST['Rating[2]']

        when you want

        $_POST['Rating'][2]

        .

        I do not know what you mean when you say it is "nesting inside itself": the repeated appearances of an increasingly long $rate array is simply due to the fact you print the contents of the array every time you add something to it.

          Here is the code (which I included in the main page of the form) for the errors function.

          function errors($field_name, $error_message)
          {
          	global $errors;
          	if(!isset($_POST[$field_name]) || ( trim($_POST[$field_name]) == '') )
          	{
          		$errors[$field_name] = '<font face="Arial" color="#FF0000" size="+1">&nbsp;' . $field_name . '<-' . $error_message . '</font>';
          	}
          }
          

          And the 3 videos I picked on form1 where:
          [0] =>UC-GD
          [1] =>UR-ED
          [2] =>UR-FS

          Which I use this data to build an array of radio buttons ($rate) for them to rate the videos on form2.

          On form2 the radio buttons are named (dynamically):
          Rating[0]
          Rating[1]
          Rating[2]

          So in the end, I need to build the $errors from the loop to look like this...

          $rate0 = errors('Rating[0]', 'This is a required field.');
          $rate1 = errors('Rating[1]', 'This is a required field.');
          $rate2 = errors('Rating[2]', 'This is a required field.');

          This is where I am now trying to take the POST data and process it into the errors function and 'add' it to my list of $errors array so if they do not rate each of the videos selected from form1, I can warn them.

          My errors function is working flawlessly (thanks to NogDogs here.)

          I am just trying to finish off the last of my validation with this (dynamically built list of radio buttons, from the users choices on from 1).

          Does that make sense, and did I fill in all the missing blanks?

          Thanks for the guidance and help.

          Don

            4 days later

            I am confused on your comment....

            If for example it's just taking its first argument as an index into $POST you'll be in trouble because you'd be looking for -> $POST['Rating[2]']

            when you want-> $_POST['Rating'][2]

            Because if, for example, I choose the 3 items from the previous form, I would end up with 3 sets of radio buttons to rate them (and unless I am approaching this incorrectly), labelled.

            <tr><td width="225" align=left style="padding-left:40px">The Great Depression</td>
            <td>Poor 1&nbsp;&nbsp;
            		  <input type="radio" name="Rating[0]" value="2" id="rating0"/>
            			</label>
            // the rest of this radio button code
            </td>
            </tr>
            
            <tr><td width="225" align=left style="padding-left:40px">Eusebio's Dream</td>
            <td>Poor 1&nbsp;&nbsp;
            		  <input type="radio" name="Rating[1]" value="2" id="rating1"/>
            			</label>
            // the rest of this radio button code
            </td>
            </tr>
            <tr><td width="225" align=left style="padding-left:40px">Freedom's Sound</td>
            <td>Poor 1&nbsp;&nbsp;
            		  <input type="radio" name="Rating[2]" value="2" id="rating2"/>
            </label>
            // the rest of this radio button code
            </td>
            </tr>
            

            So shouldn't I be looking to dynamically make the errors function text produce ...

            $POST['Rating[0]']
            $
            POST['Rating[1]']
            $_POST['Rating[2]']

            IE:

            $rate0 = errors('Rating[0]', 'This is a required field.');
            $rate1 = errors('Rating[1]', 'This is a required field.');
            $rate2 = errors('Rating[2]', 'This is a required field.');

            ?

            Or am I way off base here?

            Thanks for the help,

            Don

              Write a Reply...