#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    31

    Issue with getting values from form??


    Hi,

    I am trying to develop my own custom form in wordpress and I am having trouble!

    I have a form in one php file that has 4 checkbox (values 1, 2, 3, 4) with the same input name. eg. form_cb[].

    In my other php file, I use the following code so that I can use the values from the 4 checkbox if it is checked.
    Code:
    $mycheck = join (', ', $_POST['form_cb']);
    I use this code to help me store values into my database in this format: 1, 3, 4.

    Which I then use with this code to retrieve later:
    $my_array = explode(',',$find_values);
    foreach ($my_array as $entry){.... blah blah

    I keep getting the following error when there are zero checkbox selected:
    join() [function.join]: Invalid arguments passed in.....blah blah

    I have tried adding this code before it because I read it somewhere online:
    $mycheck = array();

    But that doesn't work.

    I have come to the conclusion that the error occurs because the join is expecting it to be atleast 1 value and when no checkbox is selected, it produces "undefined" or "" value?? Is there another way of writing this code $mycheck = join (', ', $_POST['form_cb']); to get the same result, so that this issue does not occur.

    A "join" that will accept undefined or "" as a value and not cause issues.

    Thank you : )
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    stop right there. Do not store lists or arrays or whatever in a database. See normalization (especially the part on the First normal form) to understand why this is a bad idea and what you should do instead.

    This should also solve your join() problem, because you won't need this function at all.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    31
    Hi,

    Another plugin stores favourite post IDs of each user this way and it seems to work ok.

    The reason being is that the database table could become very very big if for example one user could select 20 colors each.

    Also how would I do it your way?

    The values of the 4 checkbox is being sent in one POST??

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    31
    How do you get those 4 values of the checkbox to the function without using this:
    $mycheck = join (', ', $_POST['form_cb']);
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by rePete
    Another plugin stores favourite post IDs of each user this way and it seems to work ok.
    Well, yeah. Shooting myself in the foot also "works".

    And just because other plugins do the same doesn't mean it's good. Not every open source "developer" knows what he's doing -- actually, Wordpress itself isn't really the pinnacle of software development.



    Originally Posted by rePete
    The reason being is that the database table could become very very big if for example one user could select 20 colors each.
    So? MySQL can handle billions of rows. Should you ever reach that point, you'll need a new server architecture, anyway.



    Originally Posted by rePete
    Also how would I do it your way?
    Have you read to linked articles? They go through a concrete example and explain what to do. To cut it short: Make a "chosen_colors" table and store each choice individually as a row (together with the user ID).
    Last edited by Jacques1; December 30th, 2012 at 01:09 PM.
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    To avoid the error you would use a conditional to check whether any of the checkboxes were checked before trying to join them.

    However, Jacques1 is correct, what you've described is a bad design. A lot of WordPress plugins have bad architectures, partly because WordPress itself has a poor architecture.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    182
    Rep Power
    83
    Originally Posted by rePete
    Also how would I do it your way?

    The values of the 4 checkbox is being sent in one POST??

    Thanks
    No, they are not. You have overlooked the fact that a checkbox is only sent if it is checked. None checked means none sent, just as you've discovered.
    IF you were going to do this, then you should test for the presence of the checkbox value in the $_POST data to see that it was checked

    But don't do it for this, as like other have said, this is bad design.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    31
    Thanks E-Oreo,

    Conditions worked to prevent join being executed when it is an empty value.

    Is it bad though to use "multiple" values in one attribute in my situation?

    From my understanding, it stores the group of values as a string.

    A set of numbers collected would be separated by a comma and stored as a long string in the database.

    Then to retrieve, a script will take the long string and separate each individual value by using the comma.

    I can see where it would be an issue if it was a full name for example where you need control for searching but for just collecting a group of numbers to use so you know what posts to grab for example, I do not see what the issue could be??

    Please enlighten me because I am still learning this stuff.
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by rePete
    I can see where it would be an issue if it was a full name for example where you need control for searching but for just collecting a group of numbers to use so you know what posts to grab for example, I do not see what the issue could be??
    There are several problems:
    • composite values (in your case comma-separated lists) are more or less inaccessible to the database system, so you're basically circumventing the whole system; instead of using the built in database functionalities (counting, joining, selecting etc.), you have to re-implement everything in your application; that's obviously bad design
    • VARCHARs can contain anything, so there's no guarantee for correct and sensible data; what if your application is buggy or somebody edits the data and you end up with nonsense strings like ",,aaa,"? What do you make out of that?
    • there's no way to enforce referential integrity (making sure that the color IDs actually point to existing colors)

    I mean, sure, if you don't give a damn about correct data and proper design, you can use comma separated lists.

    But why would you do that? While you were still fighting with that join() stuff, you might as well have created the new color table and rewritten a few queries. Then you'd have a working and clean solution now.
    Last edited by Jacques1; December 31st, 2012 at 02:26 AM.

IMN logo majestic logo threadwatch logo seochat tools logo