OnChange event extracting value from Select incorrectly

R

Robert Carlson

I am trying to extract the value from a selected item in a
Select/Option, parse it server side using asp and then populate a text
box on the same page with the value that was parsed. simple example
below:

Two problems I have here:

1. The code: this.frmEditUser.Submit; does not appear to submit the
form.
2. If I submit the form using the Submit button, it works except I
get:
"ROBERT" instead of "ROBERT CARLSON - 7788"

<%
Dim sUserId
sUserId = Request.Form("cmbName")
''' Parse: "7788" from "ROBERT CARLSON - 7788"
%>

<html>
<head>
<title>Test Page</title>
</head>

<script language="javaScript">
function GetUserId()
{
alert("GetUserId called");
this.frmEditUser.Submit;
}
</script>

// This page is duly named: "droplistevent.asp"

<BODY>
<form name=frmEditUser action="droplistevent.asp" method="post">
<select size="1" name="cmbName" tabindex="7"
OnChange="GetUserId()">
<option selected>Choose A Name</option>
<option value=ROBERT CARLSON - 7788 > ROBERT CARLSON - 7788
</option>
</select>
<input type="text" name="txtNewId" size="14" tabindex="2"
value=<%=sUserId%>>
<input type="submit" value="Get User Id" name="cmdGetId"
tabindex="8">
</from>
</body>
</html>

Thanks for any support;
Robert
 
G

Grant Wagner

Robert said:
I am trying to extract the value from a selected item in a
Select/Option, parse it server side using asp and then populate a text
box on the same page with the value that was parsed. simple example
below:

Two problems I have here:

1. The code: this.frmEditUser.Submit; does not appear to submit the
form.

The name of the client-side method is "submit()", not "Submit()".
2. If I submit the form using the Submit button, it works except I
get:
"ROBERT" instead of "ROBERT CARLSON - 7788"

<option value=ROBERT CARLSON - 7788 > ROBERT CARLSON - 7788

Quote your value attributes:

<input type="text" name="txtNewId" size="14" tabindex="2"
value=<%=sUserId%>>

Same here:

<input type="text" name="txtNewId" size="14" tabindex="2"
value="<%=sUserId%>">


BE AWARE of possible line breaks or double-quotation marks in your
server-side values. They need to either be escaped or removed/modified.
 
R

Richard Cornford

Robert Carlson wrote:
Two problems I have here:

1. The code: this.frmEditUser.Submit; does not appear
to submit the form.

No it wouldn't. it is a MemberExpression and you want a CallExpression:-

this.frmEditUser.Submit();

However, in the context of the - GetUserId - the - this - keyword is a
reference to the global object and the form is not necessarily going to
be available as a named member of the global object. Access through
the - document.forms - collection is more cross-browser, and passing a
reference to the form from the event handler would probably be easiest
to write and maintain (as the form object becomes anonymous within the
function.

2. If I submit the form using the Submit button, it
works except I get:
"ROBERT" instead of "ROBERT CARLSON - 7788"
<option value=ROBERT CARLSON - 7788 > ROBERT CARLSON - 7788

The value attribute in the HTML is not surrounded with quote marks and
as a result the parser is likely to regard the first space character
encountered as ending the attribute's string and the following words,
punctuation and number as either authoring errors or custom attributes.
Authoring valid HTML (even with server side scripts) is an effective and
objectively verifiable way of avoiding many scripting errors and can be
recommended for that reason alone.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top