Welcome Guest, Not a member yet? Register   Sign In
Ci 3 $_POST not accepting spaces?
#1

Hi All, 
I am building a car dealer app for a friend using Ci 3 and HMVC.
from the database i am querying a set of car options, that get listed in a view file as checkboxes. 
in the controller :

PHP Code:
function list_options(){

$mysql_query "SELECT t1.option_name
     , t2.id
     , t2.parent_name 
  FROM option_children AS t1
LEFT OUTER JOIN option_parents AS t2
    ON t2.id = t1.parent_id"
;
$query $this->_custom_query($mysql_query);
$result $query->result();

var_dump($result); die();

foreach(
$result as $row){

 
$groupedData[$row->parent_name][] = $row->option_name;
 }
 return 
$groupedData;




and displayed in a form like this :

PHP Code:
foreach ($caroptions as $key => $value) {
   echo '<dl style="margin-bottom: 1em;">';
 echo 
"<dt>$key</dt>";
 foreach (
$value as $row ) {
  ?>
 <div class="checkbox">
    <label>
      <input name="options[<?= $row ?>]" value ="1" type="checkbox">
      
      <?=  $row ?>
    </label>
  </div> 
<?php }
?>
 </dl>
<?php



so far so good, but when i actually select some of the checkboxes and submit the form , data gets collected like this :

PHP Code:
$data["optionsarray"] = $this->input->post('options[]'TRUE); 


Now, when the key contains spaces, this does not work and an empty key is returned. like ["0"] => string(1)"1"  for example if i select an option called:
2 door , then the key will not be [2 door] but it will be empty. Same goes for when i use 2 words seperated by a space like " heated seats" 

if i use words that have no spaces like "ABS" or "airconditioning"  then the key is set and will return  ["ABS"]=> string(1) "1"

When i recreate the above scenaria in plain PHP, it works without errors.  
i have no idea why this is not working in codeigniter?  anyone any idea?
Reply
#2

You will need to put your options in a quoted string. Or generate slugs that change spaces into hyphens (-).

<input name="options['<?= $row ?>']" value ="1" type="checkbox">
Reply
#3

(02-05-2020, 11:04 AM)jreklund Wrote: You will need to put your options in a quoted string. Or generate slugs that change spaces into hyphens (-).

PHP Code:
<input name="options['<?= $row ?>']" value ="1" type="checkbox"

I tried this, but it doenst work either. 
it returns :

array(1) { [0]=> string(1) "1" }, even for the keys that have no spaces in them like the word "ABS"

the only thing that seems to work is 


PHP Code:
<input name="options[<?= $row ?>]" value ="1" type="checkbox"

and this only works if there is no space in the key.  

Reply
#4

If a checkbox is not checked then it has no value and is empty.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 02-06-2020, 11:13 AM by jreklund.)

Personally I have never tried making an options array like that. I always put my value in value="" that I want to be added to the options array.

PHP Code:
<?php if( !empty($groups['department']) ): ?>
<div class="form-group">
    <?php echo form_label(lang('groups_type_department'),'documents_department[]'); ?>
    <p class="help-block"><?php echo nl2br(lang('documents_department_help')); ?></p>
    <?php foreach($groups['department'] as $group_id => $group_name): ?>
    <div class="checkbox">
        <label>
            <?php echo form_checkbox('documents_department[]'$group_idset_checkbox('documents_department[]'$group_idin_array($group_id$groups_checked))) . $group_name?>
        </label>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>

And grabbing it like this, to retrieve the actual value.
PHP Code:
public function _get_documents_department()
{
    return 
$this->input->post('documents_department') ? $this->input->post('documents_department') : array();


In case I want to have the name and ID i put value="key_value", and make an explode('_', $array); on the other end. If I for some reason don't want to use multiple names for different settings.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB