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
https://forums.phpfreaks.com/topic/217341-please-help-to-make-requered-textfileds/
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"); }

}

<?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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.