I have two chosen select box for fetching values from MYSQL DB
When No-1 Select box change then No-2 select box generate from AJAX response.
Chosen Plugin in No-1 work perfectly. But When No-2 select box generate from ajax then chosen plugin doesn't work in No-2 select box.
Main.PHP
<div class="control-group">
<label for="textfield" class="control-label">Category<span>*</span></label>
<div class="controls">
<select name="category" id="category" multiple="multiple" class="chosen-select input-medium" required onchange="showSubCat(this.value)">
<option value='1'>Option 1</option>
<option value='2'>Option 2</option>
</select>
<input type='hidden' value='' id='categoryId' name='categoryId'>
</div>
</div>
<div class="control-group">
<label for="textfield" class="control-label">Sub Category</label>
<div class="controls">
<select name="subCat_2" id="subCat_2" multiple="multiple" class="chosen-select input-large">
<option value="">--- Select Sub Category---</option>
</select>
</div>
</div>
JS CODE :
var xmlhttp;
function showSubCat(str){
if( $('#category :selected').length > 0){
//build an array of selected values
var selectednumbers = [];
$('#category :selected').each(function(i, selected) {
selectednumbers[i] = $(selected).val();
});
}
//alert(selectednumbers);
document.getElementsByName('categoryId')[0].value = selectednumbers;
if (str==""){
document.getElementById("subCat_2").innerHTML="";
return;
}
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("subCat_2").innerHTML=xmlhttp.responseText;
//alert(xmlhttp.responseText);
console.log(xmlhttp.responseText);
}
}
xmlhttp.open("GET","MYurl.php?catId="+selectednumbers,true);
xmlhttp.send();
}
AJAX.PHP
echo "<option value=''>--- Select Sub Category --- </option>";
$sql="SELECT field FROM tableName WHERE condition;
$result = mysql_query($sql) or die('MySql Error' . mysql_error());
while($row = mysql_fetch_array($result)){
echo "<option value='1'> ----- Sub Option 1</option>";
}
Let me know what i have done Wrong..??
Assuming you're calling chosen on the 2nd list, try using
$('#subCat_2').trigger('chosen:updated');
after you update its list.
If you look at plugin page it indicates that
`You can trigger several events on the original select field to invoke a behavior in Chosen.
chosen:updated This event should be triggered whenever Chosen’s underlying select element changes (such as a change in selected options).`
You need to rebuild the selectbox with new data in it.
if (xmlhttp.readyState==4 && xmlhttp.status==200){
// bind new data
document.getElementById("subCat_2").innerHTML=xmlhttp.responseText;
// refresh chosen.
$('#subCat_2').trigger("chosen:updated");
}
Related
I have a we page (index.html), where user can select option from drop down menu. Based on the selection data will be fetched from database and be displayed on the same page using AJAX (second file is called getParameter.php). Everything works fine. Now I tried to add a Button so that a new page will open and user can edit and send new value to database. But the button press does nothing. I tried it with other html files. It works everywhere else. Any help please
Index.html
//AJAX
<script>
function showParameterData(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getParameterData.php?q="+str,true);
xmlhttp.send();
}
}
</script>
//drop down option selection
<body>
//showParameterData() is the function
<form>
<select name="Device" onchange="showParameterData(this.value)">
<option value="">Select</option>
<option value="1">Device A </option>
<option value="2">Device B </option>
<option value="3">Device C </option>
<option value="4">Device D </option>
<option value="5">Device E </option>
<option value="6">Device F </option>
</select>
</form>
//bitton to edit and send data
<div class="edit_button_class" style= "float:right;">
<form action="edit.html" method="get" target="_blank">
<input type="submit" name="submit" id ="mybutton1" value="Click to Edit" />
</form>
</div>
<div id="txtHint"></div>
</body>
//getParameter.php
<?php
$q = intval($_GET['q']);
include ("DBconnect.php");
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db ) or die("Could not connect: " .mysqli_error($conn) );
$sql="SELECT * FROM parameter WHERE ID = '".$q."'";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)) {
$price = $row['price'];
$version = $row['version'];}
echo '<table>';
echo "<tr>
<th>Price of the Device :</th>
<td > $price </td>
</tr>";
echo "<tr>
<th>version of the Device :</th>
<td > $version </td>
</tr>";
echo '</table>';
echo '</div>';
When I press the button URL is changing to something weird. It is not going to edit.html. Even if I refresh the page, the weird URL is still there. Even though it shows index.html page some extra stuff in the URL
The stuff you are seeing in the URL are get parameters. You by using method='get' you are telling the form to submit in this way.
What this will do is send all the names of the inputs and their values into the URL and "attach" them to the action page you have chosen.
For example, you clicking the button will send you to edit.html with the get parameters of submit and the value of this which is "Click to Edit". Anything that has the name attribute will be sent as a get parameter. You dont need a name for a submit input.
If you place your select into the form with the submit button, it will send the parameter for the select and that means you can use it in your edit page.
<div class="edit_button_class">
<form action="edit.html" method="get" target="_blank">
<select name="Device" onchange="showParameterData(this.value)">
<option value="">Select</option>
<option value="1">Device A </option>
<option value="2">Device B </option>
<option value="3">Device C </option>
<option value="4">Device D </option>
<option value="5">Device E </option>
<option value="6">Device F </option>
</select>
<input type="submit" id ="mybutton1" value="Click to Edit" />
</form>
</div>
I removed the name from the input as this causes it to be sent as a GET parameter. Using the above you will be sent to /edit.html?submit=Click+to+Edit which you can then use in your edit page to get what you need.
Hope this helps.
What I am trying to figure out is how to have an html input field appear when the value of other is selected from a dropdown menu. Right now the values for the dropdown list are coming from the results of a MySQL DB query, which works, but I can not seem to figure out how to get an input to appear when I select the other option.
$query = mysql_query("SELECT type FROM Dropdown_Service_Type"); // Run your query
echo '<select name="service_type">'; // Open your drop down box
echo '<option value="NULL"></option>';
// Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['type'].'">'.$row['type'].'</option>';
}
echo '<option value="Other">Other</option>';
echo '</select>';// Close your drop down box
Use javascript, like in the example below. We can add an input field and have it hidden by default, using the style attribute:
<input name='otherInput' id='otherInput' type="text" style="display: none" />
var otherInput;
function checkOptions(select) {
otherInput = document.getElementById('otherInput');
if (select.options[select.selectedIndex].value == "Other") {
otherInput.style.display = 'block';
}
else {
otherInput.style.display = 'none';
}
}
<select onchange="checkOptions(this)" name="service_type" id="service_type">
<option value="NULL"></option>
<option value="43">43</option>
<!-- other options from your database query results displayed here -->
<option value="Other">Other</option>
</select>
<!-- the style attribute here has display none initially, so it will be hidden by default -->
<input name='otherInput' id='otherInput' type="text" style="display: none" />
There are 3rd party libraries like jQuery, AngularJS, PrototypeJS, etc., which can be used to make the code simpler by adding shortcut methods for DOM manipulation (though you should read this post). For example, with jQuery, using .on() (for the event handler binding), .show() and .hide() for the input display toggling, etc:
var otherInput;
var serviceTypeInput = $('#service_type');
serviceTypeInput.on('change', function() {
otherInput = $('#otherInput');
if (serviceTypeInput.val() == "Other") {
otherInput.show();
} else {
otherInput.hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="service_type" id="service_type">
<option value="NULL"></option>
<option value="43">43</option>
<option value="Other">Other</option>
</select>
<input name='otherInput' id='otherInput' type="text" style="display: none" />
$(function() {
$('#sample').change(function() {
var val = this.value; // get the value of the select.
if (val == 'other') { // if the value is equal to "other" then append input below the select
$('html').append('<input type="text" id="inputOther"/>');
} else { // else then remove the input
$('#inputOther').remove();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="sample">
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
<option value="other">other</option>
</select>
I have a simple HTML Form that allows users to select a Type value. Based on the selected Type value they can then select from a list of Categories. The Categories are conditional based on the Type - when you select the Type a query is run against a database to select the matching Categories and display these as the select menu.
This is all working well as far as displaying the matching Categories, however when I submit the form the value for the selected Category is not part of the HTTP request and therefore nothing is being added to the database.
Here's the 2 fields in the HTML Form:
<div class="form-group">
<label for="type" class="control-label col-sm-3" >Type</label>
<div class="input-group col-xs-8">
<select class="form-control" name="type" id="type" onchange="getCategories(this.value)">
<option value=""></option>
<option value="Business">Business</option>
<option value="Commercial">Commercial</option>
<option value="Commercial Land">Commercial Land</option>
<option value="Land">Land</option>
<option value="Rental">Rental</option>
<option value="Residential">Residential</option>
<option value="Rural">Rural</option>
</select>
</div>
</div>
<div class="form-group">
<label for="category" class="control-label col-sm-3" >Category</label>
<div class="input-group col-xs-8" class="" id="categoryList">
<select class="form-control" name="category" id="category">
<option value=""></option>
</select>
</div>
</div>
Here's the script that is called when the users makes a Type selection:
function getCategories(str) {
if (str == "") {
document.getElementById("categoryList").innerHTML = "";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("categoryList").innerHTML = xmlhttp.responseText;
}
var status = xmlhttp.status == 200 ? 'success' : 'error'
var group = document.getElementById("categoryList")
group.classList.add(status)
}
xmlhttp.open("POST", "getPropertyCategories.php?type=" + str, true);
xmlhttp.send();
}
This is working fine to show the conditional Category select menu based on the Type selection, but the Category selection isn't included as part of the form submission.
The getPropertyCategories.php returns the following for example:
<select class="form-control" name="category" id="category">
<option></option>
<option value='Accommodation/Tourism'>Accommodation/Tourism</option>
<option value='Automotive' selected='selected' >Automotive</option>
<option value='Beauty/Health'>Beauty/Health</option>
<option value='Education/Training'>Education/Training</option><option value='Food/Hospitality'>Food/Hospitality</option>
<option value='Franchise'>Franchise</option><option value='Home/Garden'>Home/Garden</option>
<option value='Import/Export/Whole'>Import/Export/Whole</option>
</select>
Change the id of the select menu to match the id specified in the javascript - or vice versa
<select class="form-control" name="category" id="categoryList">
<option value=""></option>
</select>
once i select a particular value in dropdown menu based on that corresponding checkbox values should display
this is for getting dropdown values dynamically, based on this below i should get checkbox values
<tr>
<?php
$sub=mysql_query("select * from subjects");
$suboption='';
while($row= mysql_fetch_assoc($sub))
{
$suboption .='<option value = '.$row['sub_id'].'>'.$row['subname'].'</option>';
}
?>
<td width="15%">Subject :</td>
<td id="add_subject"><div id="add_subject1">
<select name="subject" id="subject" onchange="subjectskills(this.value)">
<option value="">Select Subject</option>
<?php echo $suboption; ?>
</select>
</div></td>
</tr>
You should create a 'div' tag where the updated checkbox values would be displayed through ajax. This is the following function which is called everytime the listbox value changes:
This is the script function:
function subjectskills(value)
{
var xmlhttp= new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("check_box").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","subjectskills?from="+value,true);
xmlhttp.send();
}
This is the div tag for checkbox:
<div id="check_box"></div>
Here's subjectskills.php:
<?php
if(isset($_GET['from']))
{
$from=$_GET['from'];
echo"<input type='checkbox' value='$from'>$from";
}
?>
I need to change dynamically dropdown list values based on date value by ajax.?
This is my code
<div class="span3">
<label>Bill Date
<div class="span12">
<div class="input-append date" id="dp2" data-date-format="yyyy-mm-dd">
<input class="span6" type="text" readonly="readonly" name='billdate' value="<?php echo $date1; ?>"/> <span class="add-on" disabled><i class="splashy-calendar_day" ></i></span>
</div>
</div>
</label>
</div>
<div class="span2" id="txtHintbill" style=" margin-left:85px;">
<label>Mode:</label>
<select name="mode" class="span10" onChange="billno(this.value)">
<option value="">Select Mode</option>
<option value="CASH" selected>CASH</option>
<option value="CREDIT">CREDIT</option>
<!-- <option value="CHEQUE">CHEQUE</option> -->
</select>
</div>
My script code is
<script>
function billno(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHintbill").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","billno.php?q="+str,true);
xmlhttp.send();
}
</script>
And my billno.php file is
<?php
include"config.php";
$m=$_GET["q"];
?>
<?php
$con=mysql_query("SELECT billno FROM printhub1 WHERE date1='$m' AND cash_detail='Not Received' ORDER BY billno");
$num=mysql_num_rows($con);
if($num==1)
{
?>
<select name="billno" id="billno" >
<?php
while($row=mysql_fetch_array($con))
{
$billno=$row['billno'];
?>
<option value="<?php echo $billno; ?>" <?php echo $billno; ?> ><?php echo $billno; ?></option>
<?php
}
?>
</select>
<?php
}
?>
I would to replace the select tag content.
I have already tried value retrieved and show in textbox, but i don't know how to get full dropdown list values.
Some suggestions...
Implement AJAX using jQUERY (since you have already tagged the post with jQuery, I wonder why are you not using it). This will make the entire xmlhttp thing easier for you under billno() function.
If you want to change the dropdown list dynamically based on date value entered in the text box, then you need to call the onChange() in the date input box (<input class="span6" type="text" name='billdate') - because when this value in this input box changes, you fire the AJAX.
Write the line <option value="<?php echo $billno; ?>" <?php echo $billno; ?> ><?php echo $billno; ?></option> as below:
<option value="<?php echo $billno; ?>"><?php echo $billno; ?></option>
You need a mechanism to refresh your <SELECT> based on the reply from your AJAX call. I suggest you use jQuery for updating the <select>. Example,
$.post ("billno.php", { q : str }, function (data) {
$("#txtHintbill").html (data);
});
Above code will perform the AJAX post as well as update the HTML of the div ID txtHintbill with results from server.
Hope this gives you some direction to move forward.