0

I want to submit a form which posts the input text field when the current line checkbox is checked.

When I submit, I get the productID of the checked box but all of the productLink. I want to only get the input text of the corresponding checked box.

How do I go about this?

<form action="process.php" method="post">
    <table>
        <tr> 
            <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
        </tr><tr>
            <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
        </tr><tr>
            <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
        </tr><tr>
            <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
        </tr><tr>
            <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]"  /></td>
        </tr>
    </table>
    <input type="submit" name="formSubmit" value="Submit" />
</form>
Anfath Hifans
  • 1,588
  • 1
  • 11
  • 20
  • you cant. since procuctLink is an array you will get all of them.Anyway, you will receive everything that's inside the form. You just need to filter/select the things you need. You can check which checkbox is set to true, then get the corresponding input. – Jeff Apr 22 '18 at 01:31
  • 1
    Link them with same key -> `name="productId[1]"`/`name="productLink[1]"`. Then on post, you can loop through the checkbox keys to get only the input key values – Sean Apr 22 '18 at 01:32
  • My previous answer perhaps should have been `` (notice the extra array at the beginning to allow grouping of input.) – Tim Morton Apr 22 '18 at 01:48

3 Answers3

1

You can find a lot of solution, so in my opinion you should look this one

HTML Element Array, name="something[]" or name="something"?

You can't this way, but there are some alternative method for example, when user check to checkbox then you could call a javascript function create an array and send json stringify data your php.

another way like below;

in your html

<form  method="post">
 <table>
   <tr>
    <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
   </tr>
   <tr>
    <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
   </tr>
   <tr>
    <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
   </tr>
   <tr>
    <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
   </tr>
   <tr>
    <td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
   </tr>
  </table>
  <input type="submit" name="formSubmit" value="Submit" />
</form>

in your php (suppose in codeigniter because you tagged it)

function some_func_name(){
  $product_ids = $this->input->post('productId');
  $posted_product_links = $this->input->post('productLink');
  $selected_links = [];
  for($product_ids as $id){
    $p_link = $posted_product_links[$id];
    array_push($selected_links, array('id' => $id, 'link' => $p_link));
  }
}

I hope this may help you..

Phd. Burak Öztürk
  • 1,727
  • 19
  • 29
0

update your by following codes

HTML

<form action="process.php" method="post">
    <table>
        <tr> 
            <td> 
                <input type="checkbox" name="productId[1]" value="<?=$products->id;?>" <?=  set_checkbox('productId[1]', $products->id);?> />
                <input type="text" name="productLink[1]" value="<?=  set_value('productLink[1]');?>"/>
            </td>
        </tr><tr>
            <td>
                <input type="checkbox" name="productId[2]" value="<?=$products->id;?>" <?=  set_checkbox('productId[2]', $products->id);?> />
                <input type="text" name="productLink[2]" value="<?=  set_value('productLink[2]');?>" />
            </td>
        </tr><tr>
            <td>
                <input type="checkbox" name="productId[3]" value="<?=$products->id;?>" <?=  set_checkbox('productId[3]', $products->id);?>/>
                <input type="text" name="productLink[3]" value="<?=  set_value('productLink[3]');?>" />
            </td>
        </tr><tr>
            <td>
                <input type="checkbox" name="productId[4]" value="<?=$products->id;?>" <?=  set_checkbox('productId[4]', $products->id);?> />
                <input type="text" name="productLink[4]" value="<?=  set_value('productLink[4]');?>" />
            </td>
        </tr><tr>
            <td>
                <input type="checkbox" name="productId[5]" value="<?=$products->id;?>" <?=  set_checkbox('productId[5]', $products->id);?> />
                <input type="text" name="productLink[5]" value="<?=  set_value('productLink[5]');?>" />
            </td>    
        </tr>
    </table>
    <input type="submit" name="formSubmit" value="Submit" />
</form>

Controller

public function some_func_name(){
    if($this->input->post()){
        $checkbox = $this->input->post('productId');
        $textbox = $this->input->post('productLink');
        $selected = @array_intersect_key($textbox, $checkbox);

        echo '<pre>';
        print_r($selected);
    }
}

codes here : https://pastebin.com/UC1UFPRF

Anfath Hifans
  • 1,588
  • 1
  • 11
  • 20
0

I ended up using jquery to remove the obj when they were empty.

    $(document).ready(function(){
    $("form").submit(function(){
    $("input").each(function(index, obj){
          if($(obj).val() == "") {
               $(obj).remove();
              }
            });
      });
    });