I'm trying to populate a datatable data dynamically with a datalist in one column using ajax server request.
From Php
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-clear btn-clear-datalist-dropdown"> <i class="fa fa-trash-o text-primary "></i></button>
</span>
<datalist id="dtlstTransferAcc" class="dropdown-menu-byval div-GL-list div-input-transferacc-list">
<option class="dropdown-item" data-id="0" value="Enter Input Tax Account keyword in the search box and press Enter">
<!-- dynamic list on ENTER keys-->
</datalist>
From the dynamic datalist I am able to send another ajax server request and populate the datalist options successfully.
Javascript
// target the datalist in same table row
var div_dropitem_list = $(this).closest('.input-datalist-dropdown').find('.div-GL-list');
$.ajax({
type: "POST",
url: "js/gl.php",
data: {
'a': 'GL-DATA-LIST',
'keyword': $(this).val(),
},
success: function(data) {
console.log(data);
$(div_dropitem_list).html(data);
}
});
Console log confirm the ajax data and the datalist options are populated.
However the datalist popup is not showing with the options dynamic data
You should specify the value attribute on each option in the datalist. That attribute is used to search in the list as well as providing a value to be selected in the input which is likely will be sent to the server.
In your case, you should have options with a value attribute, for example: <option value="4316 | Exchange" data-id="985" class="dropdown-item cursor-pointer">
Here's a simple demo:
<!-- to link an input with a datalist, the input must have a "list" attribute pointing to the datalist having the same "id" -->
<!-- in other words, input "list" attribute must be the same as datalist "id" -->
<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
<option value="Chocolate">
<option value="Coconut">
<option value="Mint">
<option value="Strawberry">
<option value="Vanilla">
</datalist>
the above demo was taken from <datalist> Docs on MDN which i recommand taking some time there to learn more about datalist element.
Related
I am getting City List From Database and I Want to Show the Selected City Selected While Edit Operation.
I am Using Ajax for Getting a value from Database.
That's Work Nicely. I get the Response Data From My Controller.
Now The Problem is I am not Able to set a value on selectpiker.
I get City Name In Ajax Response.
I have tried Some Example but did not work.
Here is the Ajax Code.
$.ajax({
type: "GET",
url: urlForGetCityList,
dataType: 'Json',
success: function(response){
console.log(response.cityName);
// This is What i Tried For getting SeletBox Selected.
var $select = $('#city');
$select.val(response.city).trigger('change');
// I have also Tried Some Selct2 Option for it. But it didn't work.
$("#city").select2("val", response.city);
// And at Last I have tried for like simple select box.
$("#city").val(response.city);
});
None of this work properly.
Here is the HTML Code of Selectpiker.
<div class="col-md-4">
<div class="form-group">
<label for="field-3" class="control-label">City *</label>
<select class="selectpicker" data-live-search="true" data-style="btn-white" name="city" id="city"
title="Select City">
<option value="city1">City 1</option>
<option value="city2">City 2</option>
</select>
</div>
</div>
You Already Reach to the half you're a problem is just this select piker is not showing the selected value because it's that way you should check this.
So Try This.
$('select[name=city]').val(response.city);
$('.selectpicker').selectpicker('refresh');
I have an HREF with various data-attributes (about 8) that come from a PHP query and loaded into a table. When you click the button, it takes the data-attributes via jQuery and sends them to a modal window, where I can use them in a form.
I can open the window and place the attributes into an INPUT field with no problem.
What I would like to do is place the attribute in the first OPTION in a SELECT dropdown.
Here is the button with the attributes:
<tr><td><a href='' class='modAccount'
data-modcode=".$row[partner_code]."
data-modname=\"".$row[partner_name]."\"
data-boiler=\"".$row[boilerplate]."\"
data-surcharges=\"".$row[no_new_surcharges]."\"
data-ccalog=\"".$row[cca_logistics]."\"
data-toggle='modal'>Click</a></td></tr>
I have quite a few data-attributes. I had to eliminate a few for this question.
Here is the jQuery used to retrieve the data-attributes and send them to a modal window, called modifyModal.
Here is the jQuery:
$(function()
{
$('.modAccount').click(function(e)
{
e.preventDefault();
$partnerCode = $(this).attr('data-modcode');
$partnerName = $(this).attr('data-modname');
$boiler = $(this).attr('data-boiler');
$surcharges = $(this).attr('data-surcharges');
$ccalog = $(this).attr('data-ccalog');
// the 5 below currently populate the INPUT fields for the ID's listed
$('#modpartnerCode').val($partnerCode);
$('#modpartnerName').val($partnerName);
$('#modplatform').val($platform); // <- should go to dropdown
$('#modboiler').val($boiler); // <- should go to dropdown
$('#modsurcharges').val($surcharges); // <- should go to dropdown
});
});
$(document).on("click", ".modAccount", function ()
{
$('#modifyModal').modal('show');
});
As you've probably read in the note above, I can send the attributes to the INPUT fields with their respective IDs.
I need to be able to populate the first OPTION of a dropdown SELECT field.
I don't think I need to show the MODAL window code. As stated, I can populate the INPUT fields using the ID's I listed.
How can I get the IDs to populate the dropdown SELECT first OPTION value?
Here is the code for the modal:
<div class="modal fade" id="modifyModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
</div>
<div class="modal-body">
<form role="form" action="api/modifyAcct.php" method="get" id="modAcctForm" name="modAcctForm">
<input readonly type="text" class="form-control" id="modpartnerName" name="modpartnerName" />
<input readonly type="text" class="form-control" id="modpartnerCode" name="modpartnerCode" />
<select class="form-control" id="modboiler" name="modboiler" />
<option value=""></option> // <- #modboiler goes here
<option value="Y">Yes</option>
<option value="N">No<option>
</select>
<select class="form-control" id="modsurcharges" name="modsurcharges" />
<option value=""></option> // <- #modsurcharges goes here
<option value="Y">Yes</option>
<option value="N">No</option>
</select>
<select class="form-control" id="modplatform" name="modplatform" />
<option value=""></option> // <- #modplatform goes here
<option value="Y"></option>
<option value="N"></option>
</select>
</div>
</div>
</div>
</div>
Use jquery to select the first option and set the value.
$('#modplatform option:first').val($platform);
$('#modboiler option:first').val($boiler);
$('#modsurcharges option:first').val($surcharges);
Im trying to get some values of my dropdown in php. Its in div, so how can I do that?
<div class="ui dropdown selection" tabindex="0" style="right: 120px;">
<div class="default text" name="dif" id="dif">Difficulty</div>
<i class="dropdown icon"></i>
<div class="menu" id="difficulty" name="difficulty">
<div class="item" value='0' data-value="0">Easy</div>
<div class="item" value='1' data-value="1">Normal</div>
<div class="item" value='2' data-value="2">Hardcore</div>
</div>
</div>
For example, if user select "Easy", I will get with php the value 0.
I tried:
$difficulty = ($_POST['difficulty']) ? $_POST['difficulty'] : '0';
You can access them in jQuery or Javascript:
http://robertnyman.com/2010/04/29/using-html5-custom-data-attributes-to-store-data-on-html-elements/
You could store the value in a hidden form and field and use .submit()
http://api.jquery.com/submit/
And then check for your post value. Divs do not store values, they are containers so adding 'value=0' will not work. But you can access attributes using JS or jQuery.
Edit: IGNORE BELOW I just saw your edit.
If you're using PHP, you can POST the data to another page.
<form action="process.php" method='post'>
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Then on the process.php page you can access the value with the superglobal: $_POST['cars']
You can't get them from a div with just PHP. I'm guessing that there is some Javascript that is updating a hidden input somewhere on the page when the user selects an option from this stylized dropdown list. You'll need to find that hidden input, and that's where your data is coming from when the form is submitted.
I have the simplest possible form, with one select with 3 options and a submit button. The problem is that when I click the submit button the selection in the select element, returns to the first option. And when I do the var dump of POST variable on the page that is the form handler, I get the string that is the first option. Why is this happening? What do I need to do? I have a bootstrap modal window on another page, with two select elements and it works fine there?
Here is the html code:
<form class="navbar-form navbar-center" method = "POST" action="sortiranje.php" enctype="multipart/form-data">
<select class="cities typeahead form-control" id="kriterijum" name="kriterijumSortiranja">
<option value=""></option>
<option value="CenaR">Cena rastuće</option>
<option value="CenaO">Cena opadajuće</option>
<option value="Tip">Tip</option>
</select>
<button type="submit" class="btn btn-default navbar-btn">Sortiraj</button>
And in sortiranje.php there is only:
<?php
var_dump($_POST['kriterijumSortiranja']);
?>
And the result is:
string '' (length=0)
this weekend i've been trying to use this script To create dependable menus.
It consists of an sql table with three rows: "ID, Master, Name" It later grabs the entries that contain 0 as the "master" and will use the resulting data to populate the first option list
To populate the next selection lists from the database, it uses a combination of the following JS and php:
and the rest of the select lists will populate accordinly.
The problem that i'm having is that After it populates the select lists I would like to have the visitors of the website hit a seach button to perform a search based on the data collected. The problem is that when I submit the form it sends the info stored in the "master" row of the database instead of the info on "name"
I'm Getting
index.php?genre=1&fruit=37&colour=39
Instead of
index.php?genre=Male&fruit=Strawberry&colour=Red
I tried to switch '.$row['name'].' to '.$row['id'].
But that was a no go, I also tried to only use '.$row['id'].' and it just messed up with the forms. Is there anyway I can accomplish what i'm looking for so that i can send the values selected on the fields to the url?
Thanks in advanced for any help on this one.
The behavior that you mentioned is normal as submitting a form automatically sends the value, instead of the text, of the selected option. The switch that you mentioned ('.$row['name'].' to '.$row['id'].)should work fine. If it is messing up the forms, please provide more information on what you mean by messing up the forms.
Otherwise, here is a possible solution. It's not the most elegant solution and is probably best suited for simple forms that do not require further complexities but basically, generate the querystring and redirect manually. This is based on the original example that you linked to at http://www.ssdtutorials.com/tutorials/series/dependable-dropdown.html.
JS:
var formObject = {
run: function (obj) {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
var id = obj.attr('id');
var v = obj.val();
jQuery.getJSON('http://jquery-dependable-dropdown.ssdtutorials.com/mod/update.php', {
id: id,
value: v
}, function (data) {
if (!data.error) {
obj.next('.update').html(data.list).removeAttr('disabled');
} else {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
}
});
}
};
$(function () {
$('.update').live('change', function () {
formObject.run($(this));
});
$('#submitButton').click(function () {
window.location.href = 'test.php?gender=' + $('#gender').find(':selected').text() + '&category=' + $('#category').find(':selected').text() + '&colour=' + $('#colour').find(':selected').text();
});
});
HTML:
<div id="wrapper">
<form id="theForm" action="" method="post">
<select name="gender" id="gender" class="update">
<option value="">Select one</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
<select name="category" id="category" class="update" disabled="disabled">
<option value="">----</option>
</select>
<select name="colour" id="colour" class="update" disabled="disabled">
<option value="">----</option>
</select>
<input type="button" id="submitButton" value="submit">
</form>
http://jsfiddle.net/BUJnf/1/
Hope that helps a bit!