...

View Full Version : Validate THEN submit form - problem



Figurine
08-05-2008, 04:39 PM
Hey,

I have a small problem, bear with me if this is ridiculously simple I just can't seem to spot the problem.

I am trying to get a form to check for validation and then send it on if it is ok and if not, to stay on the form with the page so that the user can fill out the missing parts.

Right now, it alerts the user of the missing fields but continues to send the form further.

This is my javascript:


<script language="JavaScript" type="text/JavaScript">

function checkForm(thisform)
{
if(thisform.name.value=='')
{
alert("Name field is required. Please fill it in.");
return false;
}

if(thisform.email.value=='')
{
alert("Email field is required. Please fill it in.");
return false;
}

if(thisform.number.value=='')
{
alert("Phone Number field is required. Please fill it in.");
return false;
}
if(thisform.message.value=='')
{
alert("Message field is required. Please fill it in.");
return false;
}

//if all is OK submit the form

thisform.submit();
}
</script>


This is my php send form


<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$number = $_REQUEST['number'] ;
$message = $_REQUEST['message'] ;


mail("my@emailaddress.com", "trial",
"Sender: $name , Netfang: $email, S&#237;mi: $simi, Texti: $message");
header( "Location: http://www.mydomain.com" );
?>


and finally this is my form


<form name="contact_form" action="sendemail.php" method="post">
<table border="0" cellspacing="5">
<tr>
<td >Nafn</td>
<td><input type="text" size="30" name="name"></td>
</tr>

<tr>
<td>Netfang</td>
<td><input type="text" size="30" name="email"></td>
</tr>

<td >S&iacute;mi</td>
<td><input type="text" size="30" name="number"></td>
</tr>
<tr>
<td valign="top"></td>
<td><textarea name="message" rows="6" cols="30"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submit" type="submit" value="Senda" onclick="checkForm(this.form)" />
</td>
</td>
</tr>
</table>
</form>


Can anyone figure out what I am doing wrong?

Best,
Figurine

abduraooft
08-05-2008, 04:49 PM
Try
<form name="contact_form" action="sendemail.php" method="post" onsubmit="return checkForm(this);">
<table border="0" cellspacing="5">
<tr>
<td >Nafn</td>
<td><input type="text" size="30" name="name"></td>
</tr>

<tr>
<td>Netfang</td>
<td><input type="text" size="30" name="email"></td>
</tr>

<td >S&iacute;mi</td>
<td><input type="text" size="30" name="number"></td>
</tr>
<tr>
<td valign="top"></td>
<td><textarea name="message" rows="6" cols="30"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Senda" />
</td>
</td>
</tr>
</table>
</form>
PS: Don't use tables to format a form, it's not semantic, see http://www.alistapart.com/articles/prettyaccessibleforms

Figurine
08-05-2008, 05:02 PM
Thanks! It works :)

REALLY appreciate all the help! And thanks for the link. I'll drop the table.

Best,
Figurine



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum