ThunderVike Posted July 17, 2010 Share Posted July 17, 2010 I am trying to make the method I read about at this tutorial work for my WordPress theme: http://www.phpfreaks.com/tutorial/working-with-checkboxes-and-a-database I have read lots and lots of tutorials on getting a multiple checkboxes to pass in an array but NONE of them has worked without producing php errors or simply feeding back ARRAY. I am using WordPress 3.0.2 with PHP 5 and mySql and a theme called ClassiPress that has its own functions and overrides. I modded the theme's own Form Builder to allow multiple checkboxes as one form input option. The form is used by a new or registered user to create Classified Ads and overrides the way Word Press defines Posts to define these meta values as a Classified Ad in what would normally be a single post page. This uses several different php files to assemble a specific form that applies to the category of "ad" (post category) the user wants to create...such as "Cars for Sale". In the step1.php page I inserted a simple checkbox form case 'checkbox': ?> <?php $options = explode(',', $result->field_values); foreach ($options as $option) { ?> <input type="<?php echo $result->field_type; ?>" name="<?php echo $result->field_name; ?>" value="<?php if($result->field_req) echo 'required'?><?php echo $option; ?>"><?php echo $option; ?><br> <?php } ?> <div class="clr"></div> <?php break; and this outputs something like this for one of many different checkbox values with different categories of values <li> <label>help checkbox: </label> <input type="checkbox" name="cp_checkbox_help[]" value="Sunday">Sunday<br> <input type="checkbox" name="cp_checkbox_help[]" value=" Monday"> Monday<br> <input type="checkbox" name="cp_checkbox_help[]" value=" Tuesday"> Tuesday<br> <input type="checkbox" name="cp_checkbox_help[]" value=" Wednesday"> Wednesday<br> <input type="checkbox" name="cp_checkbox_help[]" value=" Thursday"> Thursday<br> <input type="checkbox" name="cp_checkbox_help[]" value=" Friday"> Friday<br> <input type="checkbox" name="cp_checkbox_help[]" value=" Saturday"> Saturday<br> <div class="clr"></div> </li> I want to right there make sure that cp_checkbox_help[] holds the array on Submit of the whole form if the user selects Sunday, Tuesday, Thursday... whatever selection they make. I want to also get feedback when this form is submitted and returned immediately for review and editing before final submission by the user on the checked values. I will add that code later, right now jsquery is showing the checked values after the first draft is Posted before the final Ad submission. However the Submit action at the bottom of the form only shows "Array" after the Ad fields return for review and editing if needed by the user. My problem is that all of the array and map_array and explode and counting array iteration methods I have found and tried need to use $value= or some sort of thing like $cp_checkbox_help or if(isset($_POST[$somevalue] etc. This application uses a variable to deliver the "name" value in the input field. In one checkbox I might have cp_checkbox_help[] followed by multiple select checkboxes named cp_checkbox_rent[] or cp_checkbox_rent[] and on and on...but I don't want to have to hard-code all of these when name="<?php echo $result->field_name; ?>" delivers differently named boxes depending on the form. When the ad form is first posted with a submit it runs through some clean up functions and then comes // put all the posted form values into session vars foreach($_POST as $key => $value) $postvals[$key] = cp_clean($value); These session vars are saved temporarily as one immense string in the wp_options table instead of being held in a session for security purposes. It is at this step already that I cannot get the input fields to save as an array....I only get the first option saved in any multiple checkbox, or maybe it is the last saved value... How can I force PHP to save a comma delimited array as in the PHPFreaks tutorial similar to $admin = implode(',',$admin); or array_map('mysql_real_escape_string',$admin); every way I try to adapt these produces php errors. I have been working on this problem for days and have gone through over a hundred experiments and everything fails. Thank you for any wisdom! Quote Link to comment https://forums.phpfreaks.com/topic/208052-help-with-variation-on-tutorial/ Share on other sites More sharing options...
jcbones Posted July 18, 2010 Share Posted July 18, 2010 You need to take the array one step further: Setting an input element into an array, makes the $_POST array into a multidimensional array. //dump the array to the page to get a look at how it is built. echo '<pre>'; print_r($_POST); echo '</pre>'; //To get the values from the checkboxes. foreach($_POST['cp_checkbox_help'] as $value) { echo '<br />' . $value; } Quote Link to comment https://forums.phpfreaks.com/topic/208052-help-with-variation-on-tutorial/#findComment-1087676 Share on other sites More sharing options...
ThunderVike Posted July 18, 2010 Author Share Posted July 18, 2010 jcbones thank you very much for your help. I have tried getting this output before but I will try this again. Here is a step that adds the custom fields array..."cp_" is the proprietary method for prepending the values that are used for any custom field options or values...so when a table's field value has a "cp_" in front of it then these are Custom fields. I need to get that 'cp_checkbox_whatevername' array to go into just the $meta_value field. The table fields will look like: Examples-- meta_id post_id meta_key meta_value 851 137 cp_checkbox_amenities Jacuzzi,spa,gym 852 138 cp_checkbox_extras Gated,community pool,clubhouse,covered parking 853 139 cp_checkbox_interior 4 bath,3 levels,patio,terrace,filtered water This action is one of the steps in processing that looks for the custom field strings and grabs them separately for action 'cp_str_starts_with() is a custom function written to handle different variables -- // now add all the custom fields into WP post meta fields foreach($advals as $meta_key => $meta_value) { if (cp_str_starts_with($meta_key, 'cp_')) add_post_meta($post_id, $meta_key, $meta_value, true); } The method you showed me-- how can I use the same method where it dynamically handles whatever field_name arrives? This is what I asked about earlier. I have seen all kinds of solutions that spell out whatever name value is used in a fixed form. My problem is to make it DYNAMIC....the power of PHP and variables. Each multiple select checkbox choice will have some name I make up to when the I use the form builder to create a new situation of options. I have decided on a naming convention that all the checkbox fields will store as "cp_checkbox_-----" I cannot just WRITE in hard-code $_POST['cp_checkbox_help']. I need to make it dynamic so that I am getting an array built for each possible checkbox name that arrives later. I have re-used from name="<?php echo $result->field_name; ?>" the <?php echo $result->field_name; ?> . I have to make sure that the $value array is saved in that particular checkbox string as $meta_value Quote Link to comment https://forums.phpfreaks.com/topic/208052-help-with-variation-on-tutorial/#findComment-1087694 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.