Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

submit or return value from <script> code

Status
Not open for further replies.

robertl

Programmer
Jan 8, 2001
23
GB
I have the following piece of script near the beginning of my page.
Code:
<script>
function navcom() {
  comm=document.bookform.bcommodity.selectedIndex;
  commval=document.bookform.bcommodity.options[comm].value;
  document.write(commval);}
</script>

further down I have the following scriplet.
Code:
      <td>Commodity:</td>
      <td>
         <SELECT NAME=&quot;bcommodity&quot; onClick=&quot;navcom()&quot;> 
	       <% 
                 while (!rs.eof) {
                    val=rs(&quot;commodity_desc&quot;);
                    Response.Write('<OPTION value=&quot;' + val + '&quot;>');
                    Response.Write(val);
                    Response.Write(&quot;<BR>&quot;);
                 rs.MoveNext();
                 }
               %> 
        </SELECT>

The scriplet poplulates a combo box and what happens is
after the user selects a option from the list, the
function navcom() is executed and the name of what is selected is displayed on a blank page.

What I'm wanting to do is submit what is being displayed by the function to my database. However, when trying to figure this out, I attempted to refer to &quot;commval&quot; in my code immediately above and received an error instead.

As the code sits, the value being displayed is correct, but since all of the code is part of a page that will submit data to a database, this is one of the fields (the &quot;commval&quot;
value) that needs to be submitted as well.

How would I go about accomplishing this?

Thanks.
 
Show me the entire page and i will provide you with an answer. Can't say fairer than that, eh?
DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
Here's the entire page

Code:
<%
var connect = Server.CreateObject(&quot;ADODB.Connection&quot;);
var rs = Server.CreateObject(&quot;ADODB.Recordset&quot;);
connect.Open(&quot;DSN=verify&quot;);
var SQL = &quot;SELECT commodity_desc FROM Commodities ORDER BY commodity_desc&quot;;
rs.Open(SQL, connect); 
%>


<script>
function navcom() {
  comm=document.bookform.bcommodity.selectedIndex;
  commval=document.bookform.bcommodity.options[comm].value;
  document.write(commval);}
</script>


<html>
<head>
<title></title>
</head>

<%
  gotUID = Session(&quot;UID&quot;)				//captured userid returned from database from login screen				
%>

<body>
<center>
<h1>Cargo booking screen</h1>
</center>

<FORM ACTION=&quot;addbookcargo.asp&quot; METHOD=&quot;post&quot; NAME=&quot;bookform&quot;>
<input type=&quot;hidden&quot; name=&quot;UID&quot; value=&quot;<%=gotUID%>&quot;>

<table>
  <tr>
      <td>Shipper name:</td>
      <td><input type=&quot;text&quot; name=&quot;bshippername&quot;></td>
  </tr>

<TR>  </TR>
  <tr>
      <td>Commodity:</td>
      <td>
         <SELECT NAME=&quot;bcommodity&quot; onClick=&quot;navcom()&quot;> 
	       <% 
                 while (!rs.eof) {
                    val=rs(&quot;commodity_desc&quot;);
                    Response.Write('<OPTION value=&quot;' + val + '&quot;>');
                    Response.Write(val);
                    Response.Write(&quot;<BR>&quot;);
                 rs.MoveNext();
                 }
               %> 
        </SELECT>
      </td>
      <td align=&quot;right&quot;>Quantity:</td>
      <td><input type=&quot;text&quot; name=&quot;bquantity&quot;></td>
  </tr>
<TR>  </TR>
  <tr>
      <td>Vessel:</td>
      <td><input type=&quot;text&quot; name=&quot;bvessel&quot;></td>
      <td align=&quot;center&quot;><b>OR</b></td>
      <td>Month of loading:</td>
      <td><input type=&quot;text&quot; name=&quot;bmonloading&quot;></td>
  </tr>
<TR>  </TR>
  <tr>
      <td>Load port:</td>
      <td><input type=&quot;text&quot; name=&quot;bloadport&quot;></td>
      <td align=&quot;right&quot;>Discharge port:</td>
      <td><input type=&quot;text&quot; name=&quot;bdischport&quot;></td>
  </tr>
</table>

<BR><BR>
<center>
   <input type=&quot;submit&quot; name=&quot;Submit&quot;>
</center>

</FORM>
</body>
</html>

Thanks.
 
Hey, Robbie.

I am not entirely sure that this will work, I haven't had time to create the tables on my server to fully test it properly but i think that the coding as follows should work.

If you have any problems with it then please let me know.

P.S. the changes i have made are highlighted in red, there may be other minor mods, but i can't remember what they were if they exist at all.

Good Luck,(-:


<%
set connect = server.CreateObject(&quot;ADODB.Connection&quot;)
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

SQL = &quot;SELECT commodity_desc FROM Commodities ORDER BY commodity_desc&quot;
connect.Open(&quot;DSN=verify&quot;)
rs.Open SQL, connect, 1
%>
<script>
function navcom()
{
comm=document.bookform.bcommodity.selectedIndex;
commval=document.bookform.bcommodity.options&amp;#91;comm].value;
document.write(commval);
}
</script>


<html>
<head>
<title></title>
</head>

<%
gotUID = Session(&quot;UID&quot;) : 'captured userid returned from database from login screen
%>

<body>
<center>
<h1>Cargo booking screen</h1>
</center>

<FORM ACTION=&quot;addbookcargo.asp&quot; METHOD=&quot;post&quot; NAME=&quot;bookform&quot;>
<input type=&quot;hidden&quot; name=&quot;UID&quot; value=&quot;<%=gotUID%>&quot;>

<table>
<tr>
<td>Shipper name:</td>
<td><input type=&quot;text&quot; name=&quot;bshippername&quot;></td>
</tr>

<TR> </TR>
<tr>
<td>Commodity:</td>
<td>
<SELECT NAME=&quot;bcommodity&quot; onClick=&quot;navcom()&quot;>

<%
while not (rs.eof)
val=rs(&quot;commodity_desc&quot;)
Response.Write &quot;<option value=&quot; &amp; val &amp; &quot;>&quot; &amp; val &amp; &quot;</option>&quot;
rs.MoveNext()
wend
%>


</SELECT>
</td>
<td align=&quot;right&quot;>Quantity:</td>
<td><input type=&quot;text&quot; name=&quot;bquantity&quot;></td>
</tr>
<TR> </TR>
<tr>
<td>Vessel:</td>
<td><input type=&quot;text&quot; name=&quot;bvessel&quot;></td>
<td align=&quot;center&quot;><b>OR</b></td>
<td>Month of loading:</td>
<td><input type=&quot;text&quot; name=&quot;bmonloading&quot;></td>
</tr>
<TR> </TR>
<tr>
<td>Load port:</td>
<td><input type=&quot;text&quot; name=&quot;bloadport&quot;></td>
<td align=&quot;right&quot;>Discharge port:</td>
<td><input type=&quot;text&quot; name=&quot;bdischport&quot;></td>
</tr>
</table>

<BR><BR>
<center>
<input type=&quot;submit&quot; name=&quot;Submit&quot;>
</center>

</FORM>
</body>
</html>
DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
Hi! No go.

When I change
Code:
	       <% 
		 while (!rs.eof) {
		   val=rs(&quot;commodity_desc&quot;);
		   Response.Write('<option value=&quot;' + val + '&quot;>');
		   Response.Write(val);
		   Response.Write(&quot;<BR>&quot;); 
		   rs.MoveNext();
		 }
               %>
to
Code:
<% 
                 while ({rs.eof) {
                    val=rs(&quot;commodity_desc&quot;);
                    Response.Write &quot;<option value=&quot; &amp; val &amp; &quot;>&quot; &amp; val &amp; &quot;</option>&quot;;
                    rs.MoveNext();
                 }
%>
The value being returned from the navcom() function is only part of a word (i.e. instead of returning steel coils only steel is returned.

So I'm back to square one again. The value being returned by the navcom() function is correct, but I have to figure out how to pass that value to a variable that I can use in the <% %> tags so I can insert the value into my database later.

Cheers!
 
Maybe try adding Var commval to that first block so it's a global variable. By doing that, you can access it from other parts of the code, not just inside the navcom function.

You could also put a hidden field in your form and set its value to commval during the navcom function instead of &quot;document.write&quot;.
 
Already tried. No matter if I include var or not in front of commval when I try referencing commval in say:
Code:
<input type=&quot;hidden&quot; name=&quot;test&quot; value=&quot;<%=commval%>
I receive an error that &quot;commval is not defined&quot; It's as if I can't pass a variable from
within the <script> tag to the scriptlet ( <% ) tag.

 
Sorry rob, forgot to add the 's to the coding

This,
<%
while ({rs.eof) {
val=rs(&quot;commodity_desc&quot;);
Response.Write &quot;<option value=&quot; &amp; val &amp; &quot;>&quot; &amp; val &amp; &quot;</option>&quot;;
rs.MoveNext();
}
%>

should be this :
<%
while ({rs.eof) {
val=rs(&quot;commodity_desc&quot;);
Response.Write &quot;<option value='&quot; &amp; val &amp; &quot;[/color red]'
>&quot; &amp; val &amp; &quot;</option>&quot;;
rs.MoveNext();
}
%>[/color]

DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
Sorry rob, forgot to add the apostrophe's to the coding

This,
<%
while ({rs.eof) {
val=rs(&quot;commodity_desc&quot;);
Response.Write &quot;<option value=&quot; &amp; val &amp; &quot;>&quot; &amp; val &amp; &quot;</option>&quot;;
rs.MoveNext();
}
%>

should be this :
<%
while ({rs.eof) {
val=rs(&quot;commodity_desc&quot;);
Response.Write &quot;<option value='&quot; &amp; val &amp; &quot;'>&quot; &amp; val &amp; &quot;</option>&quot;;
rs.MoveNext();
}
%>


please not the ' before and after the val.
This will work, again apologies for the stupid mistakes.
DeltaFlyer ;-)

DeltaFlyer - The Only Programmer To Crash With Style.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top