Solved

simple form

Posted on 2003-10-27
19
186 Views
Last Modified: 2006-11-17
Hello,

I have a question about a simple form on a page called pizza.asp.

<form name="formPizza" action="pizza.asp">
  <input type="checkbox" value="Pepperoni">Pepperoni<br>
  <input type="checkbox" value="Onion">Onion<br>
  <input type="checkbox" value="Mushroom">Mushroom<br>
  <input type="submit" name="submit" value="Submit">
</form>

You have selected the following items for your pizza:

How do I get the values for the checkboxes checked to display below the form?  Do I use post or get for the method?  The user should be able to clear the previously checked boxes too and select new ones.  Clicking on the submit button will then display their new results.

Thanks!
0
Comment
Question by:tudovich
  • 6
  • 5
  • 2
  • +5
19 Comments
 
LVL 1

Expert Comment

by:paStiSSet
ID: 9629492
you have installed PHP and MySQL on your server? or it must be only in ASP? i can help you with PHP
0
 

Author Comment

by:tudovich
ID: 9629509
it must be in ASP.
0
 
LVL 1

Expert Comment

by:rstorey2079
ID: 9629560
You can choose whether you want to use get or post.  Just specify it in your form.  Post is usually the preferred method.

You also need to name your checkboxes.  You cannot use them after submitting the form if they do not have names.

<form method="post" name="formPizza" action="pizza.asp">
 <input type="checkbox" name="topping1" value="Pepperoni">Pepperoni<br>
  <input type="checkbox" name="topping2" value="Onion">Onion<br>
  <input type="checkbox" name="topping3" value="Mushroom">Mushroom<br>
  <input type="submit" name="submit" value="Submit">
</form>

Now, when you hit the submit button, the name and value pairs for the checkboxes that are checked will be sent to pizza.asp.  If the checkbox is unchecked when the form is submitted, it is not passed to pizza.asp.

That's as far as I can help you...I don't know anything about ASP.
0
 
LVL 6

Accepted Solution

by:
mattjp88 earned 50 total points
ID: 9629568
well you have a few problems with your form.  first of all, the check boxes dont have names, which makes it impossible to get the values for.  this is a working form:

<form name="formPizza" action="pizza.asp">
  <input type="checkbox" name="Pepperoni" value="yes">Pepperoni<br>
  <input type="checkbox" name="Onion" value="yes">Onion<br>
  <input type="checkbox" name="Mushroom" value="yes">Mushroom<br>
  <input type="submit" name="submit" value="Submit">
</form>

and about post or get, you would use GET with the way you have it, if you want to use post, change the <form> tag to this:

<form name="formPizza" method="POST" action="pizza.asp">

and to be very picky, you need to actually modify the existing code to make the GET moethod to work with coreect santax.  by default, the method is GET, but to be sure, change your form tag to this:

<form name="formPizza" method="GET" action="pizza.asp">

-Matt
0
 
LVL 3

Expert Comment

by:superslamwich
ID: 9629811
Although I don't know anything about asp, you can use simple HTML to get the variables that are passed:

<form name="formPizza" action="pizza.asp">
 <input type="checkbox" name="Pepperoni" value="yes">Pepperoni<br>
 <input type="checkbox" name="Onion" value="yes">Onion<br>
 <input type="checkbox" name="Mushroom" value="yes">Mushroom<br>
 <input type="submit" name="submit" value="Submit">
</form>

<script type='text/javascript'>
<!--
  args = document.location.search
  writein = "<b>Selected Toppings:</b><p>"
  if(args != "")  {
    info = args.substr(args.indexOf('?')+1)
    vars = info.split('&')
    q=false
    for(i=0;i<vars.length;i++)  {
      if(vars[i].split('=')[0] == "Pepperoni")  {
        writein += "Pepperoni<br>"
        document.formPizza.Pepperoni.checked=true
        q=true
      }
      if(vars[i].split('=')[0] == "Onion")  {
        writein += "Onion<br>"
        document.formPizza.Onion.checked=true
        q=true
      }
      if(vars[i].split('=')[0] == "Mushroom")  {
        writein += "Mushroom<br>"
        document.formPizza.Mushroom.checked=true
        q=true
      }
    }
    if(!q)  writein += "None"
    document.write(writein)
  }
//-->
</script>

{Slam}
0
 
LVL 2

Expert Comment

by:HeadAcheMike
ID: 9630277
I know you said you want it in ASP however i have done it in PHP, it should be fairly straight forward to convert. Anyway, heres the code (note. filename extension must be .php):

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="formPizza" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
  <input type="checkbox" name="Pepperoni" value="Pepperoni">Pepperoni<br>
  <input type="checkbox" name="Onion" value="Onion">Onion<br>
  <input type="checkbox" name="Mushroom" value="Mushroom">Mushroom<br>
  <input type="submit" name="submit" value="Submit">
</form>

<?php
if(isset($_POST['submit'])) print "You Selected: <p>";
if(isset($_POST['Pepperoni'])) print $_POST['Pepperoni']."<br>";
if(isset($_POST['Onion'])) print $_POST['Onion']."<br>";
if(isset($_POST['Mushroom'])) print $_POST['Mushroom'];
?>

</body>
</html>
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9630425
Here is the ASP for u... save the part below as pizza.asp

<%
Dim Pepperoni, Onion, Mushroom
Pepperoni = Request("Pepperoni")
Onion = Request("Onion")
Mushroom = Request("Mushroom")

If Pepperoni<>"" Then Response.Write "You selected Pepperoni"
'... similarly for onion
'... and mushroom

'And the html follows... (note the method attribute in form tag and the name attribute in checkbox)
%>

<form name="formPizza" action="pizza.asp" method="post">
  <input type="checkbox" name="Pepperoni" value="Pepperoni">Pepperoni<br>
  <input type="checkbox" name="Onion" value="Onion">Onion<br>
  <input type="checkbox" name="Mushroom" value="Mushroom">Mushroom<br>
  <input type="submit" name="submit" value="Submit">
</form>
0
 
LVL 2

Expert Comment

by:HeadAcheMike
ID: 9630587
seems to be a lot better in php if you ask me, just my personal opinion take note
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9631064
tudovich... are you trying out any of the solutions?

Also, please look into your previous questions.

Questions Asked  12
Questions Open  9
Questions Graded:  2
Questions Deleted:  1
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:jaysolomon
ID: 9633799
Here is a asp solution

<%
      pizza = request.form("pizza")
%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="formPizza" action="" method="post">
 <input type="checkbox" name="pizza" value="Pepperoni">Pepperoni<br>
 <input type="checkbox" name="pizza" value="Onion">Onion<br>
  <input type="checkbox" name="pizza" value="Mushroom">Mushroom<br>
  <input type="submit" name="submit" value="Submit">
</form>
<%
If pizza<>"" then
      response.write("<hr /><p>You like: <b>" & request.form("pizza") &"</b></p>")
end If%>
</body>
</html>


0
 
LVL 6

Expert Comment

by:mattjp88
ID: 9636358
i got a question jay, since all the checkboxes are the same name, then wont only 1 value get passed?  so if i check Pepperioni and Onion, from my past experiences, only Onion wil get passed.

-Matt
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9636385
No matt this is the output

You like: Pepperoni, Onion, Mushroom
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9636399
Atleast thats what i got on my IE6 and NS7
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9636506
What jay says is right, if more than one element has the same name then a CSV of all values in that name will come with the request, for that element name. Am not really sure if ALL browsers will handle it this way.

The disadvantage is that we need to do further processing (like a split) to take individual values out of it.

0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9636711
fozy i will have to let you do the split ;)

i am a javscript'r learning asp
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9636794
The author seems least interested in any of the post jay...
but here you are if you are interested

myArray = Split( Request("pizza"), "," )

Now myArray(0) will have "Pepperoni" and so on...

For iCnt = 0 To UBound(myArray)
  Response.Write myArray(iCnt) & "<br>"
Next
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9639253
Thanks fozy

what about this


<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="formPizza" action="" method="post">
...

....

</form>
<%
         for each pizza in request.form("pizza")
%>
     <p>You Choose: <b> <%= pizza%></b></p>
<%
        next
%>
..
..

.

.

I have not tested this, just curious since i am at home i do not have time to tested it.
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9642855
Don't think that will work since request.form("pizza") itself is an item and not an object.

what you are trying to do is enumerate the items in an object, but you are trying that on an item.

guess the one below will work though.

for each pizza in request.form
   Response.Write "You selected " & request.form(pizza) & "<br>"
next

but for this to work the checkboxes should have different names and submit button should not have a name
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9643551
ok

i just tested my other solution

and this is the out put i got when i checked all 3 boxes
You chose: Pepperoni

You chose: Onion

You chose: Mushroom


and the output for one box
You chose: Pepperoni

Fozy thanks for your help, i am trying to learn asp as fast as i learned JS

I don't guess the questioneer really wanted an asp solution afterall
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now