Jump to content

Please help to make requered textfileds!!!


x-nitro

Recommended Posts

Dear friends,

Can anybody help me just to add php code to make field as a required?

 

<?

if(!isset($_POST['answer'])) {

?>

 

 

<form action="index.php" method="post">

<p></p>Please fill out your personal details and answer the questions by clicking on your answer.</p>

<p>Good luck!</p>

 

<table cellspacing="0" cellpadding="3" border="0">

<tr>

<td>Your Name:*</td>

<td><input type="text" name="name" /></td>

</tr>

<tr>

<td>Tel #:*</td>

<td><input type="text" name="tel" /></td>

</tr>

<tr>

<td>Email:*</td>

<td><input type="text" name="email" /></td>

</tr>

<tr>

<td colspan="2">

 

 

 

Link to comment
Share on other sites

Can do something like this (substitute your variables of course)

 

if (isset($_POST['uname']) || isset($_POST['pword']))

{

  //submitted, check values

  if (empty($_POST['uname'])) { die ("Error: Enter a username"); }

  if (empty($_POST['pword'])) { die ("Error: Enter a password"); }

}

Link to comment
Share on other sites

<?php
if ( isset($_POST['submit']) )
{
   $name   = isset($_POST['name'])  ? $_POST['name']  : '';
   $tel    = isset($_POST['tel'])   ? $_POST['tel']   : '';
   $email  = isset($_POST['email']) ? $_POST['email'] : '';

   $errors = array();
   
   if ( empty($name) )
   {
      $errors[] = 'You did not enter your name.';
   }
   if ( empty($_POST['tel']) )
   {
      $errors[] = 'You did not enter your telephone number.';
   }
   if ( empty($_POST['email']) )
   {
      $errors[] = 'You did not enter your email address.';
   }
   
   if ( filter_var($email, FILTER_VALIDATE_EMAIL) === false )
   {
      $errors[] = 'You must enter a valid email address.';
   }
   
   if ( count($errors) == 0 )
   {
      //do something with collected data i.e. insert into a database
      // echo 'Done';
   }
   else
   {
      $error = <<<ERROR
      <table style="width: 200px; margin: 0 auto 0 auto; color: darkred; border: 1px solid #000;>
        <tr>
          <th style="font-weight: bold;">Errors:</th>
        </tr>
        <tr><td>
        implode("</td></tr>\r\n\t\t<tr><td>", $errors)
        </td></tr>
      </table>
ERROR;
      
      echo $error;
   }
      
}

?>
   <form action="index.php" method="post">
   <p></p>Please fill out your personal details and answer the questions by clicking on your answer.</p>
   <p>Good luck!</p>
   
   <table cellspacing="0" cellpadding="3" border="0">
      <tr>
         <td>Your Name:*</td>
         <td><input type="text" name="name" /></td>
      </tr>
      <tr>
         <td>Tel #:*</td>
         <td><input type="text" name="tel" /></td>
      </tr>
      <tr>
         <td>Email:*</td>
         <td><input type="text" name="email" /></td>
      </tr>
      <tr>
         <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
      </tr>
   </table>
   </form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.