<html>
<head>
<style>
div { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<select name="sweets" multiple="multiple">
<option>Chocolate</option>
<option selected="selected">Candy</option>
<option>Taffy</option>
<option selected="selected">Caramel</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
<div></div>
<script>
$("select").change(function() {
var str = "";
$("select option:selected").each(function() {
str += $(this).text() + " ";
});
$("div").text(str);
}).change();
</script>
</body>
</html>
This my code I want to display on console in jsp on Select change passing the value.I want to Print onslected item in Jsp page
<%
String k=request.getParameter("sweets");
out.println(k);
%>
Like this I want to Print on select item data please help me
Enclose select box with-in form tag and in
onchange method of select , trigger form submit
<form id="resultform" action="resultPage.jsp">
<select name="sweets" multiple="multiple">
<option>Chocolate</option>
<option selected="selected">Candy</option>
<option>Taffy</option>
<option selected="selected">Caramel</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
</form>
In jquery method write
$("select").change(function() {
$('#resultform').submit();
}
Related
I am trying to get a one-page input value to another page while clicking the order button.
while I will take a number of item value and click the order button, it will carry the value the order page. The page code is here,
<td><%=rs.getString("product_price")%></td>
<td> <input type="number" name="no_item" value="1" /></td>
<td class="text-center" width="250">
Order
Edit
Delete
</td>
The order page code is here,
<%
statement = connection.createStatement();
String u=request.getParameter("u");
String item_num =request.getParameter("no_item");
int num=Integer.parseInt(u);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
String product_price;
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<h4 style="float:left; padding-right:8px;">Product Name:</h4> <h4> <%=rs.getString("product_name")%> </h4>
</div>
<div class="form-group">
<%
product_price = rs.getString("product_price"); int num1 = Integer.parseInt(product_price); %>
</div>
<%= item_num %>
<%= num1 %>
<%
}
%>
Onclick of order button add the input value in url query string. you have need to use java script in jsp page.
<script>
function order(page, id){
input_value = document.getElementById('no_item').value;
location.href= page+"?u="+id+"&no_item="+input_value;
}
</script>
Add the onclick function within order button.
<a onclick="order('order.jsp', '<%=rs.getString("id")%>')" class="btn btn-success">Order</a>
In order.jsp page you will get the input value.
request.getParameter("no_item");
I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
request.setAttribute("code", name);
%>
</select>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
Search Text
</div>
</div>
</form>
Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance
UPDATED
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> /option>
<% System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = <%= name%>/> .
In the servlet I used,
request.getParameter("code");
update your jsp likewise,
<form...>
...
<input type="hidden" name="code" value = <%= name%>/>
....
</form>
then get it from your servlet likewise,
request.getParameter("code"); // will return value of code
NOTE :
Remove from your jsp-code if above solution you gonna implement then,
request.setAttribute("code", name);
<select name="userSelected">
<option value="-1">---Select---</option>
<c:forEach items="${users}" var="user" varStatus="status">
<option value="${user.userId}">${user.userName}</option>
</c:forEach>
</select>
<button type="button" onclick="window.location.href='${pageContext.request.contextPath}/viewExpense/userSelected/<%=-1%>'">view</button>
when a user selects any value from dropdown then we can get the corresponging value using the name attribute (in this case name is "userSelected"). But how I can append this value in the href url above using that scriptlet. Someone help!!!
If you are linking to an anchor on a page you need to put a # in front of the anchor.
Try This
$("#yourDropdown").change(function () {
var selectedValue = $('#yourDropdown option:selected').val();
$("a").attr('href','#'+selectedValue); // just Append # tag before value
});
You can try something like :
<select id="userSelected">
----------
----------
</select>
<button type="button" onclick="submitPage();">view</button>
<script type="text/javascript">
function submitPage() {
var elm = document.getElementById("userSelected");
var strVal = elm.options[elm.selectedIndex].value;
window.location.href = '/viewExpense/userSelected/' + strVal;
}
</script>
If possible, you try the same thing using JQuery.
<select id="userSelected">
--------
--------
</select>
<button id="submitButton">view</button>
<script type="text/javascript">
$(document).ready(function() {
$('#submitButton').on('click', function() {
window.location.href = '/viewExpense/userSelected/' + $('#userSelected :selected').text();;
});
});
</script>
i have written a jsp code which contains mutiple select and when i click on submit i get the null pointer exception . i am using the request.getParametervalues() to fetch the data from multiple select:
jsp code is as follows
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page language="java" import="java.lang.* ,javax.servlet.*,javax.servlet.http.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form id="form1" method="get" name="form">
<select name="color1" id="dd1" multiple>
<option value="empty">Select Color</option>
<option value="RED">red</option>
<option value="BLUE">blue</option>
<option value="GREEN">green</option>
<option value="YELLOW">yellow</option>
<option value="PINK">pink</option>
<option value="BLACK">black</option>
<option value="BROWN">brown</option>
<option value="PURPLE">purple</option>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>
<%
String [] x = request.getParameterValues("color1");
if(!x.equals(""))
{
for (int i = 0; i < x.length; i++)
out.println (x[i]);
}
%>
i am getting null pointer exception in out.println (x[i]); part
i have also tried if(x ! = null) which is giving error
i have also tried this :
if(request.getParameterValues("color1").equals(null))
{
out.println("abcd");
}
its giving the same exception
Check before doing this in your scriplet
if(request.getParameterValues("color1")!=null){
//Go On
}
As your select is in same .jsp and so on start up load it will give you null for sure.
<select name="color1" id="dd1" multiple>
Moreover you are comparing array with ""
if(!x.equals("")) //No use Check for Not Null
if(x!=null)//Cahange this
If you are using form why not pass request and perform checks and other stuff in Servlet.
I have this code in one jsp and i want to sent to the another and display selected value and correct answer. In the first page i have this code:
<form method="post" action="result.jsp" >
<p>choose answer</p>
<select name="fill">
<option value="0">Fill</option>
<option value="1">England</option>
<option value="2">China</option>
<option value="3">France</option>
</select>
<input type="submit" name="sent" />
</form>
and then the second jsp:
<body>
<jsp:declaration>
String s = "";
</jsp:declaration>
<jsp:scriptlet>
s = request.getParameter("fill");
</jsp:scriptlet>
<h1>Your answer is <jsp:expression>s </jsp:expression>and correct is England</h1>
</body>
And it returns null, why?