PHP Validation - Allow 'enter' + Echo'ing posted values

Hey

I have a couple of small problems with my script for a simple html contact form that has validation on send and also echo’s the value in a particular field if it has already been posted.

My HTML form is as below:


<form id="contact" method="post" action="result.php">
<input id="firstname" class="box" name="firstname" type="text"/><br />
<textarea id="message" class="commentbox" name="message" rows="4" cols="22"></textarea><br />
<input id="send" name="send" class="submitbutton" type="submit" value="Send"></input>
</form>

Now the problems:

I validate each field on submit to test if it is valid. All is fine expect in my ‘message’ textarea field i need to allow the user to press ENTER or RETURN. So far i have Upper/Lowercase Numbers, Letters, ?,!, full stop and a comma allowed, how can i add enter into the current validation?:


function validateMessage($message){
     if(strlen($message) < 1)
           return false;
     if(ereg('[^A-Za-z0-9,?!. ]+', $message))
           return false;
     else
           return true;
	}

On my ‘result’ page if an error from validation comes up i display the form again but i echo the posted values into each field so the user doesn’t have to ren-enter the whole form again. All is fine on the standard input fields but i cannot get the ‘message’ text area field to echo back its posted value.

My code currently is:


<form id="contact" method="post" action="result.php">
<input id="firstname" class="box" name="firstname" type="text" 
      <?PHP
      if ( isset($_POST['send']) && (!validateFirstName($_POST['firstname']))){ 
	     echo('value="Please Enter a Valid First Name"'); 
      }
      else if ($_POST['firstname']) { 
	     echo('value="'.$_POST['firstname'].'"'); 
      } 
      ?>
       /><br />
<textarea id="message" class="commentbox" name="message" rows="4" cols="22"
      <?PHP 
      if ( isset($_POST['send']) && (!validateMessage($_POST['message']))) { 
			echo('Please Enter Your Enquiry'); 
      }
      else if ($_POST['message']) { 
			echo(''.$_POST['message'].'');
      } 
      ?></textarea><br />
<input id="send" name="send" class="submitbutton" type="submit" value="Send"></input>

Can anyone help with these problems? I am sure it will be something quite quick and simple but just cannot figure it out so far…

Many Thanks

Jon

Hey

Thanks for the replies all sorted now!

Added ‘>’ as below to solve the echo’ing post value

<textarea id="message" class="commentbox" name="message" rows="4" cols="22">

And the below didn’t quite work for me…

[^A-Za-z0-9,?!.\\s ]+', $message))

But this did…

[^A-Za-z0-9,.?! ]+[\\s]+', $message))

And have also used preg instead now, many thanks for the help!!

Jon

I am sure there are quite many discussions in the forums before you can search with some close keywords.
There might be several solutions for this among which two are as follows which I normally follow:

  • Store the posted values in the session and get back echo/print in the form and do not forget to unset those sessions variables once the submitted values are processed successfully.
  • Second solution would be, just post the form in the same page and validate and redirect the page to desired page after process on successful validation. If validate fails, then your page will remain in the same page where you will have posted values in $_POST global array.

Hope this is clear and you can implement either way. Good luck!

function validateMessage($message){
if(strlen($message) < 1)
return false;
if(ereg(‘[^A-Za-z0-9,?!.\s]+’, $message))
return false;
else
return true;
}

The change is boldened. Also, you should switch to preg instead of ereg (removed in PHP 5.3 and generally rubbish).

The closing > of text area seems to be missing?

<textarea id="message" class="commentbox" name="message" rows="4" cols="22">