File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes pass a variable in the URl Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "pass a variable in the URl" Watch "pass a variable in the URl" New topic
Author

pass a variable in the URl

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Hi,

I want to pass a variable in the URL. I tries the following but its giving me an error:

function abc()
{
var iIndex = par.rowIndex;
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries?rowNumber = iIndex ";
}

Please help.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Incorrect syntax.

It should be
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries&rowNumber ="+ iIndex;

I am assuming that iIndex is a java script variable. I am right, right?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

Deeps Mistry wrote:Hi,

I want to pass a variable in the URL. I tries the following but its giving me an error:

function abc()
{
var iIndex = par.rowIndex;
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries?rowNumber = iIndex ";
}

Please help.


use:



if this not work then try to take it like a string and then variable and concate


Dhruva
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

Sam is quite right
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Incorrect syntax.

It should be
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries&rowNumber ="+ iIndex;

I am assuming that iIndex is a java script variable. I am right, right?


Hey its working fine. I am able to see the value in the URL. But in my Action class i am getting a null value if i do this:
System.out.println("rownumber:"+request.getParameter("rowNumber"));

What should i do?

Thanks.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Deeps Mistry wrote:Incorrect syntax.

It should be
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries&rowNumber ="+ iIndex;

I am assuming that iIndex is a java script variable. I am right, right?


Hey its working fine. I am able to see the value in the URL. But in my Action class i am getting a null value if i do this:
System.out.println("rownumber:"+request.getParameter("rowNumber"));

*EDIT*
I also tried this:
<html:hidden name="ViewChallanForm" property="iRowNumber" value="0"/>
<html:text name="Challan" property="perbox" styleId='<%="pb" + ids%>' disabled="true" size="5" onchange="calcBBSeries(this);"></html:text>

var iIndex = par.rowIndex;
alert(iIndex)
document.forms[0].iRowNumber.value = iIndex;
alert(document.forms[0].iRowNumber.value)

I am getting both the alerts correct. But when i access the hidden variable in my Action Class i get value = 0

System.out.println("rn:"+objViewChallanForm.getIRowNumber());

What should i do?




Early replies would be highly appreciated. Thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
When a form element is disabled, the value is NOT sent to the server.

Eric
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Eric Pascarello wrote:When a form element is disabled, the value is NOT sent to the server.

Eric


Hi,

The text box gets enabled first and then the javascript function is called. I really cannot figure out the problem.

Thanks.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Its pretty difficult to say whats going wrong looking at the code that you have posted. Could you please post the entire HTML page and Java Script?
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Its pretty difficult to say whats going wrong looking at the code that you have posted. Could you please post the entire HTML page and Java Script?


Here is my jsp file:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el"%>
<html>
<head>

<meta name="Description"
content="Information architecture, Web Design, Web Standards." />
<meta name="Keywords" content="your, keywords" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Distribution" content="Global" />
<meta name="Author" content="Erwin Aligam - ealigam@gmail.com" />
<meta name="Robots" content="index,follow" />
<html:base />
<link rel="stylesheet" href="images/Refresh.css" type="text/css" />


<title>PEW // View Challan</title>


<style type="text/css">
<!--
a:hover {
color: #003061;
}

h2 {
color: #044A92;
}

.style1 {
color: #555;
font-size: 150%;
}
-->
</style>
<script type="text/javascript">
var req;
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->



function enableThis(checkedStr)
{
var par=checkedStr.parentNode;

while(par.nodeName.toLowerCase()!='tr')
{
par=par.parentNode;
}
qty = "qty"+ par.rowIndex;
pb = "pb"+ par.rowIndex;
nbl = "nbl"+ par.rowIndex;
dbl = "dbl"+ par.rowIndex;
bbs = "bbs"+ par.rowIndex;
document.getElementById(qty).disabled = !(checkedStr.checked);
document.getElementById(pb).disabled = !(checkedStr.checked);
document.getElementById(nbl).disabled = !(checkedStr.checked);
document.getElementById(dbl).disabled = !(checkedStr.checked);
document.getElementById(bbs).disabled = !(checkedStr.checked);


}
function submitForm()
{
document.forms[0].submit();
}



function calcBBSeries(value)

{
var par=value.parentNode;

while(par.nodeName.toLowerCase()!='tr')
{
par=par.parentNode;
}
var iIndex = par.rowIndex;
alert(iIndex)
document.forms[0].iRowNumber.value = iIndex;
alert(document.forms[0].iRowNumber.value)
//document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries&rowNumber ="+ iIndex;

document.forms[0].submit();

}
</script>

</head>

<body>
<!-- wrap starts here -->
<div id="wrap"><!--header -->
<div id="header">
<div align="left" class="float-left"></div>
<div align="center"></div>
</div>

<!-- menu -->
<div id="menu">

</div>

<!-- content-wrap starts here -->
<div id="content-wrap">

<div id="sidebar">

<h1>Challans / Invoices</h1>
<div class="left-box">

</div>

<h1>Customers / P.O.</h1>

  • <html:link action="/customer">Add/Show Customer</html:link>

  • <html:link action="/custName.do">Add a P.O.</html:link>

  • Purchase Orders


<div id="sidebar2">
<h1>Print Carton Labels</h1>
<div class="left-box"></div>
</div>
</div>
<div id="main">
<blockquote>
<h2 class="style1">Challan Entry</h2>
</blockquote>
<html:form action="/challan.do?method=getProducts">
<table border="0"
>
<tr>
<td ><label>Challan No:</label></td>
<td ><bean:write name="ViewChallanForm"
property="challanNo" /></td>

<td ><label>  Date:</label></td>
<td></td>

</tr>
<tr>
<td ><label> Customer Name:</label></td>
<td ><bean:write name="ViewChallanForm"
property="custName" /></td>
<td ><label>  A.R.E. No:</label></td>

<logic:equal name="ViewChallanForm" property="areNo" value="0">
<td>NA</td>
</logic:equal>
<logic:notEqual name="ViewChallanForm" property="areNo" value="0">
<td><bean:write name="ViewChallanForm" property="areNo" /></td>
</logic:notEqual>


</tr>
<tr>
<td><label>PONO:</label></td>
<td><bean:write name="ViewChallanForm" property="poNo" /></td>
<td><label>Last Box No.:</label></td>
<td><bean:write name="ViewChallanForm" property="lastboxno" /></td>
</tr>
</table>

<table border="0"
>

<tr>
<html:hidden name="ViewChallanForm" property="iRowNumber" value="0"/>
<td >
<div><strong>S/No.</strong></div>
</td>

<td ><strong> Products</strong></td>
<td ><strong>Quantity </strong></td>
<logic:notEqual name="ViewChallanForm" property="areNo" value="0">
<td>
<div><strong>Per box</strong></div>
</td>
<td >
<div><strong>Nos. of B/L</strong></div>
</td>
<td >
<div><strong>Boxes/Loose  </strong></div>

</td>
<td>
<div><strong>BB Series</strong></div>
</td>
</logic:notEqual>

</tr>
<%
int i = 1;
%>
<%
int ids = 1;
%>
<logic:iterate id="Challan" name="Challan">
<tr>

<td><%=i%>.</td>

<td><html:multibox name="Challan" property="checkedProducts"
styleId="multi_box" onclick="enableThis(this);">
<bean:write name="Challan" property="unit" />
</html:multibox>  <bean:write name="Challan" property="products" /></td>
<td><html:text name="Challan" property="quantity"
styleId='<%="qty" + ids%>' size="6" disabled="true" /></td>
<logic:notEqual name="ViewChallanForm" property="areNo" value="0">
<td>

<html:text name="Challan" property="perbox"
styleId='<%="pb" + ids%>' disabled="true" size="5"
onchange="calcBBSeries(this);"></html:text></td>
<td><html:text name="Challan" property="nos_BL" size="5"
styleId='<%="nbl" + ids%>' disabled="true"></html:text></td>
<td><html:select name="ViewChallanForm" property="BL"
disabled="true" styleId='<%="dbl" + ids%>'>
<htmlption value="Select"></htmlption>
<htmlption value="Boxes"></htmlption>
<htmlption value="Loose"></htmlption>
</html:select></td>

<td><html:text name="Challan" property="bbSeries" size="7"
styleId='<%="bbs" + ids%>' disabled="true" /></td>
</logic:notEqual>
</tr>
<%
i++;
ids++;
%>

</logic:iterate>

</table>

</html:form>

<div align="right"><html:button property="">Save</html:button></div>

 


<div align="center"></div>
</div>

<!-- content-wrap ends here --></div>

<!--footer starts here-->
<div id="footer">

 



</div>

<!-- wrap ends here --></div>

</body>
</html>


I have tried two things:
1) red color: pass a variable in URL
2) blue color: through hidden varibale.

But in both the cases i am not able to retrieve the value in my Action Class.

Thanks
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Your code looks correct. At least nothing seems wrong, the second method of using a hidden variable should have worked. However it would be a lot better if you post the generated HTML code, since I can run that in my browser to see whats happening.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Your code looks correct. At least nothing seems wrong, the second method of using a hidden variable should have worked. However it would be a lot better if you post the generated HTML code, since I can run that in my browser to see whats happening.


Here is the generated html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<meta name="Description"
content="Information architecture, Web Design, Web Standards." />
<meta name="Keywords" content="your, keywords" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Distribution" content="Global" />
<meta name="Author" content="Erwin Aligam - ealigam@gmail.com" />
<meta name="Robots" content="index,follow" />
<base href="http://localhost:8080/struts-blank/pages/viewchallan.jsp">
<link rel="stylesheet" href="images/Refresh.css" type="text/css" />


<title>PEW // View Challan</title>


<style type="text/css">
<!--
a:hover {
color: #003061;
}

h2 {
color: #044A92;
}

.style1 {
color: #555;
font-size: 150%;
}
-->
</style>
<script type="text/javascript">
var req;
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->



function enableThis(checkedStr)
{
var par=checkedStr.parentNode;

while(par.nodeName.toLowerCase()!='tr')
{
par=par.parentNode;
}
qty = "qty"+ par.rowIndex;
pb = "pb"+ par.rowIndex;
nbl = "nbl"+ par.rowIndex;
dbl = "dbl"+ par.rowIndex;
bbs = "bbs"+ par.rowIndex;
document.getElementById(qty).disabled = !(checkedStr.checked);
document.getElementById(pb).disabled = !(checkedStr.checked);
document.getElementById(nbl).disabled = !(checkedStr.checked);
document.getElementById(dbl).disabled = !(checkedStr.checked);
document.getElementById(bbs).disabled = !(checkedStr.checked);


}
function submitForm()
{
document.forms[0].submit();
}

function calcBBSeries(value)

{
var par=value.parentNode;

while(par.nodeName.toLowerCase()!='tr')
{
par=par.parentNode;
}
var iIndex = par.rowIndex;
alert(iIndex)
document.forms[0].iRowNumber.value = iIndex;
alert(document.forms[0].iRowNumber.value)
//document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries&rowNumber ="+ iIndex;

document.forms[0].submit();

}


</script>

</head>

<body>
<!-- wrap starts here -->
<div id="wrap"><!--header -->
<div id="header">
<div align="left" class="float-left"></div>
<div align="center"></div>
</div>

<!-- menu -->
<div id="menu">

</div>

<!-- content-wrap starts here -->
<div id="content-wrap">

<div id="sidebar">

<h1>Challans / Invoices</h1>
<div class="left-box">

</div>

<h1>Customers / P.O.</h1>

<div id="sidebar2">
<h1>Print Carton Labels</h1>
<div class="left-box"></div>
</div>
</div>
<div id="main">
<blockquote>
<h2 class="style1">Challan Entry</h2>
</blockquote>
<form name="ViewChallanForm" method="post" action="/struts-blank/challan.do?method=getProducts">
<table border="0"
>
<tr>
<td ><label>Challan No:</label></td>
<td >1</td>

<td ><label>  Date:</label></td>
<td></td>

</tr>
<tr>
<td ><label> Customer Name:</label></td>
<td >Air India Ltd.</td>
<td ><label>  A.R.E. No:</label></td>



<td>1</td>



</tr>
<tr>
<td><label>PONO:</label></td>
<td>40008274</td>
<td><label>Last Box No.:</label></td>
<td>0</td>
</tr>
</table>

<table border="0"
>

<tr>
<input type="hidden" name="iRowNumber" value="0">
<td >
<div><strong>S/No.</strong></div>
</td>

<td ><strong> Products</strong></td>
<td ><strong>Quantity </strong></td>

<td>
<div><strong>Per box</strong></div>
</td>
<td >
<div><strong>Nos. of B/L</strong></div>
</td>
<td >
<div><strong>Boxes/Loose  </strong></div>

</td>
<td>
<div><strong>BB Series</strong></div>
</td>


</tr>



<tr>

<td>1.</td>

<td><input type="checkbox" name="checkedProducts" value="nos." onclick="enableThis(this);" id="multi_box">  ATLAS Drawer</td>
<td><input type="text" name="quantity" size="6" value="50" disabled="disabled" id="qty1"></td>

<td>

<input type="text" name="perbox" size="5" value="0" onchange="calcBBSeries(this);" disabled="disabled" id="pb1"></td>
<td><input type="text" name="nos_BL" size="5" value="0" disabled="disabled" id="nbl1"></td>
<td><select name="BL" disabled="disabled" id="dbl1"><option value="Select" selected="selected">Select</option>
<option value="Boxes">Boxes</option>
<option value="Loose">Loose</option></select></td>

<td><input type="text" name="bbSeries" size="7" value="-" disabled="disabled" id="bbs1"></td>

</tr>



<tr>

<td>2.</td>

<td><input type="checkbox" name="checkedProducts" value="nos." onclick="enableThis(this);" id="multi_box">  Standard Units</td>
<td><input type="text" name="quantity" size="6" value="70" disabled="disabled" id="qty2"></td>

<td>

<input type="text" name="perbox" size="5" value="0" onchange="calcBBSeries(this);" disabled="disabled" id="pb2"></td>
<td><input type="text" name="nos_BL" size="5" value="0" disabled="disabled" id="nbl2"></td>
<td><select name="BL" disabled="disabled" id="dbl2"><option value="Select" selected="selected">Select</option>
<option value="Boxes">Boxes</option>
<option value="Loose">Loose</option></select></td>

<td><input type="text" name="bbSeries" size="7" value="-" disabled="disabled" id="bbs2"></td>

</tr>



<tr>

<td>3.</td>

<td><input type="checkbox" name="checkedProducts" value="nos." onclick="enableThis(this);" id="multi_box">  ATLAS Oven Cage</td>
<td><input type="text" name="quantity" size="6" value="80" disabled="disabled" id="qty3"></td>

<td>

<input type="text" name="perbox" size="5" value="0" onchange="calcBBSeries(this);" disabled="disabled" id="pb3"></td>
<td><input type="text" name="nos_BL" size="5" value="0" disabled="disabled" id="nbl3"></td>
<td><select name="BL" disabled="disabled" id="dbl3"><option value="Select" selected="selected">Select</option>
<option value="Boxes">Boxes</option>
<option value="Loose">Loose</option></select></td>

<td><input type="text" name="bbSeries" size="7" value="-" disabled="disabled" id="bbs3"></td>

</tr>



<tr>

<td>4.</td>

<td><input type="checkbox" name="checkedProducts" value="kg" onclick="enableThis(this);" id="multi_box">  ATLAS Oven Tray</td>
<td><input type="text" name="quantity" size="6" value="100" disabled="disabled" id="qty4"></td>

<td>

<input type="text" name="perbox" size="5" value="0" onchange="calcBBSeries(this);" disabled="disabled" id="pb4"></td>
<td><input type="text" name="nos_BL" size="5" value="0" disabled="disabled" id="nbl4"></td>
<td><select name="BL" disabled="disabled" id="dbl4"><option value="Select" selected="selected">Select</option>
<option value="Boxes">Boxes</option>
<option value="Loose">Loose</option></select></td>

<td><input type="text" name="bbSeries" size="7" value="-" disabled="disabled" id="bbs4"></td>

</tr>




</table>

</form>

<div align="right"><input type="button" name="" value="Save"></div>

 


<div align="center"></div>
</div>

<!-- content-wrap ends here --></div>

<!--footer starts here-->
<div id="footer">

 



</div>

<!-- wrap ends here --></div>

</body>
</html>


Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

I don't get your code at all - however everything is still working.
You change the value of document.forms[0].iRowNumber.value in the function calcBBSeries() which is called when you enter a value for 'Per Box', the value is iRowNumber is updated and the form gets submitted - not sure why the form gets submitted here.

Also the save button is really do nothing. It neither calls a java script function nor submits the form.

You might want to re look all you functions and function calls.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:I don't get your code at all - however everything is still working.
You change the value of document.forms[0].iRowNumber.value in the function calcBBSeries() which is called when you enter a value for 'Per Box', the value is iRowNumber is updated and the form gets submitted - not sure why the form gets submitted here.

Also the save button is really do nothing. It neither calls a java script function nor submits the form.

You might want to re look all you functions and function calls.


Hey..when i enter a value in perbox i call calcBBSeries() function and call my aciton class--->calculate my bbseries---> return back to the jsp and print it .

I have not implemented the Save functionality yet.

My concern is passing the value in the url and fetching it in my action class.

Thanks
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Well when you call the calcBBSeries() the value of iRowNumber is being passed correctly. You can verify this by changing the method to 'get' instead of the default post. So nothing wrong with the value being passed.

For e.g. when I edit the 3rd row, the URL being called is

http://localhost:8080/struts-blank/challan.do?iRowNumber=3&checkedProducts=nos.&quantity=80&perbox=25&nos_BL=0&BL=Select&bbSeries=-

which is correct!
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:Well when you call the calcBBSeries() the value of iRowNumber is being passed correctly. You can verify this by changing the method to 'get' instead of the default post. So nothing wrong with the value being passed.

For e.g. when I edit the 3rd row, the URL being called is

http://localhost:8080/struts-blank/challan.do?iRowNumber=3&checkedProducts=nos.&quantity=80&perbox=25&nos_BL=0&BL=Select&bbSeries=-

which is correct!

Yeah..i am able to pass the correct value in the url but i am not able to fetch it in my java class.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Deeps Mistry wrote:
Yeah..i am able to pass the correct value in the url but i am not able to fetch it in my java class.


Well then there is simple some thing wrong in the way your Java code is trying to retrieve the value. You might want to have a look at it again - the Java Script part is working fine. If you are still unable to root into the issue you might have to tell us exactly what you Java code is and what it is trying to do.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sam Mercs wrote:
Deeps Mistry wrote:
Yeah..i am able to pass the correct value in the url but i am not able to fetch it in my java class.


Well then there is simple some thing wrong in the way your Java code is trying to retrieve the value. You might want to have a look at it again - the Java Script part is working fine. If you are still unable to root into the issue you might have to tell us exactly what you Java code is and what it is trying to do.


here is my java code:

which is getting printed as
rno: 0

Thanks
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

You should be using request.getParameter() and not request.getAttribute()...

Why use request object in a struts application anyways. Use the form bean instead!!

Anyways, changing to request.getParameter() should do the trick!!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Please use code tags!!

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass a variable in the URl
 
Similar Threads
Calling Perl procedures using Java remotely
class instance variables modifying
pulling my hair out
pass by value or pass by reference
Multiple File Upload using Struts