php - How to get value from selected icon dropdown?
Get the solution ↓↓↓I have 3 icons. i.eicon-star
,icon-check
,icon-info
. ificon-start
selected, then i want to store the value of the selected icon into database. for example:
icon-start
withvalue="star"
icon-check
withvalue="complete"
icon-info
withvalue="important"
So, the value stored into database is"star"
because i chooseicon-star
.
Where i must put the value? and how?
Here my modal:
<form autocomplete="off" novalidate action="<?= base_url('app/admin/to-do-add')?>" method="POST">
<input type="hidden" name="<?=$this->security->get_csrf_token_name();?>" value="<?=$this->security->get_csrf_hash();?>">
<div class="btn-group dropup dropdown-icon-wrapper mr-1 mb-1" style="float:right">
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" name="status">
<i class="feather icon-star dropdown-icon"></i>
</button>
<div class="dropdown-menu">
<span class="dropdown-item">
<i class="feather icon-star" data-toggle="tooltip" data-placement="left" title="Starred" value="star"></i>
</span>
<span class="dropdown-item">
<i class="feather icon-info" data-toggle="tooltip" data-placement="left" title="Important" value="important"></i>
</span>
<span class="dropdown-item">
<i class="feather icon-check" data-toggle="tooltip" data-placement="left" title="Completed" value="complete"></i>
</span>
</div>
</div>
<br>
<br>
<div class="form-group">
<label>To</label>
<div class="controls">
<select class="select2 form-control" name="to">
<option value="<?php echo $dataLogin->MEmployee_ID;?>">-SELECT-</option>
<?php foreach ($data1 as $row){ ?>
<option value=" <?php echo $row->MEmployee_ID ?>">
<?php echo $row->EmployeeName?>
</option>
<?php } ?>
</select>
</div>
</div>
<div class=" modal-footer">
<button type="submit" class="btn btn-primary"><?= $this->lang->line('add');?></button>
</div>
</form>
Here my controller :
public function addTask(){
var_dump($this->input->post('status')); die(); //i tried to var_dump, the value is NULL
$dataLogin = $this->employee_model->checkUserData($this->session->userdata('islogin'))->row();
$data = [
"MEmployee_ID" => $this->input->post('to'),
"status" => $this->input->post('status') //here the value of selected icon
];
}
Answer
Solution:
I am proposing a solution and it will work because I did it same and it worked.
- (View) Take three checkbox input for your three icons
<input type="checkbox" name="start" value="start">
<input type="checkbox" name="complete" value="complete">
<input type="checkbox" name="important" value="important">
Answer
Solution:
I think you should use a "select" instead of a "dropdown" because a dropdown is not an input. And according to ww3school (https://www.w3schools.com/html/html_form_elements.asp)
The select element defines a drop-down list.
The option elements defines an option that can be selected.
I suggest you try this code:
<?php echo form_open('app/admin/to-do-add'); ?>
<input type="hidden" name="<?=$this->security->get_csrf_token_name();?>" value="<?=$this->security->get_csrf_hash();?>">
<select class="custom-select mb-3" name="status">
<option selected disabled>-- Status--</option>
<option value="star"><i class="feather icon-star" data-toggle="tooltip" data-placement="left" title="Starred" ></i></option>
<option value="important"><i class="feather icon-info" data-toggle="tooltip" data-placement="left" title="Important"></i></option>
<option value="complete"><i class="feather icon-check" data-toggle="tooltip" data-placement="left" title="Completed"></i></option>
</select>
<div class="form-group">
<label>To</label>
<div class="controls">
<select class="select2 form-control" name="to">
<option value="<?php echo $dataLogin->MEmployee_ID;?>">-SELECT-</option>
<?php foreach ($data1 as $row){ ?>
<option value=" <?php echo $row->MEmployee_ID ?>"><?php echo $row->EmployeeName?></option>
<?php } ?>
</select>
</div>
</div>
<div class=" modal-footer">
<button type="submit" class="btn btn-primary"><?= $this->lang->line('add');?></button>
</div>
<?php echo form_close(); ?>
Answer
Solution:
You can achieve this by making ahidden input field
and changing itsvalue
every time an icon isclicked
. Here I'm usinghidden field name="icon"
, you can get it in yourcontroller
by$this->input->post('icon');
, its default value is empty, you can change it to however you want.
let icon_node = document.querySelectorAll('.feather'); // get all the elements with feather class(icons)
var icon_arr = [...icon_node]; // converts NodeList to Array
icon_arr.forEach(icon => {
icon.addEventListener('click', item => { // add a click event on all elements with feather class
let curr_value = icon.getAttribute("value"); // get value from value attribute
document.querySelector('#icon').value = curr_value; // set value of hidden field
console.clear();
console.log(curr_value);
});
});
<div class="dropdown-menu">
<input type="hidden" id="icon" name="icon" value="" /> <!-- hidden field here -->
<span class="dropdown-item">
<i class="feather icon-star" data-toggle="tooltip" data-placement="left" title="Starred" value="star">star</i> <!-- dummy text for test purpose -->
</span>
<span class="dropdown-item">
<i class="feather icon-info" data-toggle="tooltip" data-placement="left" title="Important" value="important">important</i> <!-- dummy text for test purpose -->
</span>
<span class="dropdown-item">
<i class="feather icon-check" data-toggle="tooltip" data-placement="left" title="Completed" value="complete">complete</i> <!-- dummy text for test purpose -->
</span>
</div>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.