PDA

View Full Version : Form validation / submit to url



firstnam
05-15-2012, 05:18 PM
I am trying to set up a form that contains 3 text fields as well as select between two radio spots. I'd like all fields to be required to be filled in as well as (if possible) verify the email is valid. Then after clicking submit I'd like this to go to a url, but only be able to access the url from this form page with all fields complete.

Still new to Javascript sorry! Thanks in advance


<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First Name must be filled out");
return false;
}
}
{
var x=document.forms["myForm"]["lname"].value;
if (x==null || x=="")
{
alert("Last Name must be filled out");
return false;
}
}
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false; }
}

function GetSelectedItem() {

chosen = ""
len = document.f1.r1.length

for (i = 0; i <len; i++) {
if (document.f1.r1[i].checked) {
chosen = document.f1.r1[i].value
}
}

if (chosen == "") {
alert("No Type Chosen")
}
else {
alert(chosen)
}
}
</script>


<table style="width: 300px;" align="center" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0"><form name="myForm" action="https://thecesolution.cecity.com/" onsubmit="returnvalidateForm()" method="get">
<tbody><tr>
<td width="135px"><span style="font-family: Arial,Helvetica,sans-serif; font-size: 12px; padding-left: 15px;">
<strong>First
Name:</strong></span></td>
<td style="width: 165px;"><input name="fname" type="text"></td>
</tr>
<tr>
<td width="135px">&nbsp;</td>
<td style="width: 165px;">&nbsp;</td>
</tr>
<tr>
<td><span style="font-family: Arial,Helvetica,sans-serif; font-size: 12px; padding-left: 15px;">
<strong>Last
Name:</strong></span></td>
<td style="width: 165px;"><input name="lname" type="text"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td style="width: 165px;">&nbsp;</td>
</tr>
<tr>
<td><span style="font-family: Arial,Helvetica,sans-serif; font-size: 12px; padding-left: 15px;">
<strong>Email Address:</strong></span></td>
<td style="width: 165px;"><input name="email" type="text"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" style="padding-left:15px; padding-bottom:7px;"><span style="font-family: Arial,Helvetica,sans-serif; font-size: 12px;">
<strong>Which best describes your workplace?</strong></span></td>
</tr>
<tr>
<td style="text-align: center">
<input type="radio" name="r1" value="location" /><span style="font-family: Arial,Helvetica,sans-serif; font-size: 12px;"><strong> </strong>Independent
<br />
</span></td>
<td style="text-align: center">
<input type="radio" name="r1" value="Franchise" /><span style="font-family: Arial,Helvetica,sans-serif; font-size: 12px;"><strong> </strong>Franchise
<br />
</span></td>
</tr>
<tr>
<td colspan="2" style="padding-top: 15px;" align="center"><input value="Submit" type="submit" onclick="parent.location='https://thecesolution.cecity.com/"></td>
</tr>
</tbody></form></table>

ajfmrf
05-15-2012, 05:27 PM
I don't understand the question.

firstnam
05-15-2012, 05:32 PM
I have the form but I want to have all fields required to fill in. I'd also like the email address to be verified that it is valid. and then after those two criteria are met, have the patron taken to a url.

Basically when they hit the submit button after filling out the form. The fields of the form are verified and then they are taken to a separate url.

I can figure out how to do each of these individually, but cannot figure out how to get all three coded so they work simultaneously.

Thanks
Keith

ApacheTech
05-15-2012, 05:42 PM
Can you place your code inside
tags please. It makes it a lot easier to debug your code.

firstnam
05-15-2012, 06:03 PM
Sorry about that... How's this? I just removed all unnecessary tags

<form name="form" onsubmit="return formCheck(this);" >

First Name:<input name="fname" type="text"><br />
Last Name:<input name="lname" type="text"><br />
Email Address:<input name="email" type="text"><br />
Which best describes your workplace?<br />
<input type="radio" name="r1" value="1" />Independent <br />
<input type="radio" name="r1" value="2" />Franchise<br />
<input value="Submit" type="submit" >

</form>

ajfmrf
05-15-2012, 06:07 PM
I tend to give this out as a great way to start with a feedback form

http://devingredients.com/2011/03/building-a-php-contact-form-with-captcha-from-scratch/

It is easily configured and the author is available for support.

It is pretty much straight forward and is commented as what everything is and how to add to it also.

You can email me or message me if you need help getting it going

ApacheTech
05-15-2012, 06:56 PM
Sorry about that... How's this? I just removed all unnecessary tags

<form name="form" onsubmit="return formCheck(this);" >

First Name:<input name="fname" type="text"><br />
Last Name:<input name="lname" type="text"><br />
Email Address:<input name="email" type="text"><br />
Which best describes your workplace?<br />
<input type="radio" name="r1" value="1" />Independent <br />
<input type="radio" name="r1" value="2" />Franchise<br />
<input value="Submit" type="submit" >

</form>


[/I]][/PHP] tags...

[PHP]<form name="form" onsubmit="return formCheck(this);" >

First Name:<input name="fname" type="text"><br />
Last Name:<input name="lname" type="text"><br />
Email Address:<input name="email" type="text"><br />
Which best describes your workplace?<br />
<input type="radio" name="r1" value="1" />Independent <br />
<input type="radio" name="r1" value="2" />Franchise<br />
<input value="Submit" type="submit" >

</form>

ajfmrf
05-15-2012, 11:28 PM
Are you saying that after the form is sent(after the submit button is clicked) ,you want the user to be taken to a url like a thank you page?

firstnam
05-16-2012, 12:42 PM
Yes hopefully.

ajfmrf
05-17-2012, 12:00 PM
The script I posted has that in it.If all fields are filled,the email is valid then it sends the information on and posts a thank you right there on the form.

It really is a good script and secure also.

Message me or email me if you need more help with this

firstnam
05-17-2012, 09:31 PM
Thank you.