Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    Senior Member
    Join Date
    Mar 2004
    Posts
    1,518
    Member #
    5220
    I am using a simple IF check to see if fields are filled in. For example:

    PHP Code:
    IF ($fname == "")
    header ("Location: mailerror.htm"); 
    Now, this works and if the field isn't filled, it goes to mailerror.htm. However, it does not display exactly what fields weren't filled in. How can I make it do this?

    I know I can do it with the echo function, but this puts it in a new page and on a blank white page. So, I guess my question is how can I display exactly what fields were not filled in and display them in the same layout as the page?

    Hope you understand my question. Any ideas?

    Will.
    Rednerve
    Creative Freelance Design

  2.  

  3. #2
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,657
    Member #
    1234
    Liked
    137 times
    javascript can pop up a simple javascript window saying what's missing, or you can code some PHP that checks it. Javascript would be the easiest, but not as secure.
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  4. #3
    Senior Member
    Join Date
    Mar 2004
    Posts
    1,518
    Member #
    5220
    I'm trying to keep away from the Javascript method because I know, as a user, that pop-up messages annoy me so I do not want to make my clients feel that way!

    I know I can code some PHP code, but how do I do it, that was my question .
    Rednerve
    Creative Freelance Design

  5. #4
    Senior Member Eddy Bones's Avatar
    Join Date
    Jan 2004
    Location
    Washington, USA
    Posts
    1,054
    Member #
    4651
    You mean you want the field highlighted or something that have not been filled in? I've never done it before, but I have a method that may work.

    You can make a special CSS class that highlights a form element. You're funcions check to see if the form elements were filled out, and at the end stick the CSS class onto the emtpty elements and display the updated form.

    Dunno if that made sense, and there may be an easier way to do it. I'm not sure. I'd make you an example (and try it out myself), but I'm at school now. When I get home I'll make that up for ya.

    Here's a small example:

    PHP Code:
    if (!$variable) {
      
    $display .= "<input class=\"xxxx\" type=\"text\" ... >";
      } else {
      
    $display .= "<input type=\"text\" ... >";
      } 
    A series of those. Does that make any sense? Lol.

  6. #5
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I wrote this as an example in a hurry (might be possible to trim it down a little).
    It's a basic form with a hidden field that triggers a red explanatory text above the form, and a red star outside the field with missing info:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <title>Untitled Document</title> 
    <style type="text/css"> 
    <!-- 
    .style1 {font-family: Verdana, Arial, Helvetica, sans-serif} 
    .style2 {color: #FF0000} 
    --> 
    </style> 
    </head> 

    <body> 
    <p>&nbsp;</p> 
    <?php 
    if(@!$_POST['firstname'] || @!$_POST['lastname'] || @!$_POST['email']) 

        if(@
    $_POST['sent']) 
        { 
            print 
    "<div align='center' class='style2'><em>Fill in starred fields </em></div>"
        } 
    ?> 
    <form name="form1" method="post" action="<?php echo $PHP_SELF ?>"> 
    <table class="style1" width="300" align="center" cellpadding="5"> 
      <tr> 
        <td width="26"><?php if (@!$_POST['firstname'] && @$_POST['sent']) echo "<font color='#FF0000'>*</font>" ?></td> 
        <td width="150"> 
          <input type="text" name="firstname" value="<?php echo @$_POST['firstname'?>"> 
        </td> 
        <td width="102">firstname</td> 
      </tr> 
      <tr> 
        <td><?php if (@!$_POST['lastname'] && @$_POST['sent']) echo "<font color='#FF0000'>*</font>" ?></td> 
        <td><input type="text" name="lastname" value="<?php echo @$_POST['lastname'?>"></td> 
        <td>lastname</td> 
      </tr> 
      <tr> 
        <td><?php if (@!$_POST['email'] && @$_POST['sent']) echo "<font color='#FF0000'>*</font>" ?></td> 
        <td><input type="text" name="email" value="<?php echo @$_POST['email'?>"></td> 
        <td>email</td> 
      </tr> 
      <tr> 
        <td><input type="hidden" name="sent" value="1"></td> 
        <td><div align="center"> 
          <input type="submit" name="Submit" value="Submit"> 
        </div></td> 
        <td>&nbsp;</td> 
      </tr> 
    </table> 
    </form> 
    <?php 

    else 

    ?> 
    <div align="center"><span class="style1"><br />The form was successfully submitted!</span></div>  <?php 

    ?> 
    </body> 
    </html>
    As you can see, there are nothing extra here like a reg-ex check of the validity of the email address or anything, it's just a plain form with some script embedded.

    Upload it and test. If you leave one or two fields open and click submit, you will get a warning message and a star in front of the blank field(s).
    If you fill out all fields, it will be happy.
    S. Rosland

  7. #6
    Senior Member Richard S's Avatar
    Join Date
    Jul 2004
    Location
    SW Lonodn
    Posts
    219
    Member #
    6728
    Liked
    3 times
    Heres the code I normaly use to send a email in PHP:
    PHP Code:
    <?
    $name
    =$_POST['name'];
    $email=$_POST['email'];

    $main=$_POST['main'];

    $mp=$_POST['werefrom'];

    $nameflag 0;
    $emailflag 0;

    $contents "<html>
               <head>
               </head>
               <body>
               <h3>Form Return From RRSS.co.uk</h3>
               <b>Name</b> : 
    $name<br>
               <b>Email</b> : <a href=\"mailto:
    $email\">$email</a><br><br>
               <b>Message</b> :<br> 
    $main
               </body>
               </html>"
    ;


    $subject'Form return';



    ?>
     <?
    if (empty($mp))
        {
             if (
    $_GET['t'] == 'sent')    
                {
                    
    $topmsg "<CENTER>
                    <b>Thank You!</U></B></font><BR><BR>
                    You message has been sent<BR><BR>
                    </CENTER>"
    ;
                }
            else
                {
                    
    $topmsg "";
                }
         }
    else 
         {    
            if (empty(
    $email) && empty($name))
                 {
                    
    $topmsg "&nbsp;&nbsp; You did not enter a <B><U>email address or name,</U></B> Please enter and try again";
                     
    $nameflag 1;
                    
    $emailflag 1;
                }
            elseif (empty(
    $name))
                 {
                    
    $topmsg "&nbsp;&nbsp; You did not enter a <B><U>name,</U></B> Please enter and try again";
                    
    $nameflag 1;
                    
    $emailflag 0;
                 }
            elseif (empty(
    $email))
                 {
                    
    $topmsg "&nbsp;&nbsp; You did not enter a <B><U>email address,</U></B> Please enter and try again";
                    
    $emailflag 1;
                    
    $nameflag 0;
                 }
            
            else
                {
                    if (
    mail("richardgsweet@aol.com"'Form Return From RRSS'$contents"From: FormReturn@rrss.co.uk")) 
                    {
                        @include(
    "http://www.rrss.co.uk/contact.php?t=sent");
                        exit (
    0);
                    }
                
                else
                
    $topmsg "<center><B><U>There is a problem sending your mail. <BR> Please try again later.</U></B> </center>";
                }    
        }
     
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
     <?
     
    echo '<center>'.$topmsg.'</center>';
     
    ?>
      
      <BR><BR>

     <FORM name="contact" style="margin-left:40;" method="post" action="contact.php">
     <input style="margin-left: 40px;" name="werefrom" type="hidden" value="posted">
     
     <table border="0">
     <TR>
     <TD>
    <? 
        
    if ($nameflag == 1
            { 
                echo 
    "<font color=\"red\"><B>*</B></FONT>"
            } 
        else
            {
                echo 
    "&nbsp";
            }
    ?> 
     Name: <input name="name" title="Please Enter Your Name Here." size="20" value= "<? echo $name?>" maxlength="35"><P>
    <? 
        
    if ($emailflag == 1
             { 
                echo 
    "<font color=\"red\"><B>*</B></FONT>" ;
             } 
        else
            {
                echo 
    "&nbsp";
            }
    ?> 
    &nbsp;Email:</font> <input name="email" title="Please Enter Your Email Addreess Here." size="20" value="<? echo $email?>" maxlength="35"><P>

    Message:<BR>
    <center>
    <textarea name="main" title="Please Enter Any Questions, Comments, Or Further Information In Here." cols="25" rows="5"><? echo $main?></textarea>

     <BR><BR>  <P>
      
    <input type="submit" value="Send" />
    </form>
     </td>
     </TR>
     </table>


    </body>
    </html>

  8. #7
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Edited my code example.
    Added some 'undefined index' supressors, in case you have a different config in error reporting in your php.ini file.

    If you don't have turned off NOTICE warnings like this:
    error_reporting = E_ALL & ~E_NOTICE

    but have this:
    error_reporting = E_ALL

    It will report/warn of all undefined variables.
    S. Rosland

  9. #8
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    A good way of displaying fields which haven't been filled out is to use a session to temporaily store the form information. here is the logic behind it - it has worked well for me and I use it in my site at the moment.

    First we have a form template:

    form_template.php
    PHP Code:
    <?php
        
    if (isset($_GET['errors'])) {
            
    /* this means our form has been rejected due to errors 
               a session should have been created in the form processor
               and we will start it here
             */
             
    session_start();

             
    /* load the form data from the session into a local variable */
             
    $form_data $_SESSION['form_data'];

             
    /* now we've collected the form data we can delete the session */
             
    session_destroy();
        }

    /* below is the template we use the @ suppression operator to prevent
       undeclared variable notices from disp[lay if there weren't any errors */
    ?>
    <html>
        <head>
            <title>Form Redirection Example</title>
        </head>
        <body>
            <form method="post" action="form_processor.php">
                <p><?php echo(@$form_data['err']) ?></p>
                <table>
                    <tr>
                        <td></td>
                        <td><?php echo(@$form_data['string']['err']) ?></td>
                    </tr>
                    <tr>
                        <td>Enter some text here:</td>
                        <td><input type="text" name="string"
                                   value="<?php echo(@$form_data['string']['value']) ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><?php echo(@$form_data['number']['err']) ?></td>
                    </tr>
                    <tr>
                        <td>Enter some text here:</td>
                        <td><input type="text" name="number"
                                   value="<?php echo(@$form_data['number']['value']) ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit Form" /></td>
                    </tr>
                </table>    
            </form>
        </body>
    </html>
    We also need a processing script:

    form_processor.php
    PHP Code:
    <?php
        $form_data 
    = Array();
        
    $error false;
        
        
    /* check for data */
        
    if (! posted('string''number')) {
            
    $form_data['err'] = 'Invalid data format.';
            
    $error true;
        } else {
            
    // assume magic quotes is off
            
    $string $_POST['string'];
            
    $number $_POST['number'];

            if (
    $string == '' ) { // its been left blank
                
    $form_data['string']['err'] = 'This field is required.';
                
    $error true;
            }

            if (
    $number == '') { // its been left blank
                
    $form_data['number']['err'] = 'This field is required.';
                
    $error true;
            } else if (! 
    is_numeric($number)) { // not a number
                
    $form_data['number']['err'] = 'This field must be a number.';
                
    $error true;
            } 
        }
        
        
    /*  check if any errors occured */
        
    if ($error) {    
            if (! isset(
    $form_data['err'])) {
                
    $form_data['string']['value'] = htmlspecialchars($string);
                
    $form_data['number']['value'] = htmlspecialchars($number);
            }
        
            
    // force the session to use the query string
            
    ini_set('session.use_cookies''0');
            
    session_start();
            
            
    /* store the errors and data tempraily in the session */
            
    $_SESSION['form_data'] = $form_data;
        
            
    /* go back to the form */
            
    header('Location: form_template.php?errors=1&' SID);
        } else {
            
    /* we could redirect to the success page here */
            
    echo('success');
        }

        
    /* helper function to test variables were submitted via HTTP POST */
        
    function posted()
        {
            
    $num_args func_num_args();
            
    $args func_get_args();

            for(
    $i 0$i $num_args$i++) {
                if (! isset(
    $_POST[$args[$i]])) {
                    return 
    false;
                }
            }
            
            return 
    true;
        }
    ?>
    You can see a working example here, hope it helps:

    http://adam.codedv.com/examples/form...m_template.php


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:56 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com