-1

I have one dropdown with a foreach loop which will pass to model using post method.

   <div class="form-group" ">
       <select class="form-control" id="rel" name="rl[][rel]" >
          <option></option>
          <?php
            foreach ($relationship as $row) {
                                            ?>
                <option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
                 <?php
                     }
                       ?>
                   </select>
              </div>

and in the model it is getting the proper values using post method. As

$rel = $_POST['rel'];

Here the problem is when the user select one option ,I want to get two values like

 "<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"

like

$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID

I want to get both from one selection without adding any visble dropdown or element..

coder_000_
  • 99
  • 1
  • 8
  • 3
    Cant you just use an associative array to store $rows and then use the id to get the according category like: `$rows[$relId}->relCatID`. Otherwise just seperate the values by comma and pass them as one string and explode it in php: `list($relId, $relCat) = explode(",", $_POST["rel"]);` – Code Spirit Jan 21 '22 at 10:03
  • 1
    Just querying the `relcat` based on `relId`, like @CodeSpirit says, seems best. Otherwise how are you planning to handle a case where someone alters the request so that the `rel` and `relCat` do not mach with `$row->relID` and `$row->relCatID`. – Hendrik Jan 21 '22 at 10:21
  • 1
    Does this answer your question? [Can an Option in a Select tag carry multiple values?](https://stackoverflow.com/questions/3245967/can-an-option-in-a-select-tag-carry-multiple-values) – CBroe Jan 21 '22 at 10:38

1 Answers1

1

Try bellow code with ajax call

In Javascript code get value

var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
  <select class="form-control" id="rel" name="rl[][rel]" >
    <option></option>
    <?php foreach ($relationship as $row) { ?>
      <option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
    <?php } ?>
  </select>
</div>