Must Fill The Required Fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Must Fill The Required Fields

Post by Tassadduq »

i have a simple php feedback form with the following fields
name :: email :: message

my form is very simple that if anyone click on submit it generates the message that "your message has been sent successfully".
i want to make my fields required.
anyone can submit my form without writing anythings in field like no name in name field no email address in mail address or no message in message box.
but i want if anyone submit my form in blank format than it should generates an error that you must fill the required field.
here is my form code.

Code: Select all

<?php if( isset($submit)    ) {
 
$subject = "Message From Your Website";
 
$htmlmessage = "
 
<div style=\"border:1px solid #cccccc; width:80%; padding:10px;\">
 
Name: $name<br />
 
Email: $email<br />
 
Message:
 
$message
 
</div>
 
";
 
$headers = "From: Feedback Page  <webmaster@gmail.com>\r\n";
 
$headers .= "Content-Type: text/html;\r\n charset=\"iso-8859-1\"\r\n";
 
$headers .= "Reply-To: $email\r\n";
 
mail("example@gmail.com", $subject, $htmlmessage, $headers);
 
echo "<p> THANKS! Your Message Has Been Sent Successfully.</p>";
 
 
 
}  else {
 
?>
                          <div style="width:400px; margin:auto; text-align:left;">
 
<form method="post" action="<?php $_SERVER['file:///C|/DOCUME~1/Capricon/LOCALS~1/Temp/Rar$DI00.875/PHP_SELF'];?>">
 
  <p align="left">Name
    
    <input type="text" name="name" />
    </p>
  <p align="left">Email
    
    <input type="text" name="email"  />
    </p>
  <p align="left">Message</p>
  <p align="left">
    <textarea name="message" cols="50" rows="10" ></textarea>
  </p>
  <p>
    <label>
    <div align="left">
      <input type="reset" name="Reset" id="button" value="Reset" />
      <input type="submit" value="Send" name="submit" />
    </label>
    
      <div align="left"></div>
  </form>
 
<?php 
 
}
 
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Must Fill The Required Fields

Post by andym01480 »

Couple of things

use

Code: Select all

$_POST['name']
instead of $name

Sessions are one way for this...

Code: Select all

session_start();
at the very top
Work out some logic for detecting whether form values are set and make $_SESSION['name']=$_POST['name'] etc. You could have an error variable that is set for time there is an error.
Then if no errors process it, if errors output the form again...
then you can modify the form fields so

Code: Select all

value="<?php echo $_SESSION['name'];?>"
. The first time round the session variables aren't set...

You must check user values are what you want before sending emails (or any other o/p to browser, database etc). A naughty user could use your form to send spam to the world
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Re: Must Fill The Required Fields

Post by Tassadduq »

here is my code that is placed at the top/first level of the page.
i mean my page starts from this code.

Code: Select all

<?php
 
$submit = $_POST['submit'];
 
$name = $_POST['name'];
 
$email = $_POST['email'];
 
$message = nl2br($_POST['message']);
 
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Must Fill The Required Fields

Post by andym01480 »

Code: Select all

 
session_start();
if(isset($_POST['submit']){
 
foreach ($_POST as $key->$value) //iterate through form values
{
if (isset($value))
  { 
    $form[$key]=$value; //fill form array if $_POST value set
  }
else{
$_SESSION[$key]=$value; //if $_POST not set populate session array
}
 
if(!isset($_SESSION)) {
//form processing
exit();
}
if(isset($_SESSION) echo "<p>Your form had some fields missing...</p>"
 //form with value fields set to output S_SESSIOn['name'] etc
 
Would be how i'd do it - untested!!!!
You can do all sorts of clever things like changing the colour of the form fields that are empty etc...
User avatar
Tassadduq
Forum Commoner
Posts: 60
Joined: Wed Dec 03, 2008 2:53 pm

Re: Must Fill The Required Fields

Post by Tassadduq »

Thanks andym01480.
i have got a little better an easy idea for this.
please tell me is this correct method.
i am using Dreamweaver cs3 and i have used the validate form option from tag inspector pane.
from here i check the all fields required option.
and now all fields must be filled/required
and now form is working very exactly.
is it better??
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Must Fill The Required Fields

Post by andym01480 »

No idea - don't use dreamweaver! There are a lot of ways of skinning a cat (not that I have done that :lol:)It might be using javascript validation. Which is okay for client side.

Whatever way you use, you must properly check input in your php script before sending an email using user input. I din't give you any of that!
Post Reply