Javascript and forms

Status
Not open for further replies.

stephenb

Ars Legatus Legionis
11,837
So I've got a fairly simple form that has a drop down selection and a checkbox.

I'm using this to verify that a school has be selected and that the checkbox has been ticked.

Code:
<script type="text/javascript"> 
function validate() { 
// Checking if at least one period button is selected. Or not. 
if (document.school.selection.value.length <= 2){ 

alert("Please select your school."); 
return false;}

if (!document.school.agree.checked) { alert("Please read and agree to our policies."); 
 return false; } 

return true;

}

function WinOpen() {
  var url=document.school.selection.value
  document.location.href=url

}

</script>

What I'd like to do next is if the function returns true, I'd like to launch a shopping cart using the URL returned by the selection.

The verification works, but I can't figure out how to launch the Window with the dynamic URL.

This is the form:

Code:
<form id="schoolform" name="school" method="post" action="WinOpen();" onsubmit="return validate()"><input type="hidden" name="todo" value="post">

followed by the selection, etc.

I'm guessing on what to use for action.

Thanks.
 

stephenb

Ars Legatus Legionis
11,837
Okay, so I'm trying this:

Code:
function validate() { 
// Checking if at least one period button is selected. Or not. 
if (document.school.selection.value.length <= 2){ 

alert("Please select your school."); 
return false;}

if (!document.school.agree.checked) {
	alert("Please read and agree to our policies.");
	return false;} 

else {
	var url=document.school.selection.value;
	alert (url);
	window.location=url;
	}

}

</script>

And the correct URL gets passed to the variable, but then it adds my original domain to the beginning of the script. I'm wondering if this is a drupal or htaccess issue.
 

stephenb

Ars Legatus Legionis
11,837
Code:
http://shoppingcart.com/Secure/Secure.aspx?mid=5DDF1298-3CF3-4D46-8559-CD10E7FA20F3&pid=30b406c434aa4274bf82d0e9bd7860c9&bn=1

Each one has a slightly different pid

I got it to somewhat work by using:
Code:
function validate() { 
// Checking if School is selected. 
if (document.school.selection.value.length <= 2) { 
	alert("Please select your school."); 
	return false;}

if (!document.school.agree.checked) {
	alert("Please read and agree to our terms and conditions.");
	return false;} 

else {
	var url=document.school.selection.value;
	window.open(url, '_blank');
	return false;}

}

This opens the shopping cart in a new window and keeps the original window intact, instead of a 404.

I took the action out of the form and I'm using onClick at the button instead of onSubmit. Not that that should make a difference.

I also removed that hidden input as I wasn't sure why that was there. (picked up from a sample)

It's not a drupal thing as it does the same on a bunch of other servers that don't have drupal installed.

thanks.
 

Dracorat

Ars Tribunus Militum
2,798
Copy the following in to a new HTML document and see how easy it is to do all this with JQuery:

Code:
<!DOCTYPE html>
<html>
<head>
    <title>Validation Example</title>
    <style type="text/css">
    	.FormItem { clear: left; }
		.RowTitle { font-weight: bold; float: left; font-size: 12pt; width: 220px; }
		#goButton { margin-left: 220px; }
    </style>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    	$(document).ready(function () {
    		$('#goButton').click(validateForm);
    	});

    	function validateForm()
    	{
    		if($('#schoolList').val() == "no selection")
    		{
    			alert("Please choose a school!");
    			return;
    		}

    		if($('#agreement').prop('checked') == false)
    		{
    			alert("Please agree already!");
    			return;
    		}

    		var url=[
				'http://example.com/url/page.aspx?mid=',
				$('#schoolList').val(),
				'&myid=12345',
				'&someotherid=7890'
    		].join('');
    		// Once you make the proper URL, remove the following line and uncomment the next.
    		alert(url);
    		//window.open(url, 'ShoppingCart');
    	}
    </script>
</head>
<body>
    <div class="FormItem">
    	<div class="RowTitle">Select a school:</div>
    	<div class="RowField">
			<select id="schoolList">
				<option value="no selection">Please choose:</option>
				<option value="5DDF1298-3CF3-4D46-8559-CD10E7FA20F1">School 1</option>
				<option value="5DDF1298-3CF3-4D46-8559-CD10E7FA20F2">School 2</option>
				<option value="5DDF1298-3CF3-4D46-8559-CD10E7FA20F3">School 3</option>
			</select>
		</div>
    </div>

    <div class="FormItem">
    	<div class="RowTitle">Agree to something:</div>
    	<div class="RowField">
			<input type="checkbox" id="agreement"></input>
			<label for="agreement">I agree</label>
		</div>
    </div>

    <div class="FormItem">
    	<input type="button" id="goButton" value="DO IT!"></input>
    </div>

</body>
</html>
 
Status
Not open for further replies.