I have a table like code html belows
I want to store value of check box and corresponding text value into 2 array in PHP
For example
if i check box ID_1, ID_2
the values of these check box are stored in $array_box ( 74, 75)
and values of corresponding text box are stored in $array_text ("iOS","Android")
Do you have any idea? thank you very much

<table width="341" border="1">
  <tr>
    <th width="20" scope="row"><input name="target" type="checkbox" value="74" checked /></th>
    <td width="155">ID_1</td>
    <td width="144"><form id="form1" name="form1" method="post" action="">
      <label>
        <input type="text" name="os[]" id="os" value="iOS" />
      </label>
    </form></td>
  </tr>
  <tr>
    <th scope="row"><input name="target" type="checkbox" value="75" checked /></th>
    <td>ID_2</td>
    <td><form id="form2" name="form1" method="post" action="">
      <label>
        <input type="text" name="os[]" id="os2" value="Andoird"/>
      </label>
    </form></td>
  </tr>
  <tr>
    <th scope="row"><input name="target" type="checkbox" value="76" checked /></th>
    <td>ID-3</td>
    <td><form id="form3" name="form1" method="post"  action="">
      <label>
        <input type="text" name="os3" id="os3" value="Beta" />
      </label>
    </form></td>
  </tr>
  <tr>
    <th scope="row"><input name="target" type="checkbox" value="77" checked /></th>
    <td>ID-4</td>
    <td><form id="form4" name="form1" method="post" action="">
      <label>
        <input type="text" name="os[]" id="os4" value="Window" />
      </label>
    </form></td>
  </tr>
</table>

You have to assign the checkbox also an array to the name target[] and then you will have to parse over $_GET['target'] with different methods.

You can use $elements = count($_GET['target']);

if($elements > 0) {
    for($i = 0; $i <= $elements; $i++) {
        echo $_GET['target'][$i].'-'.$_GET['os'][$i];
    }
}

In this way you are iterating over the arrays and you can do whatever you want with them.

What's happend if i checked ID_1, ID_4 . Do I get $array_box ( 74, 77) and $array_text( iOS , window)

If your checkboxes are there to purely signify that there will be content in a corresponding text field, then I would lose the checkboxes and simply check for form field content in your form handler.

You have too any forms. "There can only be one" (Highlander) :D

commented: But it must be in a Caledonian accent! :) +14
Member Avatar for diafol

The only way I can see to pass two bits of separate info for the checkbox is if you have a name array - storing the key (74 etc) and a value ('Android' etc.) and use one form as mentioned by paulkd.

<form ...>
    ...
    <input name="target[75]" type="checkbox" id="android" value="Android" /><label for="android">Android</label>
    <input name="target[74]" id="ios" type="checkbox" value="iOS" /><label for="ios">iOS</label>
    ...
</form>

I've ignored the textboxes as I can't see how they're being used - if at all.

$array_box = array();
$array_text = array();
foreach($_POST['target'] as $k=>$v){
    $array_box[] = $k;  
    $array_text[] = $v;
}

OR

$array_box = array();
$array_text = array();
if(isset($_POST['target'])){
    $array_box = array_keys($_POST['target']);
    $array_text = array_values($_POST['target']);
}

Not tested.

i am trying to pass a text box value to Db, but here i am also passing some dynamic values,
how can i pass both dynamic and textbox value at a time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.