I'm in chapter 7 of Larry Ullman's PHP book and there is a simple example that I can't get to work. 😕 (All in all, the book is going fairly well.)

When you submit the form but don't check any checkboxes, this comes up...

Error:
Notice: Undefined index: weekdays in /Users/user1/Documents/DEV/+htdocs/VisualQuickstart/0709_HandleEvent.php on line 15
Call Stack

Time Memory Function Location


1 0.0006 59120 {main}( ) ../0709_HandleEvent.php:0

According the book, my ELSE clause should handle tis scenario, and when I step through the code in NetBeans, it does skip to the ELSE part since is_array = FALSE.

Here are my two files...

0708_Event.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
		<form action="0709_HandleEvent.php" method="post">
			<p>Event Name: <input type="text" name="name" size="30"/></p>
			<p>Week Days:
			<input type="checkbox" name="weekdays[]" value="Sunday"/> S
			<input type="checkbox" name="weekdays[]" value="Monday"/> M
			<input type="checkbox" name="weekdays[]" value="Tuesday"/> T
			<input type="checkbox" name="weekdays[]" value="Wednesday"/> W
			<input type="checkbox" name="weekdays[]" value="Thursday"/> R
			<input type="checkbox" name="weekdays[]" value="Friday"/> F
			<input type="checkbox" name="weekdays[]" value="Saturday"/> S
			</p>
			<input type="submit" name="submit" value="Add the Event!"/>
		</form>
    </body>
</html>

0709_HandleEvent.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
			// Print introductory text.
			print "<p>You want to add an event called <b>{$_POST['name']}</b>
					which takes place on: <br />";

		// Print the weekday.
		if (is_array($_POST['weekdays'])) {
			foreach ($_POST['weekdays'] as $day) {
				print "$day<br />\n";
			}
		} else {
			print 'Please select at least one weekday for this event!';
		}

		// Complete paragraph.
		print '</p>';
    ?>
</body>
</html>

Some assistance would be nice!

Amy

    First check to see if it exists:

    if (isset($_POST['weekdays']) && is_array($_POST['weekdays'])) {
    

      NogDog,

      Well, that worked. Thanks!

      (Looks like we found a bug in my PHP book!!)

      But can you explain why my code didn't work? 😕

      It did branch the correct way, but still threw an error?!

      NogDog;10914007 wrote:

      First check to see if it exists:

      if (isset($_POST['weekdays']) && is_array($_POST['weekdays'])) {
      

      Wouldn't the array always exist whether it was blank of not?

      Isn't that what is_array does?

      I'm confused.

      Amy

        Checkbox <input> elements only get submitted if they are checked. So if none of them are checked, there will be no element in the $POST array with that key (index). Therefore if you run the is_array() function against that element ($POST['weekdays'] in your example), that specific element of $_POST is not defined, thus the error message.

        This error is a notice-level error, so it does not stop processing. It's purpose is to say to the programmer, "Hey, you passed a variable to a function but that variable does not exist. Maybe you typed it wrong in the program or something, so I'm just letting you know in case you need to fix something."

        In the case of the code from the book, either the author tested it under a PHP configuration that had notice-level errors turned off, or he was not as thorough in his testing as you were and did not test the boundary condition of not checking any of them.

          "Isn't that what is_array does? "

          is_array supposed that a variable has set before, if its still empty (the first page call that array has not set) you should not use the is_array on it.
          isset() is used to determine if a variable has a value.

          Nogdog's example still working but you have to divide this condition into two parts.
          (schematic)

          if (isset($_POST['weekdays']) )
          {
            this variable is an array?
            {
              yes
            }
            else
            {
              no   // this could happened if the form comes from an unsecure source, you should log this attempt and interrupt your program =>> mail to the admin with the referrer value.
          
            }
          
          }
          else
          {
          you have to select at least one
          }
          

          why? if you select only one element from the checkboxes the form will post the value as an array, no matter how many of them was selected:

              [weekdays] => Array
                  (
                      [0] => Sunday
                  )
          

          if i select two of them:

              [weekdays] => Array
                  (
                      [0] => Sunday
                      [1] => Saturday
                  )
          

            I did not see that part about wanting to verify that at least one was checked. You could it in one condition, if desired, as:

            if(!empty($_POST['weekdays']) && is_array($_POST['weekdays']) {
            

            empty() returns true if the variable is not set or if it has what PHP considers to be an "empty" value. For arrays, it is considered "empty" if it has no elements. So when it is negated by the "!" operator, it returns true if it is not empty. Lastly, empty() is like isset() in that it does not raise an error if the specified variable is not defined.

              Good conversation!

              A few questions...

              1.) I'd like to clarify how $_POST and arrays work in general work.

              My data entry form looked like this...

              		<form action="0709_HandleEvent.php" method="post">
              			<p>Event Name: <input type="text" name="name" size="30"/></p>
              			<p>Week Days:
              			<input type="checkbox" name="weekdays[]" value="Sunday"/> S
              			<input type="checkbox" name="weekdays[]" value="Monday"/> M
              			<input type="checkbox" name="weekdays[]" value="Tuesday"/> T
              			<input type="checkbox" name="weekdays[]" value="Wednesday"/> W
              			<input type="checkbox" name="weekdays[]" value="Thursday"/> R
              			<input type="checkbox" name="weekdays[]" value="Friday"/> F
              			<input type="checkbox" name="weekdays[]" value="Saturday"/> S
              			</p>
              			<input type="submit" name="submit" value="Add the Event!"/>
              		</form>
              

              I'm not sure, but this looks like $_POST would be a multi-dimensional array like this...

              [b](View #1)
              $_POST
              	name					weekdays[ ]
              ---------------------------	------------------------------
              key	value				key	value[/b]
              1	3-Day PHP Party			1	Sunday
              					6	Friday
              					7	Saturday
              
              [b](View #2 - alternate view)
              $_POST[/b]
              name:		3-Day PHP Party
              weekdays[ ]:	Sunday, Friday, Saturday
              

              Is this how data is sent (i.e. a multi-dimensional array) in the POST array?

              2.) The book's goal was to display a message if no weekdays were selected.

              Based on the earlier conversations between NogDog and djjjozsi, this is my understanding of things mentioned...

              [b]is_array()[/b]	Checks for an array which means there must be array elements
              
              [b]isset()[/b]		Checks if the array was ever given a value (i.e. "set") but does not
              			  imply that it currently has values.
              
              [b]!empty()[/b]	Checks that the array has at least one value.
              

              [/COLOR]
              So, to meet the book's goal, it seems like I JUST need to check for !empty( ) like this...

              if(!empty($_POST['weekdays'])) { 
              

              ...because you can't have an array (i.e. is_array() ) by your definitions if the arrary (e.g. weekdays[ ] ) is "empty" (that is empty() ), right?

              Thanks for all of the help so far! 🙂

              Amy

                I think you've pretty much sussed it all. When you use the "[]" naming in your form, the $POST element for that name becomes a sub-array of values under the $POST array, so yes, you are working with $POST being a multi-dimensional array in this case. The first checked box will be $POST['weekdays'][0], then next $_POST['weekdays'][1], etc.

                if(!empty($POST['weekdays'])) would generally work, but could be misleading if somehow (a hacking attempt?) a field named 'weekday' was submitted without the square bracket convention. Then $POST['weekdays'] would not be empty (assuming some sort of string was submitted with it, but it would not be an array. So to be completely sure you've received the sort of data you're expecting, it would be best to check both that it is not empty and that it is an array.

                  NogDog;10914078 wrote:

                  I think you've pretty much sussed it all.

                  What does that men??

                  NogDog;10914078 wrote:

                  if(!empty($POST['weekdays'])) would generally work, but could be misleading if somehow (a hacking attempt?) a field named 'weekday' was submitted without the square bracket convention. Then $POST['weekdays'] would not be empty (assuming some sort of string was submitted with it, but it would not be an array. So to be completely sure you've received the sort of data you're expecting, it would be best to check both that it is not empty and that it is an array.

                  Is there a way to reference the weekdays[ ] subarray within the $_POST[ ] array when using the !empty? Possibly...

                  if (!empty($_POST['weekday[ ]']) {
                  

                  or maybe...

                  if(!empty('weekdays[ ]')) {
                  

                  or maybe the best way to do it is what you mentioned as....

                  if(!empty($_POST['weekdays']) && is_array($_POST['weekdays']) { 
                  

                  Just trying to learn different (and better) ways to do things.

                  Thanks you for all of your help!

                  By the way, what part of the world are you in?

                  Amy

                    amy.damnit;10914095 wrote:

                    What does that men??

                    suss ~ v.: to infer or to figure (out)

                    Is there a way to reference the weekdays[ ] subarray within the $_POST[ ] array when using the !empty? Possibly...
                    ...
                    or maybe the best way to do it is what you mentioned as....

                    if(!empty($_POST['weekdays']) && is_array($_POST['weekdays']) { 
                    

                    ...

                    $POST['weekdays'] will reference the array of "weekdays" checkboxes submitted by the form. The foreach($POST['weekdays'] as $day) in your code will then loop through each element of that array, starting with $_POST['weekdays'][0] and so on.

                    So yes, what I'm getting at is that I think the best combination of thoroughness and brevity to validate that at least one weekday was selected is that last example. However, just to annoy you some more ( 😉 ), note that none of this so far has validated that any of the values received in that $_POST['weekdays'] array are valid day names. Depending on what you ultimately would want to do with those values and how, you might need another validation step - perhaps within the foreach loop checking each value against an array of allowed values

                    By the way, what part of the world are you in?

                    Amy

                    New Jersey, USA

                      NogDog;10914097 wrote:

                      suss ~ v.: to infer or to figure (out)

                      A new vocab word!

                      NogDog;10914097 wrote:

                      $POST['weekdays'] will reference the array of "weekdays" checkboxes submitted by the form. The foreach($POST['weekdays'] as $day) in your code will then loop through each element of that array, starting with $_POST['weekdays'][0] and so on.

                      I was just looking for a shortcut while still addressing your original security concern.

                      NogDog;10914097 wrote:

                      However, just to annoy you some more ( 😉 ), note that none of this so far has validated that any of the values received in that $_POST['weekdays'] array are valid day names. Depending on what you ultimately would want to do with those values and how, you might need another validation step - perhaps within the foreach loop checking each value against an array of allowed values

                      In addition to sounding really smart, you must be a PHP security expert.

                      That might be useful for me down the road.

                      Right now, though, I think I've beat this example to death. When I start writing my own code with my own standards, then I can revisit some of your suggestions.

                      Thanks for the help!

                      Amy

                        Write a Reply...