Skip to main content
Known Participant
January 26, 2010
Question

Undefined Index errors, Warning implode() function error

  • January 26, 2010
  • 1 reply
  • 1833 views

ERROR MESSAGES:

Notice:  Undefined index:  membership in C:\htdocs\member_application.php on line 76

Warning:  implode() [function.implode]: Invalid arguments passed in C:\htdocs\member_application.php on line 76

Notice:  Undefined index:  prescircle in C:\htdocs\member_application.php on line 82

I have an extensive form that I'm having trouble with the radio buttons and the checkboxes (I thought I had this figured out from a previous discussion).

I've posted the ones regarding the errors:

76)     $membership = implode(", " , $_POST['membership']);
    if (empty($membership)) {
        $error['membership'] = 'Please select all that apply';
        }

82)   $prescircle = $_POST['prescircle'];

I thought I defined the indexes in the code below, and I'm not sure I understand why there's an invalid argument?

Code:

<fieldset>
<label for="membership">Please select all that apply:</label><?php if (isset($error['membership'])) { ?>
<span class="warning"><?php echo $error['membership']; ?></span>
<?php } ?>
<input type="checkbox" name="membership[]" value="Annual Membership" id="annualmembership" <?php
            $OK = isset($_POST['membership']) ? true : false;
            if ($OK && isset($missing) && $_POST['membership'] == 'Annual Membership') { ?>            
            <?php } ?>
            />
<label for="annualmembership">Annual Membership</label>
<input type="checkbox" name="membership[]" value="President's Circle" id="presidentcircle" <?php
            if ($OK && isset($missing) && $_POST['membership'] == 'President\s Circle') { ?>
            <?php } ?> />
<label for="presidentcircle">President's Circle (optional)</label>
</fieldset>

<span style="clear: right;">
<label for="prescircle"><strong>PRESIDENT'S CIRCLE (optional)</strong></label><br />
<input name="prescircle" type="radio" value="$250.00 - Platinum" id="platinum" <?php
    if (!$_POST || (isset($error) && $_POST['prescircle'] == 'platinum')) { echo
    'checked="checked"';
} ?> />
<label for="platinum">$500.00 - Platinum</label>
<br />
<input name="prescircle" type="radio" value="$250.00 - Gold" id="gold" <?php
    if (isset($error) && $_POST['prescircle'] == 'gold') { echo
    'checked="checked"';
} ?> />
<label for="gold">$250.00 - Gold</label>
<br />
<input name="prescircle" type="radio" value="$100.00 - Silver" id="silver"  <?php
    if (isset($error) && $_POST['prescircle'] == 'silver') { echo
    'checked="checked"';
} ?> />
<label for="silver">$100.00 - Silver</label>
<br />
<input name="prescircle" type="radio" value="$50.00 - Bronze" id="bronze"  <?php
    if (isset($error) && $_POST['prescircle'] == 'bronze') { echo
    'checked="checked"';
} ?> />
<label for="bronze">$  50.00 - Bronze</label></span>
<br />
<input name="prescircle" type="radio" value="None" id="none" <?php
    if (isset($error) && $_POST['prescircle'] == 'none') { echo
    'checked="checked"';
} ?>  />
<label for="none">None</label>
</fieldset>

I'm not sure where this code is going wrong.

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
January 26, 2010

Checkboxes and radio buttons are not included in the $_POST or $_GET arrays if they're not selected.

When doing your validation, you need to use isset() to check whether the array index exists:

if (isset($_POST['membership'])) {

  $membership = implode(', ', $_POST['membership']);

} else {

  // membership is missing

}

toad78Author
Known Participant
January 26, 2010

That does help, but I guess I'm back to square one because:

I already have radio buttons selected when the application loads with a default, but if someone misses something (i.e., checking a checkbox because there is no default checked) in the app and the it is submitted, I get the error message:

Notice:  Undefined variable: membership

which is this:


<label for="membership">Please select all that apply:</label>
<input name="membership[]" type="checkbox" id="annualmembership" value="Annual Membership" <?php
            $OK = isset($_POST['membership']) ? true : false;
            if ($OK && isset($error) && $_POST['membership'] == 'Annual Membership') { ?>            
            <?php } ?>
            />
<label for="annualmembership">Annual Membership</label>
<input type="checkbox" name="membership[]" value="President's Circle" id="presidentcircle" <?php
            if ($OK && isset($error) && $_POST['membership'] == 'President\s Circle') { ?>
            <?php } ?> />
<label for="presidentcircle">President's Circle (optional)</label>
<br />
<?php if (isset($error['membership'])) { ?>
<span class="warning"><?php echo $error['membership']; ?></span>
<?php } ?>

if (isset($_POST['membership'])) {
  $membership = implode(', ', $_POST['membership']);
} else {
   $error['membership'] = 'Please select all that apply';
   }

Plus, my radio buttons do not retain their value, which I thought was fixed with this:

<label for="plaque">Do you have a plaque? </label>
<input name="plaque" type="radio" value="Yes I have a plaque" id="plaque-yes" <?php
    if (!$_POST || (isset($error) && $_POST['plaque'] == 'Yes')) { echo
    'checked="checked"';
} ?>    />
<label for="plaque-yes">Yes</label>

<input name="plaque" type="radio" value="No, I do not have a plaque" id="plaque-no" <?php
    if (isset($error) && $_POST['plaque'] == 'No') { echo
    'checked="checked"';
} ?>    />
<label for="plaque-no">No</label>

I've been working on this for ten hours and am not sure what else is going on to cause this problem. That, or I'm completely discombobulated that I can't think straight!