0

Hi there, I wonder if you could help me with some form validation please. Here's the problem:
at the bottom of this page http://www.antobbo.webspace.virginmedia.com/webediting/testing/bold_and_other.htm - still testing at the moment - there is a contact form. When javascript is enabled you can call the form with a button and then upon submission another javascript validates it. If a user has javascript off, I have the same form that comes up on the page but obviously it doesn't validate. Now, I had a look around and many people say that the only way to validate the form in those circumstances will be PHP.
Personally I think I shouldn't even provide an alternative to javascript because nowadays when probably the majority of users have broadbands, I cannot see too many reasons to switch javascript off in your browser. However, if the community says that it is still good practice then, so be it.
So, to get back to my form, here's the PHP file the form (both the one that comes up when you browse the page with javascript on and off) gets sent to:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>

<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" >

<title>Thank you</title>
</head>
<body>
<div id="wrapper">



<!-- BANNER STARTS HERE -->
<div id="banner">
<h1>Web editing - Livelink WCM</h1>
</div>
<div id="text_navigation">
<?php
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];

mail($to,$subject,$message);
echo "<h2>Thank you</h2><p><br><br>Thank you for sending your feedback. That will help me improve the website.<br><br></p>";
?>
</div>
</div>
</body>
</html>

The script works fine, I had some help from this forum when I wrote it, (in a separate post I will post some of the problems I am having with this script) but if I want to validate the second form (the one that comes up when javascript is off) I need a different script the form will be sent to. I wrote this script largely based on the above but it doesn't work at all, and I am not really sure why, any suggestion?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>

<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" >

<title>Thank you</title>
</head>
<body>
<div id="wrapper">



<!-- BANNER STARTS HERE -->
<div id="banner">
<h1>Web editing - Livelink WCM</h1>
</div>
<div id="text_navigation">
<?php
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];

function check_field1($field_name_1)
{
  if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\
  ]+$/s",$field_name_1))
  return TRUE;
  else
  return FALSE;
}

if(!check_field1($message))
{
  echo "Illegal input $message in 'your_name'";
}

mail($to,$subject,$message);
echo "<h2>Thank you</h2><p><br><br>Thank you for sending your feedback. That will help me improve the website.<br><br></p>";
?>
</div>
</div>
</body>
</html>

Here is the form in question if it helps:

<!-- THIS SHOULD DISPLAY ONLY IF USERS HAVE JAVASCRIPT OFF-->
<div id="hidden_form">
<form action="validation.php" method="post" onsubmit="return validationOne(document.getElementById('commentsid_no_java'),'Enter a value')">



<div class="comment_box">
<p>Comments and suggestions:<br><textarea name="comments" id="commentsid_no_java" rows="3" cols="30"></textarea><br><br>

<input type="submit" value="Send"></p>
</div> <!-- end of comment_box-->

</form>
</div>


<!-- END OF JAVASCRIPT OFF-->

thanks

2
Contributors
2
Replies
7
Views
6 Years
Discussion Span
Last Post by Violet_82
0

Hi Violet_82,

after a quick look through your code I can only see 1 obvious problem. If the comments do not pass the validation, the 'illegal input' message is shown but the email is still sent.

To prevent this you can place the mail function inside an ELSE clause in your IF statement.

if(!check_field1($message))
{
  echo "Illegal input $message in 'your_name'";
  echo "<br />Please press the BACK button and try again.";
}
else
{
  mail($to,$subject,$message);
  echo "<h2>Thank you</h2><p><br><br>Thank you for sending your feedback. That will help me improve the website.<br><br></p>";
}

Are you getting any specific error messages?

Hope this helps.
Zagga

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.