JSTL - Promotional Code - jstl

I'm trying to use JSTL, so that if a user enters a code, it is looked up in the database, if it equals the value, then the following discount is applied.
I kind of have an idea/pseudocode of what i want to write but i cant get it working or where i should go from here
In my purchase page i have:
form action="promo.jsp" method="GET">
Promotional Code: <input type="text" name="promo">
<input type = "submit" id="promo" value = "Apply Code" />
</form>
<form action="complete_purchase.jsp" method="POST">
<input type="submit" value="Complete Purchase" />
</form>
And in my "promo.jsp"
I have something a long the lines of:
<sql:query var="item">
select item_price from inventory;
</sql:query>
<sql:query var="pro">
select code,discount from promotion_code;
</sql:query>
<c:if test="${pro.promo eq 'spring01'}">
<c:set var="user" value="${(item_price-(item_price*discount))}" scope="session" />
</c:if>
<c:out value="${sessionScope.user}" />
So basically, when i click apply code, it stores the new value of the price in a variable, that i want to call on the "final purchase page"

Related

Checked radion button based on model variable + Spring forms + JSP

I have model called cuntry.java with variable lang .I want to check the radio button option in JSP based on the lang. It could be 'EN' or 'FR'. when i wrote below code, throwing error in JSP page.
Code:
<form:radiobutton path="lang" id="language" value="FR" name="radios" <c:if ${cuntry.lang == 'FR' ? 'checked="checked"' : '' } /> ></form:radiobutton>
Error : Unterminated <form:radiobutton tag
There are multiple errors in the form tag. When using Spring tags, you may not specify id and name attributes. They will be generated automatically from path variable. Tag <c:if ... /> inside another tag <form:radiobutton ... /> is not allowed. Remove end tag as well. This is the right way
<form:radiobutton path="lang" value="FR" checked="${cuntry.lang == 'FR' ? 'checked' : '' }" />
If the value of cuntry.lang is FR, the Spring tag will result the following HTML (after compiling)
<input id="lang" name="lang" checked="checked" type="radio" value="FR" />
otherwise
<input id="lang" name="lang" type="radio" value="FR" />
If you absolutely need attributes id = language and name = radios, you can not use Spring tag .
You will find a good example here.
<c:if> tag works on tags rather than attributes. As a result, your html syntax becomes invalid.
I haven't done jsp for quite a while. this should more or less work:
<form:radiobutton checked="${cuntry.lang == 'FR' ? 'checked':''}" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
If you insist on using <c:if> tag for this, you can do this:
<c:if test="${cuntry.lang == 'FR'}">
<form:radiobutton checked="checked" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
<c:if test="${cuntry.lang != 'FR'}">
<form:radiobutton path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
You can also use <c:choose>,<c:when>,<c:otherwise> tags to solve this problem.
Obviously the first solution looks better.

Form Mobile Validation - after other functions have run

I need some help with the following issue. I am pretty new to JavaScript so this one is pretty tough for me. Any help would be greatly appreciated!
We have a form for registration that is already in use and I need to make some amends to it to enhance its usability. The form itself already contains several validation scripts and I need to add in a new one - an error message to appear next to the mobile field. I am unable to amend the form HTML itself so I have to do everything by javascript – I need to bind the new function to the submit button so that it fires AFTER all the other validations (field highlight, pop ups), but before the page turns over.
Here is the form (I have just left in the feild i need to amend) :
<form method="post" action="#" id="register_form" class="validate">
<input type="hidden" name="page" value="" />
<input type="hidden" name="unregistered" value="false" />
<fieldset>
<legend>I am a new online customer</legend>
<div class="element">
<label for="mobile_number">Mobile:**</label>
<input name="mobileNumber" type="text" id="mobile_number" title="Please enter at least one number" class="text validate {validate:{minLength:10, eitherOr:'#telephone_number', messages:{minLength:'Please enter a valid phone number (at least #NUM digits long)', eitherOr:'Please enter either your Telephone or Mobile number'}}}" />
</div>
<div class="element continue_shopping_cont submit">
<button type="submit" class="submit button small_button"><span>Register</span></button>
</div>
<input type="hidden" name="secure_from" value="" />
</fieldset>
</form>
Could someone help show me how i am supposed to do this and how is best to target this field?

How to give c:if tag in display tag

I have a display tag in my jsp file. Its like..
<display:table id="currentRow" name="${ListObj}" requestURI="" sort="page" defaultsort="2"
pagesize="5" class="displayTable">
<display:caption><font color="red">Users List</font></display:caption>
<display:column property="ID" title="Role" ></display:column>
<display:column property="Name" title="User Name" sortable="true"></display:column>
<c:if test="%{currentRow.ID ne '1'}">
<display:column >
<i>edit</i>
</display:column>
</c:if>
</display:table>
i had wrote the code <c:if test="%{currentRow.ID ne '1'}"> for that i didnt want to show the edit link for user with ID 1. But that condition is not working. Ie no rows in display tag shows edit link. But if i give <c:if test="%{currentRow.ID eq '1'}">, edit link will be displayed.
How can i make it displayed for all the rows except the one with ID=1???
You should put the if statement inside the <display:column> tag as you will always want the <td> tag rendered in the table even if it is empty.
<display:column >
<c:if test="%{currentRow.ID ne 1}">
<i>edit</i>
</c:if>
</display:column>
If the id attribute is an integer you will want to perform equals on it as an int.

Form Submit using a Javascript to invoke webflow transition, doesn't take the updated value on form

I am trying to invoke a form submit using javascript (jquery) to invoke a webflow transition. It works and the submit invokes the desired transition. But, the updated radio button values is not reflected on the model object which is posted.
Here is the code:
<form:form method="post" action="#" commandName="infoModel" name="pageForm">
<form:input type="input" path="testMsg" id="success" />
<input type="button" id="clearSelections" value="Clear Selections">
<div class="question">
<h4><c:out value="${infoModel.questionInfo.description}"/> </h4>
<form:radiobuttons path="infoModel.answerId"
itemValue="answerId" itemLabel="answerDescription" items="${infoModel.answers}" delimiter="<br/>" />
</div>
<input type="submit" name="_eventId_saveQualitativeInput" value="Save" id="save" />
$(document).ready(function() {
$('#tabs').tabs();
//Clear selections (copy is server-side)
$('#clearSelections').click(function() {
//$('input[type="radio"]').prop('checked', false);
$('input[type="radio"]').removeAttr('checked');
$('#save').trigger('click');
});
});
</form:form>
The form:radiobutton, generates the below html:
<div class="question">
<h4>Is this a general obligation of the entity representing a full faith and credit pledge? </h4>
<span>
<input type="radio" checked="checked" value="273" name="infoModel.answerId" id="infoModel.answerId1">
<label for="infoModel.answerId1">Yes</label>
</span>
<span><br>
<input type="radio" value="274" name="infoModel.answerId" id="infoModel.answerId2">
<label for="infoModel.answerId2">No</label>
</span>
<br>
<span class="error"></span>
</div>
The input id= "success" value is registered and when the control goes to the server, the value of input id= "success" is updated in the "infoModel" object. But the value of answerId is not updated on the "infoModel" object.
Thoughts if i am missing something in the form:radiobutton element or if there is something else wrong?
Thanks in advance!
EDIT:::::::
Thanks mico! that makes sense. I stripped of some of the code first time to make it precise, but i have a list which is being used for building the radio-buttons, below is the code:
<c:forEach items="${infoModel.list["index"]}" var="qa" varStatus="rowCount">
<div class="question">
<h4><c:out value="${question.questionInfo.description}"/> </h4>
<form:radiobuttons path="list["index"][${rowCount.index}].answerId" itemValue="answerId" itemLabel="answerDescription" items="${question.answers}" delimiter="<br/>" />
<br>
</div>
</c:forEach>
Could you please suggest how i could try this one out?
NOTE: The same code works on a regular form submit on click of a button of type submit. Its the javascript form submit which is not working. I also tried to do whatever i want to do in javascript and then invoke the button.trigger('click'); form got submitted but the changes made on form in my javascript didnt reflect.
With commandName inside a form:form tag you set "Name of the model attribute under which the form object is exposed" (see Spring Documentation). Then in path you should tell the continuation of the path inside the model attribute.
With this said I would only drop the extra word infoModel from path="infoModel.answerId" and have it rewritten as path="answerId" there under the form:radiobutton.

How do I unhide a CFDIV that has dynamic content with AJAX binding itself?

I have the following CFSELECT tags that are used to populate a text input:
<cfselect id="this" name="this" bind="cfc:Data.getThis()" bindonload="true" />
<cfselect id="that" name="that" bind="cfc:Data.getThat({p1})" />
<cfselect id="theOther" name="theOther" bind="cfc:Data.getTheOther({p1}, {p2})" />
The text input is the only value that needs to be submitted in a form:
<cfform name="addItem" method="post" action="somepage.cfm">
<cfinput
type="text"
id="item"
name="item"
bind="cfc:Data.getResult({this}, {that}, {theOther})" /><br />
<cfinput
type="submit"
name="addButton"
value="Add Item" />
</cfform>
I want the form and it's contents to be visible only when all three selections have been made, and there is a value for the text input. What is the best way to do this? I'm assuming some use of CFDIV is the best way, but I'm not sure how to load the dynamic content (the CFINPUTs) this way.
<cfselect id="this" name="this" bind="cfc:Data.getThis()" bindonload="true" onChange="toggleForm();" />
<cfselect id="that" name="that" bind="cfc:Data.getThat({p1})" onChange="toggleForm();" />
<cfselect id="theOther" name="theOther" bind="cfc:Data.getTheOther({p1}, {p2})" onChange="toggleForm();" />
<div id="theForm" style="display:none">
<cfform name="addItem" method="post" action="somepage.cfm">
<cfinput
type="text"
id="item"
name="item"
bind="cfc:Data.getResult({this}, {that}, {theOther})" /><br />
<cfinput
type="submit"
name="addButton"
value="Add Item" />
</cfform>
</div>
<script type="text/javascript">
function toggleForm(){
var a = document.getElementById("this").selectedIndex;
var b = document.getElementById("that").selectedIndex;
var c = document.getElementById("theOther").selectedIndex;
if (a > -1 && b > -1 && c > -1){
document.getElementById("theForm").style.display = "";
}
}
</script>
Personally I would simplify that JS a bit by using jQuery, but I don't know if you're already using jQuery on your site, and I don't want to be another "use jquery" empty answer; so this should work without jQuery, should you want/need to go without it. (But jQuery is awesome!)

Resources