Variable Number of Input Fields in Classic-ASP form - asp-classic

I have a check out form where number of products can be "n". So how i can know how many input fields are in the form and take input from it?
Thanks

If it's a group of single controls - say a variable number of checkboxes representing items - the solution is pretty straightforward. For your checkboxes:
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="checkbox" name="ProductID" value="3" />Product #3
Then in your ASP, you could do this:
<%
Dim intID
For Each intID In Request.Form("ProductID")
' intID now represents a selected product ID. Insert into DB
' or whatever your process is. Note that only the "checked" values
' will be passed to the server.
Next
%>
In fact, this approach will work for any number of controls with the same name. If it were 1 - n number of textboxes with the name "FavoriteColor", you could For Each through each value in the same manner. Textboxes with no user input would not be passed.
Now, if your checkout form contains a group of input controls per item, you can build on that approach by carefully naming your other controls:
<div>
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="textbox" name="Product1_Quantity">
<input type="textbox" name="Product1_Color">
</div>
<div>
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="textbox" name="Product2_Quantity">
<input type="textbox" name="Product2_Color">
</div>
<div>
<input type="checkbox" name="ProductID" value="3" />Product #3
<input type="textbox" name="Product3_Quantity">
<input type="textbox" name="Product3_Color">
</div>
Now, again, on the server you could parse the data in this way:
<%
Dim intID
Dim intQuantity
Dim strColor
For Each intID In Request.Form("ProductID")
' this is a selected item
intQuantity = Request.Form("Product" & intID & "_Quantity")
strColor = Request.Form("Product" & intID & "_Color")
Next
%>
You'd be able to perform validation and other logic on each group of selected items in this manner.

Related

How to get which radio button is selected in symfony 3?

I have these inputs in my twig file :
<input type="text" name="txtNom" id="txtNom" value="{{user.nom}}" />
<input type="text" name="txtPrenom" id="txtPrenom" value="{{user.prenom}}" />
<input type="radio" name="rbSexe" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe" id="rbFemme" onclick="changeGender(this.id);" />
So, for calling those inputs in my Controller, I use the name attribute, for the first two it's okay :
$utilisateur->setNom($request->get('txtNom'));
$utilisateur->setPrenom($request->get('txtPrenom'));
but those with radio type have the same name, so how can I call specific one of them?
$utilisateur->setSexe(?????????);
I solved the problem :
I give the inputs a value, and make the name looks like an array:
<input type="radio" name="rbSexe[]" value="Homme" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe[]" value="Femme" id="rbFemme" onclick="changeGender(this.id);" />
and for call it in Controller, I use this :
$s = $request->get('rbSexe',0)[0];

ASP Getting radio checked value

I'm sure someone will help with this:
Basically I got a form which on submit, it directs it to my contact_form.asp which then sends out an email to a specified address, well I got some Radio buttons on that form, and need to get the checked radio button out of a group of about 3 radio buttons.
i.e.
<input type="radio" name="group1" id="serviceDaily" value="Daily"> Daily </input>
<input type="radio" name="group1" id="serviceWeekly" value="Weekly"> Weekly</input>
<input type="radio" name="group1" id="serviceMonthly" value="Monthly"> Monthly</input>
and in my contact_form.asp I am requesting the value like:
group1 = Request("group1")
Am I doing something wrong? i.e. getting the value incorrectly? The response I'm getting is "Group1", not "Daily" as I want to.
I created a test page using the code you supplied and it works fine (see below).
Try a http debugging tool such as fiddler2 to investigate the actual parameters and values passed between the two pages.
<html>
<head></head>
<body>
<form method="post">
<input type="radio" name="group1" id="serviceDaily" value="Daily"> Daily </input>
<input type="radio" name="group1" id="serviceWeekly" value="Weekly"> Weekly</input>
<input type="radio" name="group1" id="serviceMonthly" value="Monthly"> Monthly</input>
<input type="submit" value="submit" />
</form>
</div>
<%
Dim group1
group1 = Request("group1")
Response.Write ("group1='" + group1 + "'")
%>
</body>
</html>

Classic ASP checkbox question

I understand that if the all the inputs being entered as a, b, and c and all the checkbox are checked then the output would look like this.
response.write( request.form("a1") ) = a, b, c
response.write( request.form("chk") ) = 1, 1, 1
Is there a way to determined if the corresponding input text checkbox is checked if not all checkbox are checked?
ie: the input is being entered as a, b, and c then only the corresponding checkbox at "c" is checked.
The output of this will be:
response.write( request.form("a1") ) = a, b, c
response.write( request.form("chk") ) = 1
<form name="myForm">
<input type="text" name="a1" />
<input type="checkbox" name="chk" value="1" />
<input type="text" name="a1" />
<input type="checkbox" name="chk" value="1" />
<input type="text" name="a1" />
<input type="checkbox" name="chk" value="1" />
<input type"submit" value="submit" />
</form>
You're going to need to change the names of your inputs. The only type of input that is intended to have multiple instances sharing a name is a radio button. That is done so you can get the mutually-exclusive select behavior that radio buttons are designed for.
In this case, you're going to want to give each text and checkbox input a different name. So your HTML would look like:
<form name="myForm">
<input type="text" name="a1" />
<input type="checkbox" name="chk1" value="1" />
<input type="text" name="a2" />
<input type="checkbox" name="chk2" value="1" />
<input type="text" name="a3" />
<input type="checkbox" name="chk3" value="1" />
<input type"submit" value="submit" />
</form>
Then simply reference your third checkbox with:
response.write( request.form("chk3") )
It would require you to write a little code if you wanted the results to appear in a nice comma delimited list like you've shown, but I would argue that's appropriate.
If reason you want a comma-delimited string similar to "a, b, c" you could change the values of all your checkboxes to "1", "2", "3" instead of all being "1".

Get the selected input type radio button ASP.NET

I have two radio buttons:
<input type="radio" name="group1" />1
<input type="radio" name="group1" />2
How do I know which one is selected when the form is posted?
The inputs should have values:
<input type="radio" name="group1" value="1" />1
<input type="radio" name="group1" value="2" />2
Then, the value will be posted on the name group1. On Asp.net you can get it using:
Request.Form["group1"]
If you are using runat="server", it can happen that name attribute is changed at client, so you will get null as value.

Building a query string based radiobutton values

I'd like to build a query string based on values taken from 5 groups of radio buttons.
Selecting any of the groups is optional so you could pick set A or B or both. How would I build the querystring based on this? I'm using VB.NET 1.1
The asp:Radiobuttonlist control does not like null values so I'm resorting to normal html radio buttons. My question is how do I string up the selected values into a querystring
I have something like this right now:
HTML:
<input type="radio" name="apBoat" id="Apb1" value="1" /> detail1
<input type="radio" name="apBoat" id="Apb2" value="2" /> detail2
<input type="radio" name="cBoat" id="Cb1" value="1" /> detail1
<input type="radio" name="cBoat" id="Cb2" value="2" /> detail2
VB.NET
Public Sub btnSubmit_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim queryString As String = "nextpage.aspx?"
Dim aBoat, bBoat, cBoat bas String
aBoat = "apb=" & Request("aBoat")
bBoat = "bBoat=" & Request("bBoat")
cBoat = "cBoat=" & Request("cBoat ")
queryString += aBoat & bBoat & cBoat
Response.Redirect(queryString)
End Sub
Is this the best way to build the query string or should I take a different approach altogether? Appreciate all the help I can get. Thanks much.
The easiest way would be to use a non-server-side <form> tag with the method="get" then when the form was submitted you would automatically get the querystring you are after (and don't forget to add <label> tags and associate them with your radio buttons):
<form action="..." method="get">
<input type="radio" name="apBoat" id="Apb1" value="1" /> <label for="Apb1">detail1</label>
<input type="radio" name="apBoat" id="Apb2" value="2" /> <label for="Apb2">detail2</label>
<input type="radio" name="cBoat" id="Cb1" value="1" /> <label for="Cb1">detail1</label>
<input type="radio" name="cBoat" id="Cb2" value="2" /> <label for="Cb2">detail2</label>
</form>
You could use StringBuilder instead of creating those three different strings. You can help it out by preallocating about how much memory you need to store your string. You could also use String.Format instead.
If this is all your submit button is doing why make it a .Net page at all and instead just have a GET form go to nextpage.aspx for processing?

Resources