PHP form Validation

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 2275 Views - Last Post: 11 April 2012 - 09:05 AM Rate Topic: -----

#1 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

PHP form Validation

Posted 05 April 2012 - 07:41 AM

I'm trying to validate a form before I send it using PHP, here is the form:

<form method='post' action='mailform.php'>
  <fieldset>
  <label for='message'><b>Reflection:<b></label><br/><br/>
	<p>Tell us what you thought of the Workshop, did you enjoy the tasks?, Could we have done anything different?</p>
  <textarea name='message' rows='15' cols='80'>
  </textarea><br/>
  <input type='submit' VALUE='Send' size='5' />
  </fieldset>
  </form>


and here is the mailform.php:

<?php
							if (isset($_REQUEST['message']))
//if "feedback" is filled out, send feedback
  {
  //send email
 $message = $_REQUEST['message'] ;
 mail('email', 'Feedback From Workshop 1', $message);
 echo "<p>Thanks for sending your feedback, hopefully it will help us improve the experience for other students.</p>";
 echo "<p>You are now being redirected to the homepage</p>";
 }
else
//if "email" is not filled out, display error message
  {
  echo "No information entered";
  }
?>


when I send an empty form it still emails it, what am I doing wrong? also is there just a simple javascript code I could use that would just flash up red text in the message box?

This post has been edited by Addio569: 05 April 2012 - 07:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP form Validation

#2 Macjohn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: PHP form Validation

Posted 05 April 2012 - 07:56 AM

I would put my validation code in mailform.php...

Something like this


if(message!=''){
echo "error, message is empty
}
else{
send email
}
Was This Post Helpful? 0
  • +
  • -

#3 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 08:11 AM

View PostMacjohn, on 05 April 2012 - 08:56 AM, said:

I would put my validation code in mailform.php...

Something like this


if(message!=''){
echo "error, message is empty
}
else{
send email
}


I tried:

              <?php
							if(message!=''){
echo "error, message is empty
}
else{
 $message = $_REQUEST['message'] ;
 mail('email', 'Feedback From Workshop 1', $message);
 echo "<p>Thanks for sending your feedback, hopefully it will help us improve the experience for other students.</p>";
 echo "<p>You are now being redirected to the homepage</p>";
 }

?>


but got error message:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in mailform.php on line 136



which is
$message = $_REQUEST['message'] ;

This post has been edited by Addio569: 05 April 2012 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: PHP form Validation

Posted 05 April 2012 - 08:15 AM

There's a newline and space character in the textarea by default.

Change it to:
<textarea name='message' rows='15' cols='80'></textarea><br/>


Also, use this instead of isset:
if (!empty($_REQUEST['message'])){



Whenever isset is used, you will need to use strlen function with it to check if the length of input is not zero :)
Was This Post Helpful? 0
  • +
  • -

#5 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 08:21 AM

View PostsBorg, on 05 April 2012 - 09:15 AM, said:

There's a newline and space character in the textarea by default.

Change it to:
<textarea name='message' rows='15' cols='80'></textarea><br/>


Also, use this instead of isset:
if (!empty($_REQUEST['message'])){



Whenever isset is used, you will need to use strlen function with it to check if the length of input is not zero :)


like this:

 <?php
	if (!empty($_REQUEST['message'])){
        echo "error, message is empty
	}
else{
 $message = $_REQUEST['message'];
 mail('email', 'Feedback From Workshop 1', $message);
 echo "<p>Thanks for sending your feedback, hopefully it will help us improve the experience for other students.</p>";
 echo "<p>You are now being redirected to the homepage</p>";
 }

?>


same error message :(
Was This Post Helpful? 0
  • +
  • -

#6 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: PHP form Validation

Posted 05 April 2012 - 08:22 AM

echo "error, message is empty

Where's the ending double quote and semi-colon? :|
Was This Post Helpful? 0
  • +
  • -

#7 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 08:27 AM

View PostsBorg, on 05 April 2012 - 09:22 AM, said:

echo "error, message is empty

Where's the ending double quote and semi-colon? :|


sorry :( I've been doing this for hours, my eyes hurt. ok.

so I've now got:

<?php
if (!empty($_REQUEST['message']))
{
echo "error, message is empty";
}
else{
 $message = $_REQUEST['message'];
 mail('email', 'Feedback From Workshop 1', $message);
 echo "<p>Thanks for sending your feedback, hopefully it will help us improve the experience for other students.</p>";
 echo "<p>You are now being redirected to the homepage</p>";
 }



but it still sends the email even if its blank!

This post has been edited by Addio569: 05 April 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: PHP form Validation

Posted 05 April 2012 - 08:30 AM

Did you change the form as I mentioned in my previous reply?

There's a newline and space in the textarea!
Was This Post Helpful? 0
  • +
  • -

#9 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 08:32 AM

View PostsBorg, on 05 April 2012 - 09:30 AM, said:

Did you change the form as I mentioned in my previous reply?

There's a newline and space in the textarea!


yes I did that:

<form name='reflection' form method='post' action='mailform.php'>
   <fieldset>
  <label for='message'><b>Reflection:<b></label><br/><br/>
	<p>Tell us what you thought of the Workshop, did you enjoy the tasks?, Could we have done anything different?</p>
  <textarea name='message' rows='15' cols='80'></textarea><br/>
  <input type='submit' VALUE='Send' size='5' />
  </form>

This post has been edited by Addio569: 05 April 2012 - 08:33 AM

Was This Post Helpful? 0
  • +
  • -

#10 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: PHP form Validation

Posted 05 April 2012 - 08:40 AM

Do a var_dump($_REQUEST) before that if condition and check the output.

I'm using the below code and it seems to work perfectly fine.
<form name='reflection' form method='post' action=''>
    <fieldset>
        <label for='message'><b>Reflection:</b></label>
        <br /><br />
        <p>Tell us what you thought of the Workshop, did you enjoy the tasks?, Could we have done anything different?</p>
        <textarea name='message' rows='15' cols='80'></textarea><br />
        <input type='submit' VALUE='Send' size='5'/>
    </fieldset>
</form>

<?php
//if "feedback" is filled out, send feedback
if (!empty($_REQUEST['message'])) {
    echo "here" . $_REQUEST['message'] . "..";
    die;
} else {
    echo "No information entered";
}
?>

Was This Post Helpful? 0
  • +
  • -

#11 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 08:46 AM

View PostsBorg, on 05 April 2012 - 09:40 AM, said:

Do a var_dump($_REQUEST) before that if condition and check the output.

I'm using the below code and it seems to work perfectly fine.
<form name='reflection' form method='post' action=''>
    <fieldset>
        <label for='message'><b>Reflection:</b></label>
        <br /><br />
        <p>Tell us what you thought of the Workshop, did you enjoy the tasks?, Could we have done anything different?</p>
        <textarea name='message' rows='15' cols='80'></textarea><br />
        <input type='submit' VALUE='Send' size='5'/>
    </fieldset>
</form>

<?php
//if "feedback" is filled out, send feedback
if (!empty($_REQUEST['message'])) {
    echo "here" . $_REQUEST['message'] . "..";
    die;
} else {
    echo "No information entered";
}
?>


i'm sorry I have no idea what a var dump request is?? you want me to put that php code in instead?

edit: I put the php code in and when i send an empty message it sends but if i put text in it comes back with error, message is empty!

This post has been edited by Addio569: 05 April 2012 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

#12 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: PHP form Validation

Posted 05 April 2012 - 09:06 AM

Uhm add
 var_dump($_REQUEST)
before
if (!empty($_REQUEST['message']))


It'll print all the elements of $_REQUEST array.

Also, kindly post the updated code. :)
Was This Post Helpful? 0
  • +
  • -

#13 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 09:15 AM

View PostsBorg, on 05 April 2012 - 10:06 AM, said:

Uhm add
 var_dump($_REQUEST)
before
if (!empty($_REQUEST['message']))


It'll print all the elements of $_REQUEST array.

Also, kindly post the updated code. :)


thanks for all your help by the way :)

ok:

<?php
//if "feedback" is filled out, send feedback
var_dump($_REQUEST);
if (!empty($_REQUEST['message'])) {
    echo "here" . $_REQUEST['message'] . "..";
    die;
} else {
    echo "No information entered";
}
?>


no text in box gives back:

array(4) { ["message"]=> string(0) "" ["siteowner"]=> string(1) "1" ["ID_my_site"]=> string(11) "addiosamigo" ["Key_my_site"]=> string(32) "f3fcadda92b066e894a01342bee00a79" } No information entered


if I send a message it brings back:

array(4) { ["message"]=> string(4) "test" ["siteowner"]=> string(1) "1" ["ID_my_site"]=> string(11) "addiosamigo" ["Key_my_site"]=> string(32) "f3fcadda92b066e894a01342bee00a79" } heretest..

Was This Post Helpful? 0
  • +
  • -

#14 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: PHP form Validation

Posted 05 April 2012 - 09:28 AM

Uhm it seems to be working fine!

In the first case, when there's no message entered you get -> No information entered

In the second case, when you enter "test" as message you get -> heretest..

I've added a die statement to kill the script after that message, just change the if condition in your original code. I posted a sample, not the complete code :P
Was This Post Helpful? 0
  • +
  • -

#15 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP form Validation

Posted 05 April 2012 - 09:40 AM

View PostsBorg, on 05 April 2012 - 10:28 AM, said:

Uhm it seems to be working fine!

In the first case, when there's no message entered you get -> No information entered

In the second case, when you enter "test" as message you get -> heretest..

I've added a die statement to kill the script after that message, just change the if condition in your original code. I posted a sample, not the complete code :P


sorry I'm not following, like this:

<?php
if (!empty($_REQUEST['message'])) {
    echo "Error, No Information entered";
    die;
} 
else{
 $message = $_REQUEST['message'];
 mail('email', 'Feedback From Workshop 1', $message);
 echo "<p>Thanks for sending your feedback, hopefully it will help us improve the experience for other students.</p>";
 echo "<p>You are now being redirected to the homepage</p>";
 }
?>

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3