Question

I have been working on this for the past 4 hours, with problem after problem. Now that I have finally come to the end of most of my problems, I can't seem to get a solution to the final problem.

Problem: I have to send out an email to a student (email is stored in DB). In the email there has to be an un-ordered list with list items. That part was the easy part (html email). The problem that I am now facing is that I want to get the actual name of the form, not the value because the value is just

value="1" value="2"

That won't provide the student with much info to what the forms are.

These are my input fields:

<label id="form"><input type="checkbox" name="form[]" value="3" >DECSP</label>
<label id="form"><input type="checkbox" name="form[]" value="4" >DESNAP</label>

If you notice the value again is just a number, I want to actually get the NAME of the form which DESCP and DESNAP

This is what I tried:

$body .= '<ul>';

$forms = $this->input->post('form'); // The check boxes 
$dbforms = $this->queue_model->first10() + $this->queue_model->elev9() + $this->queue_model->twenty_5() + $this->queue_model->twen8thir7() + $this->queue_model->checklists(); // Here I take the forms from the database and combine the arrays


$givenforms = array_intersect_assoc($dbforms, $forms); // Here I do an intersect where form_id matches the value

foreach ( $givenforms as $key => $value) { // If a match is good then give me the form_name
$body .= '<li>' . $value . '</li>';
}

$body .= '</ul>';

I am quite confused as to where to go from here. Hopefully you guys understand.

The only thing I can possibly think of is to do a

foreach

loop over all the forms that where checked in the check boxes, then in a query

select * forms where form_id = (what form was checked)

then

return

the actual name of the form.

But again, I do not know if this would be a proper solution. Thanks in advance for any information that you guys could pass along.

Edit 1 :

The check boxes have to be an array so that I can store in the database which forms a student received. Take a look at my other question.

Was it helpful?

Solution 3

Well this worked quite well, and is surprisingly quick! I didn't even notice the wait time so thank god to that.

Code for any one else who wants to do the similar :

Input field :

<label id="form"><input type="checkbox" name="form[]" value="4" >DESNAP</label> // Yes I know id should be unique I will change that class asap!

            ...............................................
            $body .= '<ul>';

            foreach ($this->input->post('form') as $form_id) {
                $forms[] = $this->queue_model->getform($form_id);
            }

            foreach ($forms as $givenform) {
                $body .= '<li>' . $givenform . '</li>';
            }

            $body .= '</ul>';
           ............................
           $this->email->message($body);

and the finished result :

enter image description here

OTHER TIPS

There is no way for the PHP to know what the text was that is associated with a given input field. All you receive from the client is a key/value pair (the key is the name portion of the input field and value is the value portion).

As you pointed out, you could query the database again and get the corresponding names.

Alternatively, you could find some other creative ways to pass the names. Such as:

  • Create a hidden input field that contains a string representation of an array, containing the key/name pairs of all input fields.

  • You could use some JS to update a hidden input field with the names of the selected input fields

  • Have a hidden input field for each input field with name="hiddenInput_3" and value="DECSP". Now you can associate the value of the input field with the name of the hidden input field to get the name on the PHP side.

WARNING: Those are just ideas and I would recommend against doing any of the above, as you pass data through the client side, where they could easily be altered. The best approach is certainly to query the db again or store some input array in the session for later use.

You need to change some things here:

<label for="DECSP">DECSP</label><input type="checkbox" name="DECSP" value="3" />

And in the php:

$value = $_POST['DECSP'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top