How to get two id values from one select dropdown with PHP
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..
Solution 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>