• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle checkboxes

#1
I have a form that collects a user input to be emailed, including a few checkboxes which I want to attach different .pdfs if they are checked. But I'm not sure how to set up my if statement regarding the checkboxes. I havent worked with them before in codeigniter. Here is my controller function...
Code:
public function response_validation(){
    
    $this->load->library('form_validation');
    $this->form_validation->set_rules('subject', 'Subject', 'required');
    $this->form_validation->set_rules('message', 'Message', 'required');
    $this->form_validation->set_rules('attachments', 'Attachments', 'required');
    $this->form_validation->set_rules('use_email', 'Email', 'required');
    
    if($this->form_validation->run()){
        
        //load email library and model_users
        $this->load->library('email', array('mailtype'=>'html'));
        $this->load->model('model_users');
        
        //build email message
        $this->email->from('email@email.com', "email");
        //$this->email->from($this->input->post('use_email'), "From Me");
        $this->email->to($this->input->post('send_to'));
        $this->email->subject($this->input->post('subject'));
        $message = "<p>".$this->input->post('message')."</p>";
        $message .= "<p>".$this->input->post('signature')."</p>";
        $this->email->message($message);
        


        // attachments
        
        $checked = $this->input->post('attachments');
        if ($checked->nui == 1){
        //echo 'Nui has been checked';
        $this->email->attach('/attachments/somefile.pdf');
        }
        
        //
        


        $this->email->send();
        //send email
        
        if ($this->email->send()){
        $this->load->view('response_sent');
        } else {
        echo "There was a problem. Please contact the Administrator.";
        }

    } else {
        $this->load->view('main/request_details/$r->Submitted');
    }
    
   }
 and here is how I have my checkboxes in my view. The form submits fine, the problem is I just dont know how to access the checkbox array in the controller...
Code:
<td>
    <input type="checkbox" name="attachments[]" value="nui">NUI-IR<br>
   <input type="checkbox" name="attachments[]" value="lidar">LiDAR<br>
   <input type="checkbox" name="attachments[]" value="optical">Optical<br>
   <input type="checkbox" name="attachments[]" value="none">None<br>
</td>
Reply

#2
Inside your controller you can run a loop for every checked checkbox and attach a logical named file for each value like (untested)

PHP Code:
foreach($this->input->post('attachments') as $attachement)
{
    
// $attachement contains for e.g. nui, lidar, etc.
    
$this->email->attach('/attachments/file_'.$attachement.'.pdf');


Reply

#3
Thanks for the reply, I'm not sure I understand though. This is the part I'm having trouble with..

// attachments

$checked = $this->input->post('attachments');
if ($checked->nui == 1){
//echo 'Nui has been checked';
$this->email->attach('/attachments/somefile.pdf');
}

//

I dont know how to access the checkboxes correctly
Reply

#4
PHP Code:
foreach($this->input->post('attachments') as $attachement)
{
    if (
$attachement == 'nui')
    {
        
$this->email->attach('/attachments/somefile.pdf');
    } 
    
// etc.


Reply

#5
I see, thanks.

It seems to be partially working but now when I receive the email it doesn't include the message part of the email like it did before, it just sends the text "--B_ATC_5473a7876fb89--" and the numbers look like they change everytime an email is sent, and there is no attached file
Reply

#6
can you post your new code

Reply

#7
Sure, here is my controller fucntion

public function response_validation(){

$this->load->library('form_validation');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
//$this->form_validation->set_rules('attachments', 'Attachments', 'required');
$this->form_validation->set_rules('use_email', 'Email', 'required');

if($this->form_validation->run()){

//load email library and model_users
$this->load->library('email', array('mailtype'=>'html'));
$this->load->model('model_users');

//build email message
$this->email->from('email@email.com', "Email");
//$this->email->from($this->input->post('use_email'), "Email");
$this->email->to($this->input->post('send_to'));
$this->email->subject($this->input->post('subject'));
$message = nl2br($this->input->post('message'));
$signature = nl2br($this->input->post('signature'));
$message = "<p>".$message."</p>";
$message .= "<p>".$signature."</p>";
$this->email->message($message);

// attachments

foreach($this->input->post('attachments') as $attachement){
if ($attachement == 'nui'){
$this->email->attach('http://www.site.com/wp-content/uploads/2014/07/image1.jpg'); //this is not the actual location of the files, just made up for privacy
}
if ($attachement == 'lidar'){
$this->email->attach('http://www.site.com/wp-content/uploads/2014/07/image2.jpg');
}
if ($attachement == 'optical'){
$this->email->attach('http://www.site.com/wp-content/uploads/2014/07/image3.jpg');
}
// etc.
}

if ($this->email->send()){
$this->load->view('response_sent');
} else {
echo "There was a problem. Please contact the Administrator.";
}

} else {
$this->load->view('main/request_details/$r->Submitted');
}

}




and heres my view...


<td>
<input type="checkbox" name="attachments[]" value="nui">NUI-IR Laser Array Chip<br>
<input type="checkbox" name="attachments[]" value="lidar">LiDAR - IR Laser Array Field<br>
<input type="checkbox" name="attachments[]" value="optical">Optical Solutions<br>
<input type="checkbox" name="attachments[]" value="none">None<br>
</td>
Reply

#8
What i see is that your include path for the attachements are wrong. You must use the server path like

$this->email->attach('/var/www/..../file.pdf');

Reply

#9
I've tried at least 20 different paths and cant seem to get it correct. Not sure if the path would originate where the controller is that runs the code or if it would be somewhere else. I've tried just about everything
Reply

#10
But you know where the files on the server located? You can echo your current directory
Code:
echo getcwd();

In your applications, do you send just images like image1, image2 ... ? If so, this would maybe easier

PHP Code:
<td>
<
input type="checkbox" name="attachments[]" value="1">NUI-IR Laser Array Chip<br>
<
input type="checkbox" name="attachments[]" value="2">LiDAR IR Laser Array Field<br>
<
input type="checkbox" name="attachments[]" value="3">Optical Solutions<br>
<
input type="checkbox" name="attachments[]" value="4">None<br>
</
td

foreach(
$this->input->post('attachments') as $attachement)
{
    
$this->email->attach('/path.../uploads/2014/07/image'.$attachement.'.jpg');


Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.