Jump to content

Recommended Posts

On 6/28/2022 at 6:08 AM, Barand said:

Use data attributes for the options

<?php 

$data = [
            [ 'id' => 1, 'rifleName' => '.308 Bolt action', 'em' => '225', 'gm' => '1350' ],
            [ 'id' => 2, 'rifleName' => '7mm magnum',       'em' => '300', 'gm' => '1575' ],
            [ 'id' => 3, 'rifleName' => '.243 LeverAction', 'em' => '215', 'gm' => '8725' ]
        ];

    
?>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type='text/javascript'>
    $().ready(function() {
        
        $("#menu").change( function() {
            let em = $(this).find("option:selected").data("em")
            let gm = $(this).find("option:selected").data("gm")
            $("#em").val(em)
            $("#gm").val(gm)
        })
    })
</script>

<select class='nameItems' id="menu" name="selection">
    <option value="">Choose a Rifle</option>
  <?php 
        foreach ($data as $row) {
            echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . htmlspecialchars( $row["rifleName"] ) . "</option>";
        } 
   ?>
</select>

em:<input type="text" class="charge-type" name="em" id="em" value="0" disabled size="5"/>

gm: <input class="charge-type" name="gm" id="gm" value="0" disabled size="5"/>

image.png.0c0d734b2784de4cef9b755de16d091f.png

Not working for me, Was reading this post but fixing this but not able to do that.

Edited by cyberRobot
Linked to post referred to by OP
Link to comment
https://forums.phpfreaks.com/topic/315005-select-options-using-data-attributes/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.