0

I'm a beginner with PHP and I'm building a sign-up form. Now I'm using also the type="checkbox" because I want to now with the form wich day people are visiting in a week.

When I'm processing the form everything goes well if somebody check one of the boxes, but not when people don't check one of the checkboxes, then I'll get a PHP Implode error.

<li><input class="bso" type="checkbox" name="day[]" value="monday"/> monday</li>
<li><input class="bso" type="checkbox" name="day[]" value="tuesday"/> tuesday</li>
<li><input class="bso" type="checkbox" name="day[]" value="wednesday"/> wednesday</li>
<li><input class="bso" type="checkbox" name="day[]" value="thursday"/> thursday</li>
<li><input class="bso" type="checkbox" name="day[]" value="friday"/> friday</li>

So what I need to know is, what kind of php code do I need to succesfully process my PHP code without getting a warning when somebody doesn't check one of the boxes, because sometimes the people that are gone use the form don't want to visit.

THNX!

aproskam
  • 121
  • 1
  • 1
  • 10
  • there is already a question like this: http://stackoverflow.com/questions/4688880/html-element-array-name-something-or-name-something i guess in order to avoid the error u need to check if it is empty first if (!empty($_POST['day'])) { ... } – Michael Apr 09 '13 at 20:15

2 Answers2

2

The problem you are running into is that you need to check the existence of values for day in the POST and verify the value for day is indeed an array before trying to do implode() on that array.

$day_string = '';
if(!empty($_POST['day']) && is_array($_POST['day'])) {
    $day_string = implode(',', $day);
} else {
    // you didn't get at least one day checked, so issue error in this code block
}

If this same code is run for both GET and POST, then you might need to check that this is a POST (as else if or before even entering this block of code) such that you don;t get an error shown for GET requests.

As a matter of practice you should ALWAYS check for the existence of and validate expected format/values of any user-provided input before working with it in your code. Get yourself familiar with isset(), empty(), filter_var(), is_array() and similar functions.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Thnx for your quick answer! I'm doing some experiences with my php code and now I'm stuck. What I'm trying is that I'm writing a code that you must check one of the boxes and if you don't you get a echo that's saying "you haven't checked one of the boxes" but I'm failing hard with writing the code! – aproskam Apr 12 '13 at 16:10
  • @user2032585 Well typically, I always approach such validation issues on two fronts. First, I put javascript validation on the page itself, such that ideally the form wouldn't even be posted. Then I put validation on the server-side as well (as you can't trust client behavior). In your case, you can simply add an `else` condition to the conditional I placed earlier. I have updated my answer above to reflect this. – Mike Brant Apr 12 '13 at 19:51
  • Your script was really helpful, thnx again!! But since a month it stopped working, while I didn't chance a thing. The problem is now that I'm not receiving the results from the checkboxes anymore except "Array", how is that possible? – aproskam Sep 04 '13 at 18:17
  • @user2032585 I am not sure I am following your problem. If you have not changed your HTML and not changed your PHP, I see no reason why your behavior would change. I don't know what you mean by `I'm not receiving the results from the checkboxes anymore except "Array"` – Mike Brant Sep 04 '13 at 19:57
1

If I'm understanding you well, you want to get some form of statistics about when do your visitors hit your website. Isn't it easier if you slide a hidden input like this?:

<input type="hidden" name="day" value="<?php print strtolower(date("l")); ?>">

That way, you would get what you want without your users having to click anything. On monday, this would look like <input type="hidden" name="day" value="monday"> and so on.

Hope that helps :P

Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37