title: PHP POST single page form - hiding validation etc. initially
body: <p>This is my first go at PHP and I've pieced together a form that all works on one page. The trouble is, it displays "Please enter your email address so we can contact you" when you first visit the page but I only want the errors/success message after the submit button is pressed.</p>
  
  <p>Additionally, I would like all messages to be wrapped in a div when they are outputted.</p>
  
  <p>I read a post that suggests using something like the following, but when I tried to implement, NO messages would appear on pressing submit:</p>
  
  <pre><code>if (isset($_POST['submit'])) 
  { 
      // Process Form
  }
  else
  {
      // Show Form
  }
  </code></pre>
  
  <p>Anyway, here's my full code:</p>
  
  <pre><code>&lt;?php 
   $to = "me@gmail.com" ; 
   $from = $_REQUEST['Email'] ; 
   $name = $_REQUEST['Name'] ; 
   $headers = "From: $from"; 
   $subject = "Web Contact Data";
   $startmonth = $_REQUEST['StartMonth'];
   $startyear = $_REQUEST['StartYear'];
   $endmonth = $_REQUEST['EndMonth'];
   $endyear = $_REQUEST['EndYear'];
   $message = $_REQUEST['Message'];
  
   $fields = array(); 
   $fields{"Name"} = "Name"; 
   $fields{"Email"} = "Email"; 
   $fields{"Phone"} = "Phone"; 
  
   $selectedProjects  = 'None';
  if(isset($_POST['projects']) &amp;&amp; is_array($_POST['projects']) &amp;&amp; count($_POST['projects']) &gt; 0){
      $selectedProjects = implode(', ', $_POST['projects']);
  }
   $selectedSkills  = 'None';
  if(isset($_POST['skills']) &amp;&amp; is_array($_POST['skills']) &amp;&amp; count($_POST['skills']) &gt; 0){
      $selectedSkills = implode(', ', $_POST['skills']);
  }
  $selectedNoRush  = 'None';
  if(isset($_POST['norush']) &amp;&amp; is_array($_POST['norush']) &amp;&amp; count($_POST['norush']) &gt; 0){
      $NoRush= implode(', ', $_POST['norush']);
  }
  $selectedWhenReady  = 'None';
  if(isset($_POST['whenready']) &amp;&amp; is_array($_POST['whenready']) &amp;&amp; count($_POST['whenready']) &gt; 0){
      $WhenReady= implode(', ', $_POST['whenready']);
  }
  $selectedBudget  = 'None';
  if(isset($_POST['budget']) &amp;&amp; is_array($_POST['budget']) &amp;&amp; count($_POST['budget']) &gt; 0){
      $selectedBudget= implode(', ', $_POST['budget']);
  } 
  
  $body = "We have received the following information:\n\n"; foreach($fields as $a =&gt; $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
  $body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
  $body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
  $body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
  $body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
  $body .= 'Budget: ' . $selectedBudget . "\n\n";
  $body .= 'Message:' . $message . "\n";
  
  
  
   $headers2 = "From: me@gmail.com"; 
   $subject2 = "Thank you for contacting us"; 
   $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";
  
  
   if($from == '') {print "Please enter your email address so we can contact you.";} 
   else { 
   if($name == '') {print "Please enter your name so we know who sent this.";} 
   else {
   if($message == '') {print "Please give a brief description so we can understand how best to help.";} 
   else {
   $send = mail($to, $subject, $body, $headers); 
   $send2 = mail($from, $subject2, $autoreply, $headers2); 
   if($send) 
   {print "Thank you. Your request has been successfully submitted.";} 
   else 
   {print "We encountered an error sending your mail, please check your details are correct or email us at hello@lyonempire.co.uk"; } 
  }}}
   ?&gt;
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>PS. If anyone wants to point out what is better practice for brackets and laying out the code, feel free. I know I could be doing it better, but not sure how.</p>
  
  <p>MC</p>
  
  <p>UPDATE - Here's the html:</p>
  
  <pre><code>&lt;form method="post"&gt;
  
   &lt;h3&gt;Your Details&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
      &lt;input type="text" name="Name" maxlength="99" id="fullname" placeholder="Name" /&gt;
      &lt;input type="email" name="Email" maxlength="99"placeholder="Email Address" /&gt;
      &lt;input type="tel" name="Phone" maxlength="25" placeholder="Phone Number" /&gt;
  &lt;/div&gt;
  
  &lt;h3&gt;Project Type&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
      &lt;fieldset&gt;
          &lt;input type="checkbox" id="type1" name="projects[]" value="Web"&gt;
         &lt;label for="type1"&gt;Web&lt;/label&gt;
          &lt;input type="checkbox" id="type2" name="projects[]" value="Digital Media"&gt;
         &lt;label for="type2"&gt;Digital Media&lt;/label&gt;
          &lt;input type="checkbox" id="type3" name="projects[]" value="Consultancy"&gt;
         &lt;label for="type3"&gt;Consultancy&lt;/label&gt; 
      &lt;/fieldset&gt; 
  &lt;/div&gt;
  
  &lt;h3&gt;A Brief Description&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
      &lt;textarea name="Message"&gt;&lt;/textarea&gt;
  &lt;/div&gt;
  
  &lt;h3&gt;What do you do?&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
      &lt;fieldset&gt;
          &lt;div class="skillrow"&gt;
              &lt;input type="checkbox" id="skill1" name="skills[]" value="Website Design"&gt;
                 &lt;label for="skill1"&gt;Website Design&lt;/label&gt;
              &lt;input type="checkbox" id="skill2" name="skills[]" value="Social Networking"&gt;
                 &lt;label for="skill2"&gt;Social Networking&lt;/label&gt;
              &lt;input type="checkbox" id="skill3" name="skills[]" value="E-Commerce"&gt;
                 &lt;label for="skill3"&gt;E-Commerce&lt;/label&gt;
          &lt;/div&gt;
  
          &lt;div class="skillrow"&gt;
              &lt;input type="checkbox" id="skill4" name="skills[]" value="Content Management Systems"&gt;
                 &lt;label for="skill4"&gt;Content Management Systems&lt;/label&gt;
              &lt;input type="checkbox" id="skill5" name="skills[]" value="Video"&gt;
                 &lt;label for="skill5"&gt;Video&lt;/label&gt;
              &lt;input type="checkbox" id="skill6" name="skills[]" value="Images"&gt;
                 &lt;label for="skill6"&gt;Images&lt;/label&gt;
          &lt;/div&gt;
  
          &lt;div class="skillrow"&gt;
              &lt;input type="checkbox" id="skill7" name="skills[]" value="Social Media"&gt;
                 &lt;label for="skill7"&gt;Social Media&lt;/label&gt;
              &lt;input type="checkbox" id="skill8" name="skills[]" value="Strategy"&gt;
                 &lt;label for="skill8"&gt;Strategy&lt;/label&gt;
              &lt;input type="checkbox" id="skill9" name="skills[]" value="Best Practice"&gt;
                 &lt;label for="skill9"&gt;Best Practice&lt;/label&gt;
          &lt;/div&gt;
  
          &lt;div class="skillrow"&gt;
              &lt;input type="checkbox" id="skill10" name="skills[]" value="Not Sure"&gt;
                 &lt;label for="skill10"&gt;Not Sure&lt;/label&gt; 
          &lt;/div&gt;
      &lt;/fieldset&gt;
  &lt;/div&gt;
  
  &lt;h3&gt;Ideal Start Date&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
      &lt;label class="drops"&gt;
          &lt;select name="StartMonth"&gt;
              &lt;option value=""&gt;---&lt;/option&gt;
              &lt;option value="January"&gt;January&lt;/option&gt;
              &lt;option value="February"&gt;February&lt;/option&gt;
              &lt;option value="March"&gt;March&lt;/option&gt;
              &lt;option value="April"&gt;April&lt;/option&gt;
              &lt;option value="May"&gt;May&lt;/option&gt;
              &lt;option value="June"&gt;June&lt;/option&gt;
              &lt;option value="July"&gt;July&lt;/option&gt;
              &lt;option value="August"&gt;August&lt;/option&gt;
              &lt;option value="September"&gt;September&lt;/option&gt;
              &lt;option value="October"&gt;October&lt;/option&gt;
              &lt;option value="November"&gt;November&lt;/option&gt;
              &lt;option value="December"&gt;December&lt;/option&gt;
          &lt;/select&gt;
      &lt;/label&gt;
      &lt;label class="drops"&gt;
          &lt;select name="StartYear"&gt;
              &lt;option value=""&gt;---&lt;/option&gt;
              &lt;option value="2013"&gt;2013&lt;/option&gt;
              &lt;option value="2014"&gt;2014&lt;/option&gt;
              &lt;option value="2015"&gt;2015&lt;/option&gt;
          &lt;/select&gt;
      &lt;/label&gt;
      &lt;input type="checkbox" id="norush" name="norush[]" value="No Great Rush"&gt;
             &lt;label for="norush"&gt;No Great Rush&lt;/label&gt;
  &lt;/div&gt;
  
  &lt;h3&gt;Ideal End Date&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
      &lt;label class="drops"&gt;
          &lt;select name="EndMonth"&gt;
              &lt;option value=""&gt;---&lt;/option&gt;
              &lt;option value="January"&gt;January&lt;/option&gt;
              &lt;option value="February"&gt;February&lt;/option&gt;
              &lt;option value="March"&gt;March&lt;/option&gt;
              &lt;option value="April"&gt;April&lt;/option&gt;
              &lt;option value="May"&gt;May&lt;/option&gt;
              &lt;option value="June"&gt;June&lt;/option&gt;
              &lt;option value="July"&gt;July&lt;/option&gt;
              &lt;option value="August"&gt;August&lt;/option&gt;
              &lt;option value="September"&gt;September&lt;/option&gt;
              &lt;option value="October"&gt;October&lt;/option&gt;
              &lt;option value="November"&gt;November&lt;/option&gt;
              &lt;option value="December"&gt;December&lt;/option&gt;
          &lt;/select&gt;
      &lt;/label&gt;
      &lt;label class="drops"&gt;
          &lt;select name="EndYear"&gt;
              &lt;option value=""&gt;---&lt;/option&gt;
              &lt;option value="2013"&gt;2013&lt;/option&gt;
              &lt;option value="2014"&gt;2014&lt;/option&gt;
              &lt;option value="2015"&gt;2015&lt;/option&gt;
          &lt;/select&gt;
      &lt;/label&gt;
      &lt;input type="checkbox" id="whenready1" name="whenready[]" value="When It's Ready"&gt;
             &lt;label for="whenready1"&gt;When it's good and ready&lt;/label&gt;
  &lt;/div&gt;
  
  &lt;h3&gt;Estimated Budget&lt;/h3&gt;
  
  &lt;div class="formrow"&gt;
  &lt;input type="radio" id="budget1" name="budget[]" value="Under 800"&gt;
     &lt;label for="budget1"&gt;Under &amp;#163;800&lt;/label&gt;
  &lt;input type="radio" id="budget2" name="budget[]"value="800 to 1600"&gt;
     &lt;label for="budget2"&gt;&amp;#163;800 - &amp;#163;1600&lt;/label&gt;
  &lt;input type="radio" id="budget3" name="budget[]" value="Over 1600"&gt;
     &lt;label for="budget3"&gt;Over &amp;#163;1600&lt;/label&gt; 
  &lt;/div&gt;
  
  &lt;h3&gt;Good to Go?&lt;/h3&gt;
  
  &lt;div class="goodtogo"&gt;
      &lt;input type="submit" name="send" value="Submit Details Now!"&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>