Welcome Guest, Not a member yet? Register   Sign In
Retrieve a specific value from option value populated from database
#1

[eluser]rainfall[/eluser]
Earlier i worked with a dropdown that shows id in a text box from selected name ..

But now i retrieve both id and name in option value . and save separately in database using PHP explode function ..
Code:
<option value="&lt;?php echo $row->branch_name. '|'. $row->branch_id;?&gt;">  &lt;?php echo $row->branch_name ?&gt; </option>

Code earlier used for display id in a text box below

in View
Code:
echo '<option value="'.$row->emp_id.'">'.$row->employee_name.'</option>';
Jquery In view
Code:
[removed][removed]
[removed]
$(document).ready( function() {
    $('#empName').change( function() {
        // enter in an empty field code of the selected bank
        $('#empId').val( $(this).val() );
    });
});
[removed]
Its only works for only a single value in option

Now if I want to show only branch_name or brach_id in a text box below what to do in Jquery ??
#2

[eluser]Tpojka[/eluser]
You need foreach loop that will iterate and echo all options

Code:
// assuming you pass $options to view
echo '<select name="nameOfSelect">';
foreach ($options as $option)
{
  echo '<option value="' . $option['id'] . '">' . $option['name'] . '</option>';
}
echo '</select>';

You could use form helper too, but this is the way you have shown all options.
#3

[eluser]CroNiX[/eluser]
What is the benefit of storing BOTH the branch name AND the id as the select VALUE? Just store the ID and display the name like Tpojka showed. That's all you'd want to store it in the db anyway (the ID), and then you don't need to explode() it.

I'm not clear on your actual question.

As far as how to get the VALUE or the TEXT using jQuery, you'd just

var text = $("#empName Confusedelected").text();
var value = $("#empName Confusedelected").val();




Theme © iAndrew 2016 - Forum software by © MyBB