I recommend not reinventing the wheel and looking at
Reusable-Custom-WordPress-Meta-Boxes
Even though this code is no longer supported it’s concise enough to be easily manageable. I use it and it works well.
All you need to do is add the meta_box.php file to your functions.php and from then on use the predefined components:
require_once('path_to/metaboxes/meta_box.php');
Then use code such as (this is the example from the github page):
$prefix = 'sample_';
$fields = array(
array( // Text Input
'label' => 'Text Input', // <label>
'desc' => 'A description for the field.', // description
'id' => $prefix.'text', // field id and name
'type' => 'text' // type of field
),
array( // Textarea
'label' => 'Textarea', // <label>
'desc' => 'A description for the field.', // description
'id' => $prefix.'textarea', // field id and name
'type' => 'textarea' // type of field
)
);
$sample_box = new custom_add_meta_box( 'sample_box', 'Sample Box', $fields, 'post', true );
There are also other options such as CMB2 (Custom Meta Boxes)
That code appears to have better support but is also much more complex and could be overkill for your needs.