0

How can I have to values at once from select options through JavaScript function?

Here is my code:

<select name="ledger" id="ledger" onchange="setDebit(this.value)" required>
    <option value="" >Select</option>
    <?php
        $ledgerResult = $this->db->query('SELECT * FROM ledger WHERE account_type = "Creditors" ORDER BY name');
        $ledgerData = $ledgerResult->result();

        for ($c = 0; $c < count($ledgerData); ++$c) {
                    $ledger_id = $ledgerData[$c]->id;
                    $ledger_name = $ledgerData[$c]->name;
                    $ledger_credit = $ledgerData[$c]->credit; ?>

        <option value="<?php echo $ledger_id;?>"><?php echo $ledger_name;?></option>
        }
</select>

<script>
    function setDebit(ele){
        document.getElementById("set_debit").value = ele;
    }
</script>

I am getting $ledger_id and sending this value through setDebit() to the script. But what I need is to send $ledger_credit. I can do it by setting it as option value instead of $ledger_id; but I also need value of selectas $ledger_id.

How can I set $ledger_id as option value, but send $ledger_credit through setDebit(this.value)?

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Saud
  • 17
  • 1
  • 8
  • what did you mean by "get two values"? You want to select *mutiple*? – donald123 Dec 21 '16 at 10:13
  • CONCAT both $ledger_id.":".$ledger_credit in the option value, then split it with javascript, e.g. var.split(":") – JustBaron Dec 21 '16 at 10:14
  • No, I need to set option value as $ledger_id, which I have done. But at the same time I need to send $ledger_credit through setDebit() function. – Saud Dec 21 '16 at 10:16

4 Answers4

1
    <option value="<?php echo $ledger_id.":".$ledger_credit;?>"><?php echo $ledger_name;?></option>

    function setDebit(ele){
            var Value = document.getElementById("ledger").val();
            var Parts = Value.split(":");
            var LedgerID = Parts[0];
            var LedgerCredit = Parts[1];
    }
JustBaron
  • 2,319
  • 7
  • 25
  • 37
0

You have to set the attribute multiple in the HTML part: http://www.w3schools.com/tags/att_select_multiple.asp

After that you can have a look at this question for further info: How to get all selected values of a multiple select box using JavaScript?

Community
  • 1
  • 1
ShadowBeast
  • 135
  • 1
  • 2
  • 6
  • No you didn't get the point. I will select an option, i.e., $ledger_name. And then, option value will be set as $ledger_id, and setDebit(this.value) function value will be set as $ledger_credit. – Saud Dec 21 '16 at 10:21
  • Oh I understand, maybe you should think about just sending the ID and converting it back to the credit on the server side(using an associative array or database query in the PHP code), things on the client side can easily be spoofed – ShadowBeast Dec 21 '16 at 10:31
0

You can add an atribute data-credit in yours options

<option value="<?php echo $ledger_id;?>" data-credit="<?php echo $ledger_credit;?>"><?php echo $ledger_name;?></option>

And in the setDebit function:

var ledger_credit = $('option:selected', "#ledger").data('credit');

You must use jquery in this solution

Aminesrine
  • 2,082
  • 6
  • 35
  • 64
0

If you are not using jquery.

<option value="<?php echo $ledger_id."||".$ledger_credit; ?><?php echo $ledger_name;?> </option>
 <script>
   function setDebit(){
     var details = document.getElementById("ledger").value;
     var allData = details.split("||");
     var ledger_id = allData[0];
     var ledger_credit = allData[1];
     console.log(ledger_id);
     console.log(ledger_credit);
}
</script>
Amruth
  • 5,792
  • 2
  • 28
  • 41
  • Its not working. details variable is not getting the expected value from ledger. – Saud Dec 21 '16 at 10:47
  • are you using jquery? – Amruth Dec 21 '16 at 10:48
  • No. I m using straight javascript. – Saud Dec 21 '16 at 10:54
  • ok now check and no need to pass parameter in onchange function. just use onchange="setDebit()" – Amruth Dec 21 '16 at 10:55
  • Thank u so much. Ur solution helped me a tons, dude. But one more thing which I m not clear about. – Saud Dec 21 '16 at 11:00
  • Setting option value like this: means I m setting two values which I will split later. But, which value I m passing through though ledger to database? $ledger_id or $ledger_credit? I can see the 1st one. Why its passing only the 1st one rather than both or the 2nd one? – Saud Dec 21 '16 at 11:05
  • you can pass ether ledger_id or ledger_credit. its depends on you which value you are going to send. can you show me how you are sending values – Amruth Dec 21 '16 at 11:08
  • I just catching the value like this: $ledger_id = $this->input->post('ledger'); – Saud Dec 21 '16 at 11:12
  • ok then in php code also you can split and take the values. as you done in your javascript function – Amruth Dec 21 '16 at 11:13
  • Oh got it. Thank you again so much dude. – Saud Dec 21 '16 at 11:25
  • @Saud you are most welcome. can you please upvote this answer if working – Amruth Dec 21 '16 at 11:27
  • 1
    I tried from the very beginning dude. Its restricted. I hope you appreciate it. – Saud Dec 21 '16 at 12:29