Solved

form html

Posted on 2009-05-09
13
230 Views
Last Modified: 2012-05-06
i have the form.
if the user didn't select, i want to give popup menu says "Please select size."
do you know how to do it?
<!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>
<script type="text/javascript">
function selectProduct()
{
var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
var form1 = document.getElementById('form1');
 
if (val1 == "XS/S/M")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654321;
 
}
 
}
else if(val1 == "M/L/XL/XXL")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654322;
}
}
}
</script>
</head>
<body onload="javascript:selectProduct();">
 
<form id="form1" method="POST" action="http://secure.ultracart.com/cgi-bin/UCEditor">
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="javascript:selectProduct();">
<OPTION VALUE="error" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="javascript:selectProduct();">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" name="submit" value="add to cart" />
</form>
</body>
</html>

Open in new window

0
Comment
Question by:Hiroyuki Tamura
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 24347119
Just check the value in a function set as the <form> onsubmit attribute
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24347123
thank you.
where should i put that?
could you modify my snippet?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 40 total points
ID: 24347146
You can do:
<form id="form1" method="POST" onsubmit="return isValidForm();" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
 
...
 
function isValidForm() {
	return document.forms[0].mylist1.value != "error";
}
	

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 85 total points
ID: 24347753
Or nicer:

<form id="form1" method="POST" onsubmit="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
 
...
 
function isValidForm(theForm) {
  if(theForm.mylist1.selectedIndex<1) {
    alert('Please select size');
    theForm.mylist1.focus();
    return false;
  }
  return true; // allow submit
}

Open in new window

0
 

Author Comment

by:Hiroyuki Tamura
ID: 24347981
thank you.
it doesn't work...
0
 

Author Comment

by:Hiroyuki Tamura
ID: 24347982
is this ok?
<!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>
<script type="text/javascript">
function selectProduct()
{
var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
var form1 = document.getElementById('form1');
 
if (val1 == "XS/S/M")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654321;
 
}
}
else if(val1 == "M/L/XL/XXL")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654322;
}
}
function isValidForm(theForm) {
  if(theForm.mylist1.selectedIndex<1) {
    alert('Please select size');
    theForm.mylist1.focus();
    return false;
  }
  return true; // allow submit
}
}
</script>
</head>
<body onload="javascript:selectProduct();">
 
<form id="form1" method="POST" onsubmit="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="javascript:selectProduct();">
<OPTION VALUE="" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="javascript:selectProduct();">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" name="submit" value="add to cart" />
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Hiroyuki Tamura
ID: 24348205
do i need to add "onload" javascript?

<!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>
<script type="text/javascript">
function selectProduct()
{
var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
var form1 = document.getElementById('form1');
 
if (val1 == "XS/S/M")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654321;
 
}
}
else if(val1 == "M/L/XL/XXL")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654322;
}
}
function isValidForm(theForm) {
  if(theForm.mylist1.selectedIndex<1) {
    alert('Please select size');
    theForm.mylist1.focus();
    return false;
  }
  return true; // allow submit
}
}
</script>
</head>
<body onload="javascript:selectProduct();isValidForm();">
 
<form id="form1" method="POST" onsubmit="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="javascript:selectProduct();">
<OPTION VALUE="" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="javascript:selectProduct();">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" name="submit" value="add to cart" />
</form>
</body>
</html>

Open in new window

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 85 total points
ID: 24348986
You have too many brackets

Load it into Firefox and look at the error console

Here is a better version but I did not have time to test - note how I pass the form to the function and removed the name="submit" from the submit button

<!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>
<script type="text/javascript">
function selectProduct(theForm) {
  var val1 = theForm.mylist1.options[theForm.mylist1.selectedIndex].value;
  var val2 = theForm.mylist2.options[theForm.mylist2.selectedIndex].value;
  if (val1 == "XS/S/M") {
    if (val2 == "NATURAL/BLUSH") {
      theForm.add.value = 987654321;
    }
  }
  else if (val1 == "M/L/XL/XXL") {
    if(val2 == "NATURAL/BLUSH") {
    theForm.add.value =  987654322;
  }
}
function isValidForm(theForm) {
  if(theForm.mylist1.selectedIndex<1) {
    alert('Please select size');
    theForm.mylist1.focus();
    return false;
  }
  return true; // allow submit
}
</script>
</head>
<body onload="selectProduct(document.form1);">
 
<form id="form1" method="POST" onsubmit="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
 
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="selectProduct(this.form);">
<OPTION VALUE="" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="selectProduct(this.form);">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" value="add to cart" />
</form>
</body>
</html>

Open in new window

0
 
LVL 1

Accepted Solution

by:
mohgupta earned 375 total points
ID: 24382246
You can use onclick event instead of onsubmit as follows :

<!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>
<script type="text/javascript">
function selectProduct()
{
var val1 = document.getElementById('mylist1').options[document.getElementById('mylist1').selectedIndex].value;
var val2 = document.getElementById('mylist2').options[document.getElementById('mylist2').selectedIndex].value;
var form1 = document.getElementById('form1');
 
if (val1 == "XS/S/M")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654321;
 
}
 
}
else if(val1 == "M/L/XL/XXL")
{
if(val2 == "NATURAL/BLUSH")
{
document.getElementById('add').value = 987654322;
}
}
}

function isValidForm(theForm) {
  if(theForm.mylist1.selectedIndex<1) {
    alert('Please select size');
    theForm.mylist1.focus();
    return false;
  }
  return true; // allow submit
}

</script>
</head>
<body onload="javascript:selectProduct();">
 
<form id="form1" method="POST" onclick="return isValidForm(this);" action="http://secure.ultracart.com/cgi-bin/UCEditor">
<input type="hidden" name="merchantId" value="SWISH" />
<input type="hidden" name="add" id="add" value="" />
<br />
<SELECT id="mylist1" NAME="mylist1" onchange="javascript:selectProduct();">
<OPTION VALUE="error" >Select size:</OPTION>
<OPTION VALUE="XS/S/M" >XS/S/M</OPTION>
<OPTION VALUE="M/L/XL/XXL">M/L/XL/XXL</OPTION>
</SELECT>
<br />
<SELECT id="mylist2" NAME="mylist2" onchange="javascript:selectProduct();">
<OPTION VALUE="NATURAL/BLUSH" >NATURAL/BLUSH</OPTION>
</SELECT>
<br />
<input type="text" name="quantity" value="1" />
<br />
<input type="submit" name="submit" value="add to cart" />
</form>
</body>
</html>
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 85 total points
ID: 24382268
Not a great idea to use onClick - why would you want to do that when you can cancel the submission using onSubmit???
Also NEVER call anything in a form for NAME="submit"
0
 

Author Closing Comment

by:Hiroyuki Tamura
ID: 31579871
thank you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24389448
:-)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24389788
Hmm...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Logon script fails 23 45
SequenceInputStream example 3 19
html input 8 43
AWK How to eradicate Windows carriage returns from 52 files now on unix/mac? 4 20
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.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

832 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