Dynamically populating checkbox from database using ajax - jquery-ui

I am new with JQuery. (I want to do it using AJAX, JQuery, PHP)
I am trying to dynamically populate a list of checkboxes from the database.
What I have is a drop down. Based on the selected option I want to query the database, and depending on the recordset I want to dynamically display the checkboxes.
Any suggestions
<script>
$(function(){
$("#softwareapp").change(function(){
if($(this).val() > 0){
var app_id = $('#softwareapp').val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>ajaxcalls/get_softwareappversions/"+app_id,
data:{savid = $(this).val()},
success: function(data){
var _table = $("<table></table>");
for(var i = 0; i< data.length; i++){
$("<tr></tr>").append($("<td></td>").html("<label><input type='checkbox' value='data[i]' name='softappver[]'/>" + data[i] + "</label>")).appendTo(_table);
}
$("#displayappversions").html("").append(_table);
}
});
}
});
})
For some reason this has stoped working. I am trying to get rid fo the table stuff.
and following is my HTML:
<select name="softwareapp" id="softwareapp">
<option value="0" selected="selected">Please select</option>
<option value="1">SAP</option>
<option value="2">SAGE</option>
<option value="3">SWIFT</option>
</select>
<div class="form-row">
<p class="form-label">Application Version</p>
<div class="form-item" id="displayappversions">
<!--- VERSIONS ARE SUPPOSE TO COME HERE--->
</div>
</div>

<script type="text/javascript">
$(document).ready(function(){
$("#vendor").change(function(){
if($(this).val() > 0){
$("#displayappversions").load("<?php echo base_url(); ?>ajaxcalls/get_softwareappversions/" + $(this).val());
}
});
});
My ajax call is returning HTML that makes the list of chcek boxes.

Related

jQuery Mobile Navigate Between Pages using Form

I have a form using select values, I'd like to make each option works as a link. How can I do it?
<form>
<label for="direction">Select Direction:</label>
<select name="dir" id="dir">
<option value="1">N</option>
<option value="2"><a href="#page3">S</option>
</select>
</fieldset>
<input type="submit" data-inline="true" value="Confirm">
</form>
Listen to submit event to retrieve select value and then change page.
Remove anchor tag from option, and put page ID in value value="#pageID".
$(document).on("pagecreate", "#pageID", function () {
$("#formID").on("submit", function (e) {
e.preventDefault();
var page = $("#selectID").val();
$.mobile.pageContainer.pagecontainer("change", page);
});
});
Demo

Cloning div and incrementing

I am atempting to clone a div (multiple times) and add increments so I can parse the results into a database using XML. I only want to clone the form section not the data entered. The Div I am cloning has 3 selects and 2 radios (all of which when added need to be empty).
HTML
<div id="Template1" class="template">
<div class="_100">
<div class="_25"> <fieldset>
<label class="label_analysis" for="analysis">Analyte:</label>
<select class="select_analyte" name="analysis" id="analysis">
<option value="">Select</option>
<option value="TN">TN</option>
<option value="TP,NO2+3">TP,NO2+3</option>
</select> </fieldset></div>
<div data-role="controlgroup" class="_13">
<label><input type="checkbox" data-mini="true" name="Filtered" id="Filtered" value="True">
0.45u Filtered</label>
<label><input type="checkbox" data-mini="true" name="Dup" id="Dup" value="True">
Field Dup</label></div>
<div class="_25"><fieldset>
<label class="label_preserve" for="preserve">Preserved</label>
<select class="select_preserve" name="preserve" id="preserve">
<option value="">Select</option>
<option value="HNO3">HNO₃</option>
<option value="H2SO4">H₂SO₄</option>
</select></fieldset></div>
<div class="_20"> <fieldset>
<label class="label_cool" for="cool">Cooled</label>
<select class="select_cool" name="cool" id="cool">
<option value="">Select</option>
<option value="Ice">Ice</option>
<option value="Frozen">Frozen</option>
<option value="None">None</option>
</select>
</fieldset></div>
<div class="_13">
Add Analyte
Remove</div>
</div>
</div>
<div id="place" class="place"></div>
I have tried two different scripts
This script works as I want to hide the remove button on the original div and only have it appear on the cloned but it doesn't increment.
(function($){
var Template = $('.Template');
var count = 0;
$('.removeNew').hide().on('click', function(e) {
e.preventDefault();
$(this).closest('.Template').remove();
});
$('a.showNew').on('click', function(e) {
e.preventDefault();
var clone = Template.clone(true, true).insertAfter("#place").find('.removeNew').show().end();
});
})(jQuery);
and this one which increments but doesn't hide the remove button
$('#showNew').click(function() {
var num = $('.template').length,
newNum = new Number(num + 1),
newElem = $('#Template' + num).clone(true, true).attr('id', 'Template' + newNum).appendTo('#place');
newElem.find('.analysis').attr('id', 'ID' + newNum + '_analysis').attr('name', 'ID' + newNum + '_analysis').val();
newElem.find('.preserve').attr('id', 'ID' + newNum + '_preserve').attr('name', 'ID' + newNum + '_preserve').val();
newElem.find('.cool').attr('id', 'ID' + newNum + '_cool').attr('name', 'ID' + newNum + '_cool').val();
$('#Template'+ num).after(newElem);
});
})
I am wondering if there is some way to combine them...
I am also having issues as no matter what I seem to try the original div becomes the clone, complete with remove button, while what should be the original div can be changed.
I believe the simplest approach is to have an original copy of the template hidden and base all clones off of that. This fiddle demonstrates this approach.
(function ($) {
var Template = $('#Template');
var count = 0;
var nextId = 0;
Template.find('.removeNew').on('click', function (e) {
e.preventDefault();
$(this).closest('.template').remove();
count--;
});
function cloneTemplate(removable) {
var clone = Template.clone(true, true);
clone.attr('id', clone.attr('id') + nextId);
clone.find('label[for]').each(function( index ) {
var elem = $(this);
elem.attr('for', elem.attr('for') + nextId);
});
clone.find('select, input').each(function( index ) {
var elem = $(this);
elem.attr('id', elem.attr('id') + nextId);
elem.attr('name', elem.attr('name') + nextId);
});
if (!removable) {
clone.find('.removeNew').remove();
}
clone.insertBefore("#addNew").removeClass('hide');
count++;
nextId++;
}
// Create First Analyte and delete the remove button.
cloneTemplate(false);
$('a.showNew').on('click', function (e) {
e.preventDefault();
cloneTemplate(true);
return false;
});
})(jQuery);
It didn't make sense to have the Add New button inside the template so I moved it outside. All new clones are added before the Add New button.
You should notice in the script above that the first element is created by cloning the Template. Then new elements are created calling the same clone function when the Add New button is clicked.
Note: This solution does not include updating element id or name attributes. Something that you almost certainly want to do.

jQuery mobile clone

I am trying to get jQuery-mobile to clone a div like I get it to do on jsfiddle.
head
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<link rel="stylesheet" type="text/css" href="form.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery.maskedinput.min.js" type="text/javascript"></script>
<script type="text/javascript" src="json2.min.js"></script>
<!--[if lt IE 9]>
<script type="text/javascript" src="flashcanvas.js"></script>
<![endif]-->
<script type="text/javascript" src="CreateXMLScript.js"></script>
<script>
$(document).bind('mobileinit', function() {
$.mobile.page.prototype.options.addBackBtn= true;
$.mobile.page.prototype.options.backBtnText="Back";
$.mobile.page.prototype.options.backBtnIcon="arrow-l";
$.mobile.selectmenu.prototype.options.nativeMenu = false;
$.mobile.page.prototype.options.keepNative = ".native";
$.mobile.keepNative=".native";
})
</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
HTML
<div id="Template" class="template hide">
<div class="_100">
<div class="_25">
<fieldset>
<label class="label_analysis" for="analysis">Analyte:</label>
<select class="select_analyte" name="analysis" id="analysis">
<option value="">Select</option>
<option value="TN">TN</option>
<option value="TP,NO2+3">TP,NO2+3</option>
<option value="TP,NO2+3,NH3+4">TP,NO2+3,NH3+4</option>
<option value="DO">Dissolved Orthophosphate</option>
<option value="TR">TR Metals, Hardness</option>
<option value="FF">Dissolved Aluminum (FF)</option>
<option value="ULL">TR Metals (ULL Hg)</option>
<option value="TSS">TSS</option>
<option value="TSS/TDS">TSS/TDS</option>
<option value="DM">Dissolved Metals (FF)</option>
<option value="TOC">TOC</option>
<option value="ABF">Anions, Bromide, Fluoride</option>
<option value="CH">Cations, Hardness</option>
</select>
</fieldset>
</div>
<div data-role="controlgroup" class="_13">
<label><input type="checkbox" data-mini="true" name="Filtered" id="Filtered" value="True">0.45u Filtered</label>
<label><input type="checkbox" data-mini="true" name="Dup" id="Dup" value="True">Field Dup</label>
</div>
<div class="_25">
<fieldset>
<label class="label_preserve" for="preserve">Preserved</label>
<select class="select_preserve" name="preserve" id="preserve">
<option value="">Select</option>
<option value="HNO3">HNO₃</option>
<option value="H2SO4">H₂SO₄</option>
<option value="H3PO4">H₃PO₄</option>
<option value="HCL">HCL</option>
<option value="None1">None</option>
</select>
</fieldset>
</div>
<div class="_20">
<fieldset>
<label class="label_cool" for="cool">Cooled</label>
<select class="select_cool" name="cool" id="cool">
<option value="">Select</option>
<option value="Ice">Ice</option>
<option value="Frozen">Frozen</option>
<option value="None">None</option>
</select>
</fieldset>
</div>
<div class="_13">
Remove
</div>
</div>
</div>
<div id="addNew">
ADD ANALYTE
</div>
Script
<script>
$(document).ready(function() {
(function ($) {
var Template = $('#Template');
var count = 0;
var nextId = 0;
Template.find('.removeNew').on('click', function (e) {
e.preventDefault();
$(this).closest('.template').remove();
count--;
});
function cloneTemplate(removable) {
var clone = Template.clone(true, true);
clone.attr('id', clone.attr('id') + nextId);
clone.find('label[for]').each(function( index ) {
var elem = $(this);
elem.attr('for', elem.attr('for') + nextId);
});
clone.find('select, input').each(function( index ) {
var elem = $(this);
elem.attr('id', elem.attr('id') + nextId);
elem.attr('name', elem.attr('name') + nextId);
});
if (!removable) {
clone.find('.removeNew').remove();
}
clone.insertBefore("#addNew").removeClass('hide');
count++;
nextId++;
}
// Create First Analyte and delete the remove button.
cloneTemplate(false);
$('a.showNew').on('click', function (e) {
e.preventDefault();
cloneTemplate(true);
return false;
});
})(jQuery)
});
</script>
I have received tons of help with jQuery but no matter what I do, it just won't work in jQuery-mobile. Either it clones upside down (the 1st item becomes last and all fields are populated) or the new fields on the new div won't let me populate them (the current issue).
I didn't create a Fiddle as everything I have tried works in the fiddle, just not in Mobile.
I checked the source of your pages from the link you provided, im not sure exactly what you are trying to achieve as you have a Complex Form. However, place this amended code i made below just before the </body> tag of your page and remove the other script part you have at the moment. Make sure you do a backup of your JQM APP before just incase you need to Undo these changes i am providing.
I also noticed a few $(document).ready(function() { in your pages, these are not needed so remove them. Just a bare bones click functions are ok. You can consolidate all these under one <script> ///// </script>. Where you place your functions is important, as some need to be in the <head> some in the <body> and some after the </body> or </html> tags
<script>
var Template = $('#Template');
var count = 0;
var nextId = 0;
Template.find('.removeNew').on('click', function (e) {
e.preventDefault();
$(this).closest('.template').remove();
count--;
});
function cloneTemplate(removable) {
var clone = Template.clone(true, true);
clone.attr('id', clone.attr('id') + nextId);
clone.find('label[for]').each(function( index ) {
var elem = $(this);
elem.attr('for', elem.attr('for') + nextId);
});
clone.find('select, input').each(function( index ) {
var elem = $(this);
elem.attr('id', elem.attr('id') + nextId);
elem.attr('name', elem.attr('name') + nextId);
});
if (!removable) {
clone.find('.removeNew').remove();
}
clone.insertBefore("#addNew").removeClass('hide');
count++;
nextId++;
}
// Create First Analyte and delete the remove button.
cloneTemplate(false);
$('a.showNew').on('click', function (e) {
e.preventDefault();
cloneTemplate(true);
return false;
});
</script>

changeing language files in JTSage Date and Time Picker plugin for jQueryMobile

I can't find a way how to Change the language on a the databox
this are my codesnippets
I am loading the language files
<script type="text/javascript" src="../../js/i8n/jquery.mobile.datebox.i18n.en.js"></script>
<script type="text/javascript" src="../../js/i8n/jquery.mobile.datebox.i18n.de.js"></script>
<div data-role="fieldcontain" id="langSelect">
<label for="picklang">
Language</label>
<select name="picklang" id="picklang" data-native-menu="false">
<option value="en">[en] English US</option>
<option value="de" selected="selected">[de] German</option>
</select>
</div>
<div data-role="fieldcontain" id="calendar">
<input name="startDate" type="date" data-role="datebox" id="startDate" data-options='{"mode": "flipbox", "useLang": "de"}' />
</div>
I want to change the localisation of the databox according to the selected values of #picklang and I am using therefore the following script code
<script type="text/javascript">
$(document).delegate('#picklang', 'change', function () {
var val = $("#picklang option:selected").val();
alert(val);
$('#startDate').attr('data-options', '{"mode": "flipbox", "useLang":"' + val + '"}');
});
A this is the problem. The alert shows the selected value but the databox (=flipbox) shows the same values as before (= after loading the page).
Is there anybody, who can give me advice?
Michael
Here's a little snippet that will not only load the newly selected language dynamically, but it will change to the new selection on every datebox on the page.
$('.opt-pop-lang').live('change', function() {
newLang = $(this).val();
$.ajax({
url: "http://dev.jtsage.com/cdn/datebox/i18n/jquery.mobile.datebox.i18n." + newLang + ".utf8.js",
success: function(data) {
eval(data);
var x = $.mobile.datebox.prototype.options.lang[newLang];
$(document).find('[data-role=datebox]').each(function() {
$(this).data('mobileDatebox').options.lang[newLang] = x;
$(this).data('mobileDatebox').options.useLang = newLang;
});
}
});
});
It may be more than you need, but the .useLang line is what is changing the language. The bits before are pulling in the file and injecting it into each datebox instance.

How do I get my success message to show after ajax submitting form in Jquery ui-tab

I have form on a jQuery ui-tab, which loads data onto a list displayed on the same ui-tab. I have managed to get the data loaded, and the display to return to same tab for a further item to be entered. However depending on the sort order of the list the new item is not always visually obvious, I want to fade in a message, but I can't get it to work along with returning to the same tab without a manual page refresh.
I have attached both the scripts I am trying to use.
Script A
<script> // this one loads data perfectly but no success message appears
$(document).ready(function() {
$("#addtodoitem").live( 'submit' , function(evt) {
evt.preventDefault();
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data) {
$('.ui-tabs-panel:visible').html(data);
$('.itemadded').fadeIn(1000).delay(4000).fadeOut(1000);
};
});
});
});
Script B
<script> //this one shows the message and loads the data, but it also loads another
//copy of ui-tabs overlapping the original
$(document).ready(function() {
$('#addtodoitem').ajaxForm(function(data) {
$('.ui-tabs-panel:visible').html(data);
$(".itemadded").fadeIn(1000).delay(4000).fadeOut(1000);
}); return false;
});
HTML
<div id="tabs_5" class="tabs">
<h2 id="todo5" class="tablecaption">Add To-do</h2>
<p>To raise a new issue please enter the description of the issue below.</p>
<form id="addtodoitem" class="todo" method="post" action="todo_do.php?do=<?=$todo_do != '' ? "edit&to_id={$_GET['to_id']}" : 'add'?>&pub=1&site_ref=NA">
<input type="hidden" value="0" name="status" id="status">
<textarea name="todo" id="todo" placeholder="To raise a new issue, please type the details of the issue here"><?=$todo_free_text?></textarea> <br />
<input type="submit" value="Submit" class="button" />
</form>
<h2 class="tablecaption itemadded">Thank you, your item has been added to the list.</h2>
I got to a satisfactory solution by using a separate refresh link button and php page to cause the refresh after posting so the script is so:
<script type="text/javascript">
$(document).ready(function(){
$('#addtodo').click(function(e){
e.preventDefault();
if($('#todo').val()!="")
{
$.ajax({
type: 'POST',
url: 'todo_do.php',
data: { 'status': $('#status').val(),
'todo': $('#todo').val()
},
success: function(data){
$('.itemadded').slideDown(500).delay(2000).fadeOut(500);
$('.refreshtodo').slideDown(500);
$('#addtodoitem')[0].reset();
$('#tabs > ul').tabs({selected: 5 });
}
});
}
else{$('.nothing').slideDown(500).delay(1000).fadeOut(500)}
});
});
and the extra php page just contains
<?
header("Location: resident.php#tabs_5");
?>

Resources