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

Recommended Answers

All 2 Replies

Member Avatar for Zagga

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

Hi Zagga,
yes the validation is not passed but I don't get any message at all. Simply if I don't input anything and press send I get the thank you page.
I have made the amendments you suggested but still no joy. Here's the offending page:http://www.antobbo.webspace.virginmedia.com/webediting/testing/bold_and_other.htm
If you disable javascript you'll see that nothing has unfortunately changed.
No I don't get any error message
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.