pass jsp parameter to another using button - java

so i'm trying to pass selected value from a combobox in a jsp to another jsp here is what i got myself into
<form name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>
<button onclick="window.location.href='/acceuil.jsp?mat=' JSGetSelectedItemMat() " >Statistique</button>
<script>
function JSGetSelectedItemMat()
{
var e = document.getElementById("soflow");
var strSel = e.options[e.selectedIndex].value;
document.getElementById("btt").value = strSel;
}
</script>

try this code to get selected valued from select tag. As there is no need to use js, you are using post method in your html form. Also get the value in acceuil.jsp using getParameter method. that is request.getParameter("mat");
<form action="acceuil.jsp" name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>

Related

How to send dropdown value to servlet NOT the option text

I have dropdown with options and the values. I can get the option value by the dropdown name in servlet but how can i get the dropdown "value" in servlet. In screenshot, temporarily i concatenated the with options but i want to store value in variable in servlet.
Please help:
HTML:
<input type="text" name="taxiDropdown" id= "taxiDropdown" placeholder="Search taxi...">
</div>
<div class="scrolling menu">
<%
List eList = (ArrayList) session.getAttribute("taxiInfo");
%>
<%
for (int i = 0; i < eList.size(); i++) {
%>
<div class="item" data-value="<%=((TaxiInfo) eList.get(i)).getID()%>">
<div class="ui green empty circular label"></div>
<%=((TaxiInfo) eList.get(i)).getTaxiPlate() +" "+ ((TaxiInfo) eList.get(i)).getID() %>
</div>
<%
}
%>
</div>
</div>
Servlet:
String val = request.getParameter("taxiDropdown");
(in "val", I want to store the value of the dropdown not the option text)
In JSP you should have something like that:
<form method="post">
<select name="taxiDropdown" id="taxiDropdown">
<%
List<TaxiInfo> eList = (List<TaxiInfo>) request.getAttribute("taxiInfo");
for (TaxiInfo taxiInfo : eList) {
%>
<option name="<%=taxiInfo.getTaxiPlate()%>" value="<%=taxiInfo.getID()%>"><%=taxiInfo.getTaxiPlate()%></option>
<%
}
%>
</select>
<input type="submit" />
</form>
Then in controller/servlet you will receive the id of TaxiInfo:
String val = request.getParameter("taxiDropdown");
System.out.println(val);
Or in your case you should set a hidden input with javascript with desired value.
added this code in html:
Move value of the dropdown selection to hidden textbox
<script type='text/javascript'>
$(function() {
$('#driverdp').change(function() { <-- this is my dropdown -->
var x = $(this).val();
$('#driverid').val(x); <-- this is my textbox -->
});
});
</script>
Servlet:
get the value of hidden text in servlet
String text= request.getParameter("driverId");
hope it will help someone

Hyperlink in JSP to add item to basket

I am trying to add products (in viewProduct.jsp) to a basket (basket.jsp).
I'm pretty new to JSP. I know it's done by using an appropriate hyperlink to basket.jsp in viewProduct.jsp. I've written a long line in <p> tags in viewProduct.jsp where I'd like the hyperlink to be. Thanks
viewProduct.jsp
<%# page import="shop.Product"%>
<jsp:useBean id='db'
scope='session'
class='shop.ShopDB' />
<html>
<head>
<title>My Shop</title>
</head>
<body>
<%
String pid = request.getParameter("pid");
Product product = db.getProduct(pid);
// out.println("pid = " + pid);
if (product == null) {
// do something sensible!!!
out.println( product );
}
else {
%>
<div align="center">
<h2> <%= product.title %> by <%= product.artist %> </h2>
<img src="<%= product.fullimage %>" />
<p> <%= product.description %> </p>
<p> -------------------------- link goes here -------------------</p>
</div>
<%
}
%>
</body>
</html>
basket.jsp
<%# page import="java.util.Collection,
java.util.Iterator"%>
<jsp:useBean id='basket'
scope='session'
class='shop.Basket'
/>
<%
String empty = request.getParameter("emptyBasket");
if (empty!=null) {
basket.clearBasket();
}
String item = request.getParameter("addItem");
basket.addItem(item);
%>
<html>
<body>
<% Collection items = basket.getItems();
for (Iterator i = items.iterator(); i.hasNext(); ) {
out.println( "<p>" + i.next() + "</p>" );
}
%>
<p> Order total = <%= basket.getTotalString() %>
<%
if ( basket.getTotal() > 0) {
%>
<form action="order.jsp" method="post">
<input type="text" name="name" size="20">
<input type="submit" value="Place Order" />
</form>
<form action="basket.jsp" method="get">
<input type="hidden" name="emptyBasket" value="yes">
<input type="submit" value="Empty Basket" />
</form>
<%
}
%>
</body>
</html>
As you can see from your basket.jsp if you have a parameter called addItem, then it will be added to the basket
so how about
<p> <%= product.description %> </p>
<!-- use pid -->
<a href="basket.jsp?additem="<%=pid%>><%= product.description %></a>
Although use of JSTL tags would be nicer.
See http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

display selected value of the drop down in jsp page

I want to display selected value. In the text field I can display it within value like below
value ="<%=event_data.getE_venue()%>"
code :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" value ="<%=event_data.getE_target_category()%>" id="single1">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
but in dropdown box it doesn't work.
please help me. thanks..
Firstly, select doesn't work in that way , you need to put selected attribute in option that matches your input.
for example:
<option selected='selected'>Sports</option>
check this fiddle :
http://jsfiddle.net/ZLTS7/
your code should be something like :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" id="single1">
<option <%= (event_data.getE_target_category().equals("Sports")?"selected='selected'":"") %>>Sports</option>
<option <%= (event_data.getE_target_category().equals("Corporate")?"selected='selected'":"") %>>Corporate</option>
<option <%= (event_data.getE_target_category().equals("Religious")?"selected='selected'","") %>>Religious</option>
<option <%= (event_data.getE_target_category().equals("Music")?"selected='selected'":"") %>>Music</option>
</select>
You need to change the dropdown value through Javascript or jQuery. You can assign desired value to dropdown just like input
var dd = document.getElementById('single1');
var opts = ddl.options.length;
var value = <%=event_data.getE_venue()%>;
for (var i=0; i<opts; i++){
if (dd.options[i].value == value){
dd.options[i].selected = true;
break;
}
}
or if you are using jQuery.
$("#single1").val(value);
See this example:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#slectboxid option').click(function(){
$('#textboxid').val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="post">
<select name="select" id="slectboxid">
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<input type="text" name="text" id="textboxid" />
</form>
</body>
</html>

dropdown values

I have the below code that display list of values from database in drop down. The query also selects Jt_JOB_Description. I would like to display the Jt_JOB_Description based on the JT_JOB_TITLE selected in Text Area. Help plz.
<select name="jTitle" id="jTitle" style="background-color: #D8D8D8">
<%
Statement stt = conn.createStatement();
ResultSet rstt = stt.executeQuery("SELECT JT_JOB_TITLE, JT_JOB_DESCRIPTION FROM uap.dbo.UAP_JOB_TITLE ORDER BY JT_JOB_TITLE");
while (rstt.next()) {
%>
<option value="<%=rstt.getString("JT_JOB_TITLE")%>"> <%=rstt.getString("JT_JOB_TITLE")%>
</option>
<%
}
stt.close();
rstt.close();
%>
</select>
<textarea class="styled" rows="5" cols="12" name="jDesc" id="jDesc" ></textarea>
Modify your code as given below
<select name="jTitle" id="jTitle" style="background-color: #D8D8D8" onchange="setText(this)">
<%
String str="";
Statement stt = conn.createStatement();
ResultSet rstt = stt.executeQuery("SELECT JT_JOB_TITLE, JT_JOB_DESCRIPTION FROM uap.dbo.UAP_JOB_TITLE ORDER BY JT_JOB_TITLE");
while (rstt.next()) {
str+=rstt.getString(1)+"#"+rstt.getString(2)+"#";
%>
<option value="<%=rstt.getString("JT_JOB_TITLE")%>"><%=rstt.getString("JT_JOB_TITLE")%>
</option>
<%
}
stt.close();
rstt.close();
%>
</select>
<input type="hidden" name="txtHidStr" id="txtHidStr" value="<%=str %>" />
<textarea class="styled" rows="5" cols="12" name="jDesc" id="jDesc" ></textarea>
and wirte following javascript function
function setText(element){
var arr_main=document.getElementById("txtHidStr").value.split("#");
for(i=0;i<arr_main.length;i++)
{
arr_val=arr_main[i].split("#");
if(arr_val[0]==element.value)
{
document.getElementById("jDesc").innerHTML=arr_val[1];
break;
}
}
}

Form Not Submitting after Jquery Validation

$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>
Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.

Categories

Resources