SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    Manc, UK
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to not send blank form fields

    I have a php contact form - very basic I must add but is there a way to not send form fields that are left blank. The form has required fields which must be completed and sent but its for a car hire company so only one car may be required so the details for the other two on the form would not be needed. I have added the code that I am using below

    Code:
    <?php
    
    $date=$_POST['date'];
    $time=$_POST['time'];
    $bname=$_POST['bname'];
    $bphone=$_POST['bphone'];
    $gname=$_POST['gname'];
    $gphone=$_POST['gphone'];
    $cname=$_POST['cname'];
    $cadd=$_POST['cadd'];
    $cpost=$_POST['cpost'];
    $recname=$_POST['recname'];
    $recadd=$_POST['recadd'];
    $name=$_POST['name'];
    $recpost=$_POST['recpost'];
    $email=$_POST['email'];
    $inst=$_POST['inst'];
    $car1=$_POST['car1'];
    $c1tim=$_POST['c1tim'];
    $c1pic=$_POST['c1pic'];
    $c1tri=$_POST['c1tri'];
    $c1gu=$_POST['c1gu'];
    $c1ad=$_POST['c1ad'];
    $c1pos=$_POST['c1pos'];
    $car2=$_POST['car2'];
    $c2tim=$_POST['c2tim'];
    $c2pic=$_POST['c2pic'];
    $c2tri=$_POST['c2tri'];
    $c2gu=$_POST['c2gu'];
    $c2ad=$_POST['c2ad'];
    $c2pos=$_POST['c2pos'];
    $car3=$_POST['car3'];
    $c3tim=$_POST['c3tim'];
    $c3pic=$_POST['c3pic'];
    $c3tri=$_POST['c3tri'];
    $c3gu=$_POST['c3gu'];
    $c3ad=$_POST['c3ad'];
    $c3pos=$_POST['c3pos'];
    
    
    
    
    
    
    
    
    $to='admin@mysite.com';
    
    $headers = 'From: '.$name."\r\n" .
    	'Reply-To: '.$email."\r\n" .
    	'X-Mailer: PHP/' . phpversion();
    $subject = 'Car instruction form From wedding Cars Manchester';
    $body='Car instruction form From wedding Cars Manchester'."\n\n";
    $body.='Wedding Date: '.$date."\n\n";
    $body.='Time Of Service: '.$time."\n\n";
    $body.='Brides Name: '.$bname."\n\n";
    $body.='Brides Phone: '.$bphone."\n\n";
    $body.='Grooms Name: '.$gname."\n\n";
    $body.='Grooms Phone: '.$gphone."\n\n";
    $body.='Church Name: '.$cname."\n\n";
    $body.='Church Address: '."\n".$cadd."\n\n";
    $body.='Church Postcode: '.$cpost."\n\n";
    $body.='Reception Name: '.$recname."\n\n";
    $body.='Reception Address: '."\n".$recadd."\n\n";
    $body.='Reception Postcode: '.$recpost."\n\n";
    $body.='Senders Name: '.$name."\n\n";
    $body.='Email Address: '.$email."\n\n";
    $body.='Special Instructions: '."\n".$inst."\n\n";
    $body.='Car 1: '.$car1."\n\n";
    $body.='Time at house for: '.$c1tim."\n\n\n";
    $body.='Pick Up Number: '.$c1pic."\n\n";
    $body.='Trips From House: '.$c1tri."\n\n";
    $body.='Guests To Be Taken To Church: '."\n".$c1gu."\n\n";
    $body.='Address To Be Picked Up From: '."\n".$c1ad."\n\n";
    $body.='Postcode: '.$c1pos."\n\n";
    $body.='Car 2: '.$car2."\n\n";
    $body.='Time at house for: '.$c2tim."\n\n";
    $body.='Pick Up Number: '.$c2pic."\n\n";
    $body.='Trips From House: '.$c2tri."\n\n";
    $body.='Guests To Be Taken To Church: '."\n".$c2gu."\n\n";
    $body.='Address To Be Picked Up From: '."\n".$c2ad."\n\n";
    $body.='Postcode: '.$c2pos."\n\n";
    $body.='Car 3: '.$car3."\n\n";
    $body.='Time at house for: '.$c3tim."\n\n";
    $body.='Pick Up Number: '.$c3pic."\n\n";
    $body.='Trips From House: '.$c3tri."\n\n";
    $body.='Guests To Be Taken To Church: '."\n".$c3gu."\n\n";
    $body.='Address To Be Picked Up From: '."\n".$c3ad."\n\n";
    $body.='Postcode: '.$c3pos."\n\n";
    
    
    
    	
    if(mail($to, $subject, $body, $headers)) {
    	die('Thanks. We will be back to you soon.');
    } else {
    	die('Error: Mail failed');
    }
    
    ?>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    1 never assign values directly from $_POST into variables
    a) that just sets up two fiends with the same content and
    b) you then don't know which variables are tainted and which have been validated.

    so pass the $_POST field through appropriate validation and then save it to the separate untainted variable.

    2. When you come to validate $_POST['car2'] if you trim the content and it is then empty then skip over processing all of the following fields as obviously only one car has been input.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •