Welcome Guest, Not a member yet? Register   Sign In
Need Assistance with multiple checkbox $_POST issue and submit button.
#1

[eluser]Judgestar[/eluser]
Hi there I should start of by saying I am new to php coding and have learned alot from these forums. I am having an issue with multiple checkbox $_POST data not being sent to my submit button to process the data and hoping a fresh pair of eyes could point to what I might be doing wrong. Ignore some of the commented out code that is me trying to get it to work using a loop.

The view I have works with the checkbox if I were to select one box but not multiple, so I can select any checkbox individually and it works perfectly but not if I try to select multiple checkboxes.

Here is the view for my form:

Code:
<div id='content-main'>
  
   <div id='task'>
   <table>
    
    <tr>
    
     <th>RequestID</th>
     <th>Submitted</th>
     <th>Submitted By</th>
     <th>Agent</th>
     <th>Billing Sys</th>
     <th>Product</th>
     <th>Add/Rem</th>
     <th>Comment</th>
    
    </tr>

    &lt;?php if(!empty($requests)): ?&gt;
    &lt;?php foreach($requests as $r): ?&gt;

     &lt;?php form_open('skills/manage');

      /*$RequestedID = array(
       'name' => 'SkillRequests',
       'id' => $r->RequestID,
       'value' => $r->RequestID,
       'checked' => FALSE
      );*/
     ?&gt;

     <tr>
      <td class="title-short">&lt;?php echo anchor('skills/updateSkill/'.$r->RequestID.'', $r->RequestID); ?&gt;&lt;?php echo form_checkbox($r->RequestID,$r->RequestID); ?&gt;</td>
      <td class="title-short">&lt;?php echo !empty($r->CreatedDate) ? date('D m-d-y', strtotime($r->CreatedDate)) : 'N/A'; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->Supervisor; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->Name; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->BillingSys; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->Product; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->AddRem; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->RequestDescription; ?&gt;</td>

      &lt;?php echo form_close(); ?&gt;
      &lt;!--&lt;?php echo var_dump($RequestedID); ?&gt;--&gt;

     &lt;?php endforeach; ?&gt;

     &lt;!--&lt;?php $rids = array($_POST[$data]);  ?&gt;
      &lt;?php foreach($rids as $r2): ?&gt;--&gt;
       &lt;?php echo form_open('skills/updateSkill/'.$r->RequestID.''); ?&gt;
       &lt;?php echo form_submit('Approve', 'Approve'); ?&gt;
      
       &lt;?php echo form_open('skills/updateSkill/'.$r->RequestID.''); ?&gt;
       &lt;?php echo form_submit('Deny', 'Deny'); ?&gt;
       &lt;?php echo form_close(); ?&gt;
      &lt;!--&lt;?php endforeach; ?&gt;--&gt;

    &lt;?php endif; ?&gt;
    
   </table>

    &lt;?php echo form_open('skills/updateSkill/'.$r->RequestID.''); ?&gt;
    &lt;?php echo form_submit('Approve', 'Approve'); ?&gt;
    
    &lt;?php echo form_open('skills/updateSkill/'.$r->RequestID.''); ?&gt;
    &lt;?php echo form_submit('Deny', 'Deny'); ?&gt;
    &lt;?php echo form_close(); ?&gt;
    
   </div>
  
  </div>

Here is my model for this view:

Code:
function getRequest($options = array())
{
  where_fields(array('RequestID', 'RequestStatus', 'CreatedBy', 'ID'), $options);
  $this->db->order_by('RequestID', 'ASC');
  
  $query = $this->db->get('SkillRequestID');
  if($query->num_rows() > 0)
  {
   if(isset($options['RequestID']))
   {
    return $query->row();
   }
   return $query->result();
  }
  return false;
} // END function

function updateSkills($options = array())
{
  foreach($_POST[$options] as $option)
  {
   $sql = "Update AgentSkills set ";
  
   if($option['BillingSys'] == 'trac')
   {
    $sql .= "T";
   } elseif($option['BillingSys'] == 'AC')
   {
    $sql .= "A";
   }
  
   switch($option['Product'])
   {
    case 'NET':
     $sql .= "NET = ";
     break;
    case 'CEL':
     $sql .= "CEL = ";
     break;
    case 'Video':
     $sql .= "Video = ";
     break;
    default:
     break;
   }
  
   if($option['AddRem'] == 'Add')
   {
    $sql .= "1";
   } elseif($option['AddRem'] == 'Rem')
   {
    $sql .= "0";
   }
  
   $sql .= " where ID = {$option['ID']}";
  
   $this->db->query($sql);
  
   $this->approveRequest($option['RequestID']);
  }
} // END function

function denyRequest($options = array())
{
  $this->db->set('RequestStatus', 'Denied');
  $this->db->where('RequestID', $options['RequestID']);
  
  $this->db->update('AgentSkillRequest');
  return $this->db->affected_rows();
} // END function


and this is my controller for this page:

Code:
function updateSkill($id=0)
{
  if(!$this->session->userdata('Manager') == 1 && !$this->session->userdata('Project') == 1)
  {
   redirect('skills');
  }
  
  $data = array();
  
  $this->load->model('skill_model');
  $data['r'] = $this->skill_model->getRequest(array('RequestID' => $id));
  
  if(isset($_POST['Approve']))
  {
   $options = array(
    'BillingSys' => $data['r']->BillingSys,
    'Product' => $data['r']->Product,
    'AddRem' => $data['r']->AddRem,
    'ID' => $data['r']->ID,
    'RequestID' => $id
   );
  
   $this->skill_model->updateSkills($options);
   redirect('skills/manageRequest');
  }
  
  if(isset($_POST['Deny']))
  {
   $options = array(
    'RequestID' => $id
   );
  
   $this->skill_model->denyRequest($options);
   redirect('skills/manageRequest');
  }
  
  $this->load->view('template/header', $data);
  $this->load->view('skills/detail', $data);
  $this->load->view('template/footer', $data);
}

like I said it works when I select one checkbox but not multiples, I tried a few loops and they did not work for me (probably did not do it correctly) as I said still new to php and codeigniter. Thanks for any help that is given.
#2

[eluser]CroNiX[/eluser]
For one thing, you can't nest forms in HTML, which you have an awful lot of.

Also, you need to use an array for the name of your checkbox field if you are going to use multiple values like that. The way you have it now is will only send one checkbox out of all of them that are checked because they all use the same name and not an array.
Code:
&lt;input type="checkbox" name="mycheckbox[]" value="1" /&gt;
&lt;input type="checkbox" name="mycheckbox[]" value="2" /&gt;
&lt;input type="checkbox" name="mycheckbox[]" value="3" /&gt;

Then you retrieve like:
Code:
$this->input->post('mycheckbox');
if the first and third were checked, but not the 2nd, it would look like:
Code:
array(
0 => 1, //the value of the first checkbox
1 => 3 //the value of the third
)
Any checkboxes that aren't checked don't get sent with the POST request.
#3

[eluser]Judgestar[/eluser]
I did create an array for the name and value so the foreach loop made one for each checkbox. I did a var_dump to see what was in the array and this is what is in it:

array(2) { ["name"]=> string(15) "skillrequests[]" ["value"]=> int(851) }
array(2) { ["name"]=> string(15) "skillrequests[]" ["value"]=> int(852) }
array(2) { ["name"]=> string(15) "skillrequests[]" ["value"]=> int(853) }
array(2) { ["name"]=> string(15) "skillrequests[]" ["value"]=> int(886) }
array(2) { ["name"]=> string(15) "skillrequests[]" ["value"]=> int(887) }
array(2) { ["name"]=> string(15) "skillrequests[]" ["value"]=> int(888) }


I tried the code you suggested for the post portion for the submit button but it went nowhere it just went back to the same page and nothing was processed.

here is the code in the view:
Code:
&lt;?php if(!empty($requests)): ?&gt;
    &lt;?php foreach($requests as $r): ?&gt;

     &lt;?php form_open('skills/manage');

      $RequestedID = array(
       'name' => 'skillrequests[]',
       'value' => $r->RequestID
      );
     ?&gt;

     <tr>
      <td class="title-short">?&gt;&lt;?php echo form_checkbox($RequestedID); ?&gt;</td>
      <td class="title-short">&lt;?php echo !empty($r->CreatedDate) ? date('D m-d-y', strtotime($r->CreatedDate)) : 'N/A'; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->Supervisor; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->Name; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->BillingSys; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->Product; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->AddRem; ?&gt;</td>
      <td class="title-short">&lt;?php echo $r->RequestDescription; ?&gt;</td>

      &lt;?php echo form_close(); ?&gt;
      &lt;?php echo var_dump($RequestedID); ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
    
   </table>

    &lt;?php echo form_open('skills/updateSkill/'.$this->input->post('skillRequests').''); ?&gt;
    &lt;?php echo form_submit('Approve', 'Approve'); ?&gt;
    
    &lt;?php echo form_open('skills/updateSkill/'.$this->input->post('skillRequests').''); ?&gt;
    &lt;?php echo form_submit('Deny', 'Deny'); ?&gt;
    &lt;?php echo form_close(); ?&gt;

What am I doing wrong?




Theme © iAndrew 2016 - Forum software by © MyBB