SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question simple email form need required fields

    hello all
    i have this email form which i downloaded from the net but getting blank emails
    so presume peeps messing about
    is there a way to have name and email addy as a required field before submission
    cheers
    Doug

    this is the code i have

    PHP Code:
    <?php
         
    if ($_SERVER['REQUEST_METHOD'] != 'POST'){                          
         
    $me $_SERVER['PHP_SELF'];                    
    ?>                       
     </div>
     
    <form name="form1" method="post"                             
         action="<?php echo $me;?>">
                                  
                <table width="600" border="0" cellspacing="0" cellpadding="2" align="center">                             
                <tr>                                
                <td align="left"><span>Name:</span></td>                                
                <td align="left"><input type="text" name="Name"></td>                             
                </tr>                             
                <tr>                                
               <td align="left"><span>Telephone Number:</span></td>                                
               <td align="left"><input type="text" name="TelNo"></td>                             
               </tr>                            
               <tr>                                
               <td align="left"><span>Email:</span></td>                                
                <td align="left"><input type="text" name="Email"></td>                            
                </tr>                             
               <tr>                                
               <td align="left" valign="top"><span>Enquiry:</span></td>                                
               <td align="left"><textarea name="MsgBody" cols="30" rows="5"></textarea></td>                             
               </tr>                             
              <tr>                                
              <td align="left"><input type="submit" name="Submit" value="Send"></td>                               
              </tr>                          
             </table>                       
           </form>                   
    <?php                             
    } else {                                  
                    
    error_reporting(0);                                  
                    
    $recipient 'joeblogs@hotmail.co.uk';                                  
                    
    $subject 'Caravan Hire';                                  
                    
    $from stripslashes($_POST['Name']);                                  
                    
    $telno stripslashes($_POST['TelNo']);                                  
                    
    $email stripslashes($_POST['Email']);                                  
                    
    $company stripslashes($_POST['Company']);                                  
                    
    $message stripslashes($_POST['MsgBody']);                                  
                    
    $msg "Message from: $from\n\nTelephone Number: $telno\n\nEmail: $email\n\nUsers Message:\n\n $message\n\n";                                  
                    if (
    mail($recipient$subject$msg))                                     
                    echo 
    nl2br("<b>Message Sent</b>");                                  
                    else                                     
                    echo 
    "An unknown error occurred.";
                                          
     }        
    ?>
              </tr>
              </table>
              </form>

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($name || $email || $address))
    {
    //submit form
    }
    else
    {
    echo 
    'form not filled';


  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also for mail function try using get instead of post

  4. #4
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for quick reply
    but where do i put this on the form
    sorry bit of a newbie
    cheers
    Doug

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:

    PHP Code:
    <html>             
    <?php                              
    if(isset($_GET['submit']))
        {
        
    //short vaiables
        
    $your_form_variables trim($_GET['your_form_variables']);
        
    $your_form_variables trim($_GET['your_form_variables']);
        
    $your_form_variables trim($_GET['your_form_variables']);
        
    $your_form_variables trim($_GET['your_form_variables']);
                        
                
    if(!
    $your_form_variables || !$your_form_variables || !$your_form_variables || !$your_form_variables)
            {
            echo
    '<font color="#000000" size="+1">ALERT!! You have not filled all fileds.  Email not submitted</font>';
            exit;
            }

                                       
                    
    error_reporting(0);                                   
                    
    $recipient 'xxxxxxx';                                   
                    
    $subject 'Caravan Hire';                                   
                    
    $from stripslashes($_GET['Name']);                                   
                    
    $telno stripslashes($_GET['TelNo']);                                   
                    
    $email stripslashes($_GET['Email']);                                   
                    
    $company stripslashes($_GET['Company']);                                   
                    
    $message stripslashes($_GET['MsgBody']);                                   
                    
    $msg "Message from: $from\n\nTelephone Number: $telno\n\nEmail: $email\n\nUsers Message:\n\n $message\n\n";                                   
                    if (
    mail($recipient$subject$msg))                                      
                    echo 
    nl2br("<b>Message Sent</b>");                                   
                    else                                      
                    echo 
    "An unknown error occurred."
                                           
     }     
    ?> 

                                
                <table width="600" border="0" cellspacing="0" cellpadding="2" align="center">    
    <form name="form1" method="get" action="<?php $_SERVER['PHP_SELF'?>">                           
                <tr>                                 
                <td align="left"><span>Name:</span></td>                                 
                <td align="left"><input type="text" name="Name"></td>                              
                </tr>                              
                <tr>                                 
               <td align="left"><span>Telephone Number:</span></td>                                 
               <td align="left"><input type="text" name="TelNo"></td>                              
               </tr>                             
               <tr>                                 
               <td align="left"><span>Email:</span></td>                                 
                <td align="left"><input type="text" name="Email"></td>                             
                </tr>                              
               <tr>                                 
               <td align="left" valign="top"><span>Enquiry:</span></td>                                 
               <td align="left"><textarea name="MsgBody" cols="30" rows="5"></textarea></td>                              
               </tr>                              
              <tr>                                 
              <td align="left"><input type="submit" name="Submit" value="Send"></td>                                
              </tr>                           
             </table>                        
           </form>       
              </tr> 
              </table> 
              </form>
    </html>
    if you want to force validation on the client side you need to use JavaScript
    hth

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code has to go in your form processing code ( the form post back to the same page ). Your op indicates you want to check for these values before form submission, you will need a little javascript to do that. Php is a server side language, which means that the form must be submitted for the code to be processed.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do i have to change these
    sorry not with it today

    PHP Code:
    //short vaiables     $your_form_variables = trim($_GET['your_form_variables']);     $your_form_variables = trim($_GET['your_form_variables']);     $your_form_variables = trim($_GET['your_form_variables']);     $your_form_variables = trim($_GET['your_form_variabl 

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes. chaneg them to your own form input variables.

  9. #9
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok
    sorry to say could not get it to work
    can you suggest another way which may be easier for me to use
    cheers
    Doug

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd probably be better off searching Google for some tutorials on the subject (look up JavaScript form validation too, so you can validate the form client side and server side). You've been offered some solutions, so if you can't make them work, your best bet is probably reading up on the subject, as it will be covered in more detail.

    Good luck.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •