5 Replies - 2041 Views - Last Post: 19 June 2012 - 08:57 AM Rate Topic: -----

#1 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-March 12

Display error message in form field

Posted 18 June 2012 - 11:27 AM

I have created a form and formatted it in CSS. The field inputs are then validated by a php script and an error code generated if there is a data error. If an error is generated, the error flag is set and the user is returned to the form. I can display the error above the field input box, but it skews the label. I would like to have the error message displayed in the field input box, but can find no reference online about how to do this in PHP. The PHP error sections in the following code are at present only used for debugging.


          <div id="contact">
   
          <h1>Contact Form</h1>

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

                <fieldset>
                         
                   <label for="name">Name:</label>
                            
<?php

$error_flag = 0;

$error_flag = ($_SESSION['form_error']);

$error_flag2 = ($error_flag & 1);

 if ($error_flag & 1) {

          echo $error_flag2;
         }
 else {
      
          echo "no error";
         }

?>
                   
                   <input name="name" type="text" id="name" placeholder="Enter your full name"/>

                            
                   <label for="email">Email:</label>

<?php

$error_flag = 0;

$error_flag = ($_SESSION['form_error']);

$error_flag2 = ($error_flag & 10);

 if ($error_flag & 10) {

          echo $error_flag2;
         }
 else {
      
          echo "no error";
         }

?>
                            
                   <input name="email" type="email" id="email"  placeholder="Enter your email address" />
                            
                            
                   <label for="message">Message:</label>

<?php

$error_flag = 0;

$error_flag = ($_SESSION['form_error']);

$error_flag2 = ($error_flag & 100);

 if ($error_flag & 100) {

          echo $error_flag2;
         }
 else {
      
          echo "no error";
         }

?>

                   <textarea name="message" id="message" placeholder="What's on your mind?"></textarea>
                            
                            
                   <input type="submit" value="Send message" />

                </fieldset>
                         
             </form>                            

       </div><!-- ends class contact -->





Is This A Good Question/Topic? 0
  • +

Replies To: Display error message in form field

#2 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: Display error message in form field

Posted 18 June 2012 - 11:37 AM

If I understand you correctly, you want the error to be put into the input box? If so, change your echo statements to put the error inside of the input boxes.

Example:
echo "<input name='name' type='text' id='name' value='" . $error . "' />";

Was This Post Helpful? 1
  • +
  • -

#3 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-March 12

Re: Display error message in form field

Posted 18 June 2012 - 11:43 AM

Thank you I'll try it.
Was This Post Helpful? 0
  • +
  • -

#4 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-March 12

Re: Display error message in form field

Posted 18 June 2012 - 01:06 PM

It works great except in the textarea it does not display anything


 if ($error_flag & 100) {

          echo "<textarea name='message' id='message' value='" .$message_error. "'></textarea>";
         }
 else {
      
          echo "<textarea name='message' id='message' placeholder='What's on your mind?'></textarea>";
         }


Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,676
  • Joined: 30-January 09

Re: Display error message in form field

Posted 19 June 2012 - 02:44 AM

There;s a couple of small problems with your code. Firstly, I believe the value of a textarea is put between the tags. Also, in the else part, you need to escape the single quote, but the placeholder attribute is non-standard. If you're using it as a custom placeholder, fair enough, but I'd try this instead:

 if ($error_flag & 100) {

          echo "<textarea name='message' id='message'>$message_error</textarea>";
         }
 else {
      
          echo "<textarea name='message' id='message'>What's on your mind?</textarea>";
         }


Was This Post Helpful? 2
  • +
  • -

#6 don57  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 15-March 12

Re: Display error message in form field

Posted 19 June 2012 - 08:57 AM

Worked great Thank You. You were right placeholder is defined in the CSS sheet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1