The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Issue with getting values from form??
Discuss Issue with getting values from form?? in the PHP Development forum on Dev Shed. Issue with getting values from form?? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 30th, 2012, 12:07 PM
|
|
|
|
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 : )
|

December 30th, 2012, 12:21 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

December 30th, 2012, 12:29 PM
|
|
|
|
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
|

December 30th, 2012, 12:31 PM
|
|
|
|
How do you get those 4 values of the checkbox to the function without using this:
$mycheck = join (', ', $_POST['form_cb']);
|

December 30th, 2012, 12:57 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | 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.
Quote: | 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.
Quote: | 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.
|

December 30th, 2012, 03:36 PM
|
 |
Lost in code
|
|
|
|
|
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.
|

December 30th, 2012, 10:59 PM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | 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.
|

December 30th, 2012, 11:02 PM
|
|
|
|
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.
|

December 31st, 2012, 02:20 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|