Questions about this do exist on stack but the answers are either wrong or of little use.
Coming from older VB, and being familiar with Java (where the code runs fine on client), trying to populate a textbox server side asp is driving me up the wall. Microsoft's logic is simply inane (regardless of topic).
Yes, it's probably due to a lack of conceptualization on my part, but if the textbox has an id and I can pull data, why can't I write to a different textbox with a named id?
I can write to the body, so why not a text box with a named id? It makes no sense to me. Is there some issue with just text boxes?
Any feedback, esp with a tech explanation of what I fail to understand, would be greatly appreciated.
Very simple code example:
<!DOCTYPE html>
<html><body>
<form action="p3.asp" method="post">
AnyVal: <input type="text" name="AnyVal" size="20" />
Other: <input type="text" name="Other" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim sRet
sRet=Request.Form("AnyVal")
If sRet <> "" Then
Response.Write("Your Val is: " & sret & "!<br>")
Other.value=sRet
End If
%>
</body></html>
Yes, it's probably due to a lack of conceptualization on my part,
I agree with that statement, you simply can't do that ... let me explain it simple taking an extract of recent answer i wrote in the past days
vbscript dropdown in function without using HTML (classic ASP)
classic-asp like almost every other preprocessor languages for web
applications, doesn't have the faculty to interact directly with your
browser. instead the language provides you a set of methods to write
and recieve data from the user-agent (not necesarry a browser).
and the browser relies on HTML,XHTML,CSS and derivatives to construct
an interface to the user, and due to fact that preprocessor doesn't
interact directly with HTML, that its the reason because you can't
make a dropdown in pure vbscript bypassing HTML code.
What are you trying to do is to treat your form elements as objects using ASP language, unfortunately you can't do that because the form elements are not visible to server preprocessor (ASP).
You need to write code to actually on the HTML, because such form elements doesn't exists to the ASP engine
<%
dim sRet
sRet=Request.Form("AnyVal")
If sRet <> "" Then
Response.Write("Your Val is: " & sret & "!<br>")
End If
%>
<!DOCTYPE html>
<html><body>
<form action="p3.asp" method="post">
AnyVal: <input type="text" value="<%=sRet%>" name="AnyVal" size="20" />
Other: <input type="text" value"<%=sRet%>" name="Other" size="20" />
<input type="submit" value="Submit" />
</form>
</body></html>
I hope I'm being clear in my explanation about how really ASP works
Looking at your code I think the easiest way to achieve what you want is the following.
<%
dim sRet
sRet=Request.Form("AnyVal")
%>
<!DOCTYPE html>
<html><body>
<form action="p3.asp" method="post">
AnyVal: <input type="text" name="AnyVal" size="20" />
Other: <input type="text" name="Other" size="20" value="<%= Sret %>" />
<input type="submit" value="Submit" />
<%
If sRet <> "" Then
Response.Write("Your Val is: " & sret & "!<br>")
End If
%>
</form>
</body></html>
Your Classic ASP is server side code and your form elements are client side, so they can't interact with each other.
NB You've tagged this as Classic ASP. If you mean ASP.net then you need to use a server side control - <asp:Textbox> Note that VB and VBScript are different, just as Java and JavaScript are different
Edit - #Rafael beat me to posting this, and he gives a more detailed explanation of more or less the same, so you should probably accept his answer
Thank you for your kind replies, but I think I got it nailed.
I'll post the code below, in case others may find it useful.
I kinda knew that asp & java interact with html differently, but one of the things that threw me was using a javascript variable to get the server side variable.
for example,
var cret;
cret = <%= sret %>
which works if is sret is a number but not if it's a string (don't know why, perhaps someone could enlighten me).
I did figure out getting asp to generate a text box on the fly and populate it.
But, I really wanted the javasript on the client side to do this part. From a practical point of view, I could have just given up and let asp do it, but I became a bit ocb about the whole thing.
I've read a lot of postings, blogs, etc. about this issue. It seemed to me that the proposed solutions were either convoluted of just didn't work. The question seems to get asked a lot, so I was surprised to find no fairly straightforward answer.
Everything I wanted to do could have been done in javascript, even using runat=server. But I specifically wanted to keep some of the code behind the page private. afaik, this needed asp.
I suspect that this is the main reason other want to populate elements via javascript whicle still using asp on the server.
The following code uses a form post, where asp can do some manipulation and generate an input element which can be subseqently accessed/manipulated by javascript. For this example the input element is hidden.
<!DOCTYPE html>
<html><head>
<title>asp java test</title>
</head>
<body>
<form action="p5.asp" method="post">
<p>Paste into the yellow box and then click Go.</p>
Set Val:
<input type="text" id="setval" name="setval" size="30"
style="background-color: #FFFFCC" />
<input type="submit" value="Submit" /><p>
Ret Val:
<input type="text" id="retval" name="retval" size="30" />
</form>
<!-- Server Side -->
<%
dim sret
sret = Request.Form("setval")
If sret = "" Then sret = " Nada"
// <!-- gen output server side-->
response.write("<input type='hidden' id='hid1' name='hid1' value='" & sret & "'/><br>")
%>
<!-- Client Side -->
<script language = "JavaScript">
var cret;
{
// Get the val
cret = document.getElementById("hid1").value;
// Populate the output box
document.getElementById("retval").value = cret;
}
</script>
</body></html>
Seems to work quite well for it's intended purpose.
Gary
Related
I am trying to use my if else statement inside my Function Save() but I cannot make it works? After click on the img button I want to verify if mycheckBox_A is checked. By the way I am using classic ASP
<img src="blah" onclick="<%Save()%>"/>
<%
Function Save()
If mycheckBox_A.checked Then
'mycheckBox_A is checked
Else
'mycheckBox_B is checked
End If
End Function
%>
The onclick attribute is not for calling server-side functions. You can assign JavaScript to this one, but not VB function. Think if you really need server-side code here that's possible that all you need in this exact place could be done with JavaScript.
EDIT:
If you need to do something on the server, you have to use AJAX to send only data you want to the server or submit the form with submit button or JavaScript call to submit() method. After submitting date you have to process it in your server-side function. Check this short tutorial on processing submitted with both POST and GET methods.
According to how to call a ASP function in the onclick event:
ASP functions execute only on the server side, while the onclick event
executes only on the client side. You are trying to mix the two in an
impossible way.
The problem is that the function calculate() does not exist on the
client-side. The browser cannot see it.
You will need to use JavaScript (or another client-side technology) if
you want to have this type of functionality.
This also applies to your Save() function.
You can't use onclick like this. Your only option is to have the button be either a submit button, or a link, then handle it on postback (if you need to do it server-side) or to write it in javascript if it can be done client-side.
Using postback to handle server logic is quite simple and basically how it works in modern ASP. You could have the submit button be in a form which has an integer, showing what you want to do, then handle this at the top of the page. That way, you can postback to the same page, handling different kinds of logic depending on what button was pressed, etc.
Should be noted that this makes for pretty ugly code, but then again, it's classic ASP.NET, it will look ugly no matter what.
No need to use any client script here at all
<%
'Have you POST to this page?
If Len(Request.Form) > 0 Then Call Save()
Sub Save()
Dim a_check, i_item, i_items
a_check = Split(Request.Form("checkbox") & "", ",")
If IsArray(a_check) Then
i_items = UBound(a_check, 1)
For i_item = 0 To i_items
'Could store this and output it later on but is just a quick example
Response.Write a_check(i_item) & " is checked"
Next
End If
End Sub
%>
<html>
<head>
</head>
<body>
<!-- make the page POST to itself -->
<form method="POST" action="">
<! -- your other fields etc
<input type="checkbox" name="checkbox" value="A" />
<input type="checkbox" name="checkbox" value="B" />
<input type="submit" value="Save" />
</form>
</body>
</html>
Quick example nothing special untested.
You can use JQuery post method and pass your variables to classic asp page.
Although I don't consider myself a web programmer, I've done a fair amount of web programming, so I'm almost embarrassed to ask what is wrong with the below code. There is something fundamental about ASP.NET that I must be missing.
I have two pages, source.aspx and destination.aspx:
source.aspx - html:
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
destination.aspx - code behind:
protected void Page_Load(object sender, EventArgs e)
{
// Below variable gets assigned null.
string text1 = Request.Form["Text1"];
}
When I submit the source.aspx form, once it gets to the destination.aspx form, there is no information in the FORM variables. I thought that the forms 'runat="server" ' would ensure that I ended up in the ASP.NET page pipeline, and in fact I can step through this code. There are no POSTed form variables other than viewstate, and the PARAMs collection doesn't have anything corresponding to control data either, not even ones that would correspond to decorated control names. The question is, what is happening that is making my POSTed variables 'disappear', at least to the destination page?
I'm not looking for alternatives how to make this work (i.e. make the controls server controls with runat="server", etc). I can fix the problem. What I am trying to determine is 'what is it about ASP.NET that makes my controls not appear to be receivable by the destination page. Thanks - I thought that I understood HTTP pretty well, but there seems to be a little sleight of hand courtesy of ASP.NET that I'm not seeing.
You can remove the runat="server" off of your form tag since you want to opt out of what ASP.NET gives you with server controls. You are basically thinking correctly that this should work without needing all the ASP.NET page processing bits.
It's a small change you need to make - you need to use 'name' instead of 'id' on your input controls in order for them to appear in the Form collection. It's a subtle thing but I believe it's not exclusive to ASP.NET - the name attribute specifies what to associate the value with in the POST variable collection.
Consult HTML input - name vs. id for more information on id vs. name
Good luck
Request.Form uses name attribute from elements. So you should write name attributes to each of the html elements.
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" name="Text1" type="text" />
<input id="Text2" name="Text2" type="text" />
<input id="Checkbox1" name="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
I have a simple form that passes to an ASPX page to watch a video. The ASPX page uses a hidden field from the form to load the correct video.
This works for me, but a couple other people are the error message because it appears the value isn't being passed. I'm assuming it's a setting in IE... Anyone seen this or know how to fix? OR a better idea?
Simple form on "yourdomain.org"
<form action="http://www.mydomain.org/WatchVideo/Default.aspx" method="post" enctype="multipart/form-data">
<input type="hidden" name="hidden" value="movie.flv" />
<input type="submit" name="submit" value="Watch Video" />
</form>
The ASPX page on "mydomain.org"
If Request.Form("hidden") IsNot Nothing Then
lit.Text = Request.Form("hidden")
Else
Response.Write("Not Authorized. Video Not Passed.")
End If
Sounds and loooks pretty straight forward. Not sure why the same version of IE would have different results. Any other thoughts on how to do this? I need to keep the "off site" coding simple and in HTML for non-technical folks.
Thanks.
It appears to be a setting inside of the browser. I couldn't narrow it down to a specific setting, but once we changed the "Internet" zone to medium from medium-high it began working. It has something to do with posting form data across domains.
I have following form,
<form action="contact_us.asp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="firstname" type="text" id="firstname" size="30" />
<input name="lastname" type="text" id="lastname" size="30" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
But when I am trying to get value of these post variables in my ASP file contact_us.asp then it returns blank. Code is below:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<%
Dim FirstName, LastName, Email, Message
FirstName = request.form("firstname")
LastName = request.form("lastname")
response.write(FirstName & "OK")
%>
Its returning only "OK" to me. nothing in Message variable?
Please help me and tell me what's wrong here?
Classic ASP doesn't provide built-in support for working with `multipart/form-data. This is a surprisingly basic deficiency even for a language of ASP's venerable age, but what're you gonna do about it, move to ASP.NET? (Yes? oh. well never mind then.)
If you aren't doing file uploads, it's easiest just to stick with the default enctype (which is application/x-www-form-urlencoded). The only advantage of multipart/form-data is that you can put file uploads in it. (Theoretically, it would also have the advantage that you can specify character encodings definitively. But no browser actually does that.)
If you do need to handle multipart/form-data in Classic ASP you will need to parse the incoming request body yourself, splitting it into fields and values. Or rather, more typically, use an existing library to do it*.
That library will usually provide separate interfaces for reading the uploaded files and the other form values. This completely replaces use of the Classic ASP Request.Form interface. Exactly where you can find it depends on the library you choose. This does mean that if you want to have a form that can respond to either enctype equally you have to check the type and use one of the two different interfaces.
*: There are loads. for example. I'm not endorsing either of these as such... neither of them actually parse multiparts properly as per the standard and both seem a bit lax about filename security (never store a file under the user-submitted filename! security disaster!). But that seems to be par for the course for ASP upload scripts. At least, unlike many, they're not asking money for them.
I found by removing enctype completely (which defaults to application/x-www-form-urlencoded) from the form tag that Request.Form("SomeInputTagId") worked fine with method="post". I also didn't need to install any third party readers. Hope this helps.
Don't use enctype="multipart/form-data"
Remove that from the code and see if it works. The form-data enctype is used for uploading data, for example image files. You need to access the form elements slightly differently if you use that enctype.
If you are uploading data, then the ASP object you are using (for example ASP Upload) will have functions to access form fields. Request.form("") wont work.
Accessing the form values would be along the lines of:
Set yourUploadComponent = CreateObject("Your.UploadComponentClassString")
sFormValue = yourUploadComponent.Form.Item("txtName").Value
You will need to read the objects documentation.
I have an html page that I am converting over to an asp .net page. This page contained a form that accesses an external website that I have no control over. There is some sample code below:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
</form>
</asp:Content>
The form is more complicated than the example I have provided, but it gives a rough idea of what i need to convert over. Here are the problems I have encountered.
If I leave it as is:
When you click on the submit button you have a postback to the current page and not to the external page
If simply convert everything over to be asp form controls and change the postback url:
The id's become some convoluted "ctl00_body_ctl00" which the external page is not able to interpret.
Note: I do need the page to be an aspx page because I am using a master page for other content on the page.
Additional note: this is not Microsoft MVC.
What am I missing?
The issue was with nested forms as others have mentioned.
I was able to fix all my issues by simply doing the following:
Remove the extra form element i was adding.
Leave all controls as simply html controls, except for the submit button.
Replace the submit button with an asp .net button, and set the postback url.
The old code is as follows:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
</form>
</asp:Content>
The new code:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
<asp:button postbackurl="http://anexternalwebsitehere.com/subscribe.asp" text="Subscribe" runat="server" />
</asp:Content>
This fixes any of the issues with invalid nested forms as there are none. It also addresses the issue of asp .net renaming the asp elements because the only control that is being renamed is the asp button control which was not necessary for the submission to succeed.
Since you probably have the server form tag on your masterpage spanning your contentplaceholder, this new form you're declaring will be placed inside the server-form (by server-form i mean the one asp.net use for postbacks with runat="server")
I've had cases when i needed a special non-server form on an aspx page that already had a server-form, and the way i solved the problem was to place this non-server form outside the server-form - what i mean is, place it after the server-form. Since you use masterpages, you will need a new contentplaceholder on that masterpage, you can call it "noform". It is placed after the server-form so any content put in this noform will be placed outside the server-form. This mean no asp.net controls will work in this specific contentplaceholder (noform) since they won't be picked up by the framework, but you will be able to place your non-server form there and do your magic on that.
The problem, as you've probably guessed, is that you've got one form inside another form - ie the legacy form is appearing inside the ASP.NET form required by the master page.
One quick (if rather clunky) way to get around this is to close the ASP.NET form above the legacy form, and then open a new form below the legacy form. This means you've got three forms on the page, none of which are nested.
So you end up with something like this:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
</form>
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
</form>
<form method="post" action="myAspNetPage.aspx">
</asp:Content>
The closing </form> tag at the start closes the ASP.NET from the master page. You then have your form, which should now work as expected. Then the open <form> tag at the end simply ensures that the closing </form> tag from the master page is valid HTML.
Obviously anything appearing on the master page after the legacy form won't be within the standard ASP.NET form, so this may not work for you depending on how the rest of your page is structured.
It's not a particularly elegant solution, but it works as a quick fix (depending on what else is on your master page). We've used it where we had one legacy form required on a site with hundreds of pages, so we simply wanted a one-off fix rather than anything that affected the master page itself.
In our case, we couldn't change the legacy form as this was supplied by a third-party, regularly changed, and needed to be dropped into the ASP.NET page without a developer getting involved to amend it (eg as opposed to Brian's solution to his own question which requires editing the form and is clearly a better option in his case - and probably in most other cases where there is a similar problem).
Your button's click event will handle submission of the url and data.
//C# source
protected void button_Click(object sender, EventArgs e)
}
string customURL = "http://anexternalwebsitehere.com/";
string emailValue = textBoxEmail.Text; //of course validate this for proper email...
customURL += "page.aspx?email=" + emailValue;
Response.Redirect(customURL);
}