Variable checking in JSP - javascript

I am fairly new to the JSP scene, but have been programming in other languages for a few years. I am writing an application where I am passing a different variable to java based on a selection option. My main question was where to do the variable checking to see which value to pass?
Here is my current state of value checking (I am currently rewriting it in JSTL as I found sciplets are bad practice).
if (request.getParameter("Freq").equals("Minutes")) {
runOn = request.getParameter("numberOfMinutes");
} else if (request.getParameter("Freq").equals("Weekly")) {
runOn = request.getParameter("dayOfWeek");
} else if (request.getParameter("Freq").equals("Monthly")) {
runOn = request.getParameter("dayOfMonth");
} else {
runOn = "-1";
}
Should this sort of check be done on the jsp page, in javascript, or somewhere else? Looking for a best practice kind of answer.
Side question: Right now, I create a number of different drop down boxes and hide/unhide them using an onChange function in javascript. Would it be better to have one list box where the options change in javascript or keep the current system? Essentially the list boxes are asking the user what day they want their process to run. So if frequency is weekly, a list box with the days of the week would show, but if they have month selected, a list of numbers from 1 to 31 shows. It could all be done with one list box that changes its options when the 'frequency' drop down changes. Again, I'm looking for a best practice answer?
Thank you in advance!

Related

How to get value of Qualtrics Drag & Drop Choice using Javascript

I have what should be a simple answer. I have a drag and drop style multiple choice question on Qualtrics. I have recoded the answers as I wish. I think it assigns to each answer a variable like QID15_1 QID15_2 QID15_3, etc. The outputted data then gives the rank order of that selected option.
So if I ranked them, for example
QID15_3
QID15_1
QID15_2
The value of QID15_3 =1, the value of QID15_1=2, and the value of QID15_2=3.
What is the correct syntax to access these values? I want to set an Embedded Data item with the value of QID15_1, for example (so it should equal 2), but I can't seem to get it correct.
I've tried Qualtrics.SurveyEngine.getSelectedAnswerValue and things like this.
Any help would be appreciated. Thanks!
EDIT: With the help of a friend who knows Java, we figured it out. You can write something like
Qualtrics.SurveyEngine.addOnUnload(function()
{var order=document.getElementById( 'QR~QID15~1' ).getElementsByClassName( 'rank' )[0].innerText;
}
You don't need to set an embedded variable in JavaScript. You can just pipe the value wherever you need it (in a subsequent question, in the survey flow, etc.). It would be:
${q://QIDx/ChoiceNumericEntryValue/y}
where x is the question id and y is the choice id.
EDIT:
You can assign it to an embedded data variable in the survey flow like this:
edvar = ${q://QIDx/ChoiceNumericEntryValue/y}

Updating the view of a screen in AngularJS

Hey—I’m working on an angular 1.6 app, and I’ve come across an issue I’m not quite sure how to resolve. Here’s a brief discussion of the set up. The page is a form with thirty questions. There is a lot of conditional logic from the client that goes as follows: if user selects “Yes” on Q1 then show them Q2-4, if “No” on Q1, then skip to Q5. This repeats itself in the thirty question form.
I’ve come across the following scenario—what if the user selects “Yes” on Q1, answers Q2-4 and then realizes Q1 should really be “No”.
So my question is: 1) how do I clear the models? and 2) how do I update the view? So that if they decide, third time around, that Q1 should in fact be yes, Q2-Q4 are unchecked/clear/blank.
For 1) I just wrote a simple “clearAll method as follows:
function clearAll(arr){
arr.forEach(function(element){
if(element!==undefined){
element = null;
}
});
}
This clears the models but doesn’t update the view. How do I update the view so that Q2-4 are clear/empty/blank?
The idea I had is to update the view, so
$scope.$watch( myModel, function(val){
//set to null or delete?
}
But I cannot generalize this so it is flexible enough to accept an array. Any advice would be appreciated. Thanks.
For your particular problem I would use ui-router and resolve different states based on a question, i.e. state.questions and take different parameters like if you have url /questions as your base intro page (maybe weclome to questions or something), you would have /questions/1 and it would be 1st question /questions/2 would be second etc...
Then keep one list of objects on some simple service i.e.:
let questions = [];
which will persist throughout the app, later to be filled with some objects like:
{
q1: {
answers: [1,2,3]
},
{
q2: {
answers: [1,3]
}
You can always clear it after you are done with it or push new answer. Also you can check for particular answer before even loading the state, and this will give you great flexibility what you actually want.
well for ugly solution your watch maybe could work if you add watch group and timeout:
$scope.$watchGroup(arrayOfModels, (newVal, oldVal)=>{
if(newVal) {
// now do something you want with that models
// if they don't apply, try $timeout(()=> $scope.$apply());
}
})

Select a valid choice. '"xxx" is not one of the available choices

I know many people have been asking this question "Select a valid choice. 'xxx' is not one of the available choices." but I just think my scenario is somewhat different.
Well, what I was trying to do is; I have an input choice field that allow someone to choose between two choices (Let's say choices are A and B). Hence if someone decides to choose choice A; some of available input fields will disappear while holding default values that i have assigned to them (this is to allow form submission. I can't make them required=False since if you choose choice B, this input fields will appear and if required=False they may be submitted without values.Something that I don't want):
forms.py
cars = forms.ModelChoiceField(queryset = cars.objects.all(),widget=Select(attrs={'class': 'form-control seleckpicker','required':'True'}),empty_label="Choose car") . Hence if it return empty list if nothing in table. So what I was trying to do is append value.
under html page
$('select[name=fuel] option:eq(1)').attr('selected', 'selected');
$('#id_cars').append($('', {
value: 'AA',
text: 'AA',
}));
$('id_total_fruits').val(0);
So what I was trying to do is append value to id_cars incase the dropdown is empty field
This is why I decided to append value to be able to submit my though; even though doing this makes me getting this error:
"Select a valid choice. 'xxx' is not one of the available choices."
I hope I make myself clear. Anyone with solution please...????
I may be misunderstanding the question a bit here, so if im way off base let me know and i'll delete my answer haha.
why not do an if or switch block statement switched based on the selections to set the required attribute
ie.
`$('#selector').on('change', function() {
if ($(this).val() == "A")) {
$('#inputA').prop('required', true);
$('#inputB').removeAttr('required');
}
if ($(this).val() == "B")) {
$('#inputB').prop('required', true);
$('#inputA').removeAttr('required');
}
});
Thats what i have used on some of my pages in this scenario.

how to refresh php div every few seconds

So I'm basically trying to implement an online trading card game in php and (maybe minimal) javascript, and so far I have a GUI set up in php, a database with the required tables (so far) to hold the decks, fields, and other various data. I've got it to the point where i have a function that initializes the fields for both players and populates their fields and hands with cards from their deck. I also got it to the point where a player on another computer or device can take the room number of the game and join the game so the see the exact initialized field.
my next objective was to be able to set up the turn logic, and allow the screen to update for one player while the other is making plays during their turn. My problem is I'm not sure what the minimum viable solution will be for this, because I have long intricate functions that display the current field based off of values that come from a field table. So my ideal logic is set up like this:
//find game form
//host game form
//if host is set (post){
//call insert field function (a long function that creates a table for the current game being played and initializes the field and hand with random cards from the deck table)
//link game to host for future field printing
// populate host function (a function that prints a table of every card on the field with the most up to date variables to represent each spot)
}
//if find is set (post){
//call insert field function
//link game to host for future field printing
//a button form to start the game which begins by calling heads or tails
// populate find function (same as host but for find player side)
}
//if start game is set (post){
// do game logic such as coin flip which will be notified to opponent in a message field in the game table upon the refresh
}
I was wrapping the populate host function in a refreshable div like this:
print "<div><meta http-equiv='refresh' content='5' />"; //host populate just disapears
populatehost($roomnumber); //populates self and opponents in hosts perspsective
print "</div>";
so that the function would be called every 5 seconds or so, but this just causes the field to disappear after 5 seconds. So...
Why is it disappearing?
How can i get it to refresh the page and just display one instance of the field using the populate field function?
Is this approach viable? if not, what's the easiest approach I could take and how?
I also tried a solution where the function was called every 5 seconds and gotten rid of, but i'm not sure if I was doing it right, the PHP manual didn't explain it very clearly, and after lots of trial and error i think i crashed my server. It was something like this:
//loop
ob_start();
populatehost($_SESSION['gamenumber']);
sleep(5);
ob_end_clean();
I kept playing around with that logic but it either printed infinitely, didn't print at all, or crashed my server. I'm afraid to play around even more for fear of crashing the server again. Maybe someone can explain the correct way to do it or why it is not viable if it isn't?
I'm sorry if these are dumb questions, I've spent days and days researching approaches to this problem, I wasn't sure if I could implement it using javascript or ajax along with my field printing function, I tried some and they weren't working, and I'm not sure if it's because my approach is viable or not.
You can take javascript or Ajax help for div you can assign id to the div and call id in javascript:
jQuery(function () {
var $els = $('div[id^=quote]'),
i = 0,
len = $els.length;
$els.slice(1).hide();
setInterval(function () {
$els.eq(i).fadeOut(function () {
i = (i + 1) % len
$els.eq(i).fadeIn();
})
}, 2500)
})
check this fiddle:
http://jsfiddle.net/arunpjohny/asTBL/
Thanks

Creating variable arrays with Javascript (dyamic, drop down)

I've been searching the board for a while (both here and Google) and can't seem to find what I'm looking. (Sorry if I've missed it and this qualifies as an annoying/redundant question.)
I'm working on form that will have copious amounts of drop downs based on previous selected variable(s). I was trying to come up with an easier way than having to create the individual fields and then hide/show based on selection. Ultimately, I'm aiming for a "tree" with between 3-5 levels of menus. (Clients doing, not mine.)
What the Logic looks like:
Variables: type, offer1, offer2, insert1, insert2, insert3,...
Where [type] determines [offer1,2] which has up to 3 variables each [insert1,2,3...]
So If user selects Type A: Offer1 = Array A (Insert1 = ArrayA1, Insert2 = ArrayA2, Insert3 = null) and Offer2 = Array B (Insert4 = ArrayB4, Insert5 = null); and so on and so forth.
So far, everything I've found only seems to handle the first tier, and JS isn't exactly my forte. Any pointing in the right direction would be greatly appreciated.
In case anyone else runs across this issue, I found this solution that uses JQuery:
http://www.appelsiini.net/projects/chained

Categories

Resources