PDA

View Full Version : Form Validation, window.location.href and onsubmit



ump
08-27-2008, 11:48 PM
Hi all,
I'm fairly new to javascript and ive hit a road block.
What im trying to accomplish.
Submit a form to my php page and with the onsubmit validate my form fields and bounce the user to a 'thankyou.html'

Here's my Javascript


<script type="text/javascript">
function validateForm() {
return Spry.Widget.Form.validate(document.myform);
}
function redirect() {
window.location.href = 'http://mydomain.com/thanks.html';
}
</script>



And Here's my form:


<form action="mypage.php" method="post" name="myform" onSubmit="return validateForm();redirect()">


For some reason when i submit it takes me to mypage.php (which posts straight to a db) and not the thanks.html (where i would like it to go).

Thanks in advance!

rangana
08-28-2008, 12:52 AM
Could you please show us the involved markup (HTML).

mburt
08-28-2008, 02:34 AM
For some reason when i submit it takes me to mypage.php (which posts straight to a db) and not the thanks.html (where i would like it to go).

That's because your action is "mypage.php".

Use a PHP redirect from "mypage.php"

ump
08-28-2008, 05:01 PM
That's the problem. I cant change or modify mypage.php. It resides somewhere else. i only used "mypage.php" as an example. I need to submit data to "mypage.php" and then redirect to a thank you page.

Thanks for the help!

ump
09-01-2008, 05:50 AM
Sorry for the delay here's my code. I decided to use dreamweaver's validation and i still dont know where or how to work the redirect. Thanks for the help!!


<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
</head>

<body>
<form action="../vatt-appling.php" method="post" name="1430" id="1430" onSubmit="MM_validateForm('firstname','','R','lastname','','R','address','','R','city','','R','dayphone','','RisNum','email','','RisEmail');return document.MM_returnValue">
<table width="50%" border="0" cellpadding="2">
<tr>
<td colspan="2">logo </td>
</tr>
<tr align="left">
<td colspan="2"><p class="style3"><span class="txtRequired">*</span> Indicates required
field</td>
</tr>
<tr align="left">
<td><span class="txtLabel">First Name: </span><span class="txtRequired">*</span></td>
<td><input type="text"name="firstname" size="25">
</td>
</tr>
<tr align="left">
<td><span class="txtLabel">Last Name: </span><span class="txtRequired">*</span></td>
<td> <span class="txtRequired">
<input type="text"name="lastname" size="25">
</span></td>
</tr>
<tr align="left">
<td><span class="txtLabel">Street Address: </span><span class="txtRequired">*</span></td>
<td><input type="text"name="address" size="35">
</td>
</tr>
<tr align="left">
<td><span class="txtLabel">City: </span><span class="txtRequired">*</span></td>
<td><input type="text"name="city" size="25" maxlength="30">
</td>
</tr>
<tr align="left">
<td><span class="txtLabel">State: </span><span class="txtRequired">*</span></td>
<td><input type="text" name="state">
</td>
</tr>
<tr align="left">
<td><span class="txtLabel">Zip Code: </span><span class="txtRequired">*</span></td>
<td><input type="text" name="Zip">
</td>
</tr>
<tr align="left"><td><span class="txtLabel">Country: </span></td>
<td><input type="text"name="country" size="25">
<tr align="left">
<td><span class="txtLabel">Daytime Phone: </span><span class="txtRequired">*</span></td>
<td><input type="text"name="dayphone" size="15">
no dashes or spaces</td>
</tr><tr align="left">
<td><span class="txtLabel">Email Address: </span><span class="txtRequired">*</span></td>
<td><input type="text"name="email" size="25">
</td>
</tr>
<tr align="left">
<td colspan="2">&nbsp;</td>
</tr>
<tr align="left">
<td colspan="2"><input name="Submit" type="submit" value="Submit" />
</td>
</tr>
</table>
</form>

rangana
09-02-2008, 05:39 AM
Why make it so hard. You can make it work by choosing either of these three on mypage.php:

Use the meta tag:


<meta http-equiv="refresh" content="1;url=http://thisismyTHANKYOUpage.com">

Use JS:


<script type="text/javascript">
window.onload=function(){location.href='http://thisismyTHANKYOUpage.com';}
</script>

Use server-side (PHP) :


<?php
header("Location: http://www.thisismyTHANKYOUpage.com/"); /* Redirect browser */
?>



Hope that helps.

ump
09-02-2008, 04:57 PM
That's where my problem lies. I cannot access "mypage.php". It's on an external site that i cannot edit. I simply used "mypage.php" as a placeholder.