Welcome Guest, Not a member yet? Register   Sign In
problems in insert checkbox value and corresponding text value.
#2

What is throwing you off might be a missing piece of knowledge about checkboxes. If a checkbox is unchecked when its form is submitted, then no value is submitted to the server to represent its unchecked state and the value is not submitted to the server at all.

In other words, only checked boxes are sent to the server.

In the screenshot you attached there are five checked boxes. If you get the posted values based on that screenshot with this code
PHP Code:
$certids $this->input->post('certid');
$nocs $this->input->post('noc1'); 
Then the $certids array will only have five items, but the $nocs array will have seven items.

You need a way to figure out which text input is linked to the posted checkboxes. To do that use the checkbox value attribute. The checkbox value is never seen on the client-side, but on the server this is the value given to the data submitted with the checkbox's name. By adding a value to each checkbox that represents the index in the $certids array you can determine which text input is linked to each checkbox.

(Interesting extra info: If you don't supply a checkbox value then the data submitted with a checked box will be the string "on".)

For this demo, I'm changing the text input values so that it is clear that we are getting the correct text input linked to each checkbox. Here's that code.

PHP Code:
<input type="text"  name="noc1[]" id="eprimaryincome1" size="2" value="850"  
<
input type="text"  name="noc1[]" id="eprimaryincome2" size="2" value="100"  
<
input type="text"  name="noc1[]" id="eprimaryincome3" size="2" value="200"  
<
input type="text"  name="noc1[]" id="eprimaryincome4" size="2" value="300"  
<
input type="text"  name="noc1[]" id="eprimaryincome5" size="2" value="400"  >
<
input type="text"  name="noc1[]" id="eprimaryincome6" size="2" value="500"  >
<
input type="text"  name="noc1[]" id="eprimaryincome7" size="2" value="600"  

Change the checkbox inputs to this.
PHP Code:
<input type "checkbox" id="mycheck1" name="certid[]" value="0">
<
input type "checkbox" id="mycheck2" name="certid[]" value="1">
<
input type "checkbox" id="mycheck3" name="certid[]" value="2">
<
input type "checkbox" id="mycheck4" name="certid[]" value="3">
<
input type "checkbox" id="mycheck5" name="certid[]" value="4">
<
input type "checkbox" id="mycheck6" name="certid[]" value="5">
<
input type "checkbox" id="mycheck7" name="certid[]" value="6"

Now consider this code.

PHP Code:
$certids $this->input->post('certid');
$nocs $this->input->post('noc1'); 
var_dump($certids);
var_dump($nocs); 

If you check all boxes except the third and sixth (like your screenshot shows) this is the var_dump output.

Quote:array (size=5)
 0 => string '0' (length=1)
 1 => string '1' (length=1)
 2 => string '3' (length=1)
 3 => string '4' (length=1)
 4 => string '6' (length=1)

array (size=7)
 0 => string '850' (length=3)
 1 => string '100' (length=3)
 2 => string '200' (length=3)
 3 => string '300' (length=3)
 4 => string '400' (length=3)
 5 => string '500' (length=3)
 6 => string '600' (length=3)

Now it is easy to link a checked box with its text input. Simply use a value from a $certids item as the index into $nocs. For instance, you can print out the text values linked to each checked box like this.

PHP Code:
foreach($certids as $selected)
{
   echo "certid[".$selected."] = ".$nocs[$selected]."<br>"; 


The screen output will be.
Quote:certid[0] = 850
certid[1] = 100
certid[3] = 300
certid[4] = 400
certid[6] = 600

I hope this is clear and you can use the ideas here to construct the data structure you need to save to the database. I'd offer more concrete code for that, but I'm not at all sure exactly what you are trying to save. An explanation of the table schema would help my understanding.
Reply


Messages In This Thread
RE: problems in insert checkbox value and corresponding text value. - by dave friend - 04-13-2019, 07:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB