Hi there,

I'm stuck on some PHP coding and can't get my form data to work properly.

Can some kind person tell me what I'm doing wrong? Here's the markup:

<form method="post" action="social_m_prices-mailer.php">
  <div>Name:<span class="ss-required-asterisk">*</span><br>
  <input name="name" size="45" type="text"><br>
  <br>
Email:<span class="ss-required-asterisk">*</span><br>
  <input name="email" size="45" type="text"><br>

  <br>
  <input name="Social-media" value="Facebook" type="checkbox"> Facebook &nbsp; <input name="Social-media" value="Linked_In" type="checkbox"> Linked In &nbsp; <input name="Social-media" value="Twitter" type="checkbox">&nbsp;
Twitter &nbsp;&nbsp; <input name="Social-media" value="Qype" type="checkbox">&nbsp; Qype &nbsp;&nbsp; <input name="Social-media" value="Meengle" type="checkbox">Meengle<br><br>

<input name="Social-mediaSubmit" value="Submit" type="submit">
  <div style="text-align: center;"><big style="font-weight: bold;"><br>
&pound;35
each or &pound;150 for all five.</big><br>
  </div>
  <br>
Would you like to have a BT Tradespace account?<br>
  <br>
  <input name="BT-Tradespace[]" value="Bronze" type="radio">Bronze <input name="BT-Tradespace[]" value="Silver" type="radio">Silver <input name="BT-Tradespace[]" value="Gold" type="radio">Gold <input name="BT-TradespaceSubmit" value="Submit" type="submit"> </div>

  <br>
  <br>
  <br>
Comments:<span class="ss-required-asterisk">*</span><br>
  <br>
  <textarea rows="14" name="message" cols="45"> </textarea> <br>
  <br>

  <input value="Submit" name="submit" type="submit"></form>

And here's the code:

<?php function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}

$Facebook = 'unchecked';
$Linked_In = 'unchecked';
$Twitter = 'unchecked';
$Qype = 'unchecked';
$Meengle = 'unchecked';

if (isset($_POST['Social-mediaSubmit'])) {

if (isset($_POST['Facebook'])) {
$Facebook = $_POST['Facebook'];

if ($Facebook = = 'Facebook') {
$Facebook = 'checked';
}
}

if (isset($_POST['Linked_In'])) {
$Linked_In = $_POST['Linked In'];

if ($Linked_In = = 'Linked_In') {
$Linked_In = 'checked';
}
}

if (isset($_POST['Twitter'])) {
$Twitter = $_POST['Twitter'];

if ($Twitter = = 'Twitter') {
$Twitter = 'checked';
}
}

if (isset($_POST['Qype'])) {
$Qype = $_POST['Qype'];

if ($Qype = = 'Qype') {
$Qype = 'checked';
}
}

if (isset($_POST['Meengle'])) {
$Meengle = $_POST['Meengle'];

if ($Meengle = = 'Meengle') {
$Meengle = 'checked';
}
}
}


$Bronze = 'unchecked';
$Silver = 'unchecked';
$Gold = 'unchecked';

if (isset($_POST['BT-TradespaceSubmit'])) {

$selected_radio = $_POST['BT-Tradespace'];

if ($selected_radio = = 'Bronze') {
$Bronze = 'checked';
}
else if ($selected_radio = = 'Silver') {
$Silver = 'checked';
}
else if ($selected_radio = = 'Gold') {
$Gold = 'checked';
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed

//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("info@example.co.uk", "Subject: $subject",
$message, "From: $email" );
echo "Thanks. I'll get back to you within twenty four hours.";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='social_m_prices-mailer.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}

?>

Sorry for being such a n00b, but with your help I'll soon figure it out. Thanks in advance.

    Try changing "From: $email" to "From: $email".PHP_EOL it's worked for a few others... Also most mail services don't like spoofed emails. You may think about doing From: a valid internal email, and Reply to: $email IE

    $email = $_REQUEST['email'] ; 
    $subject = $_REQUEST['subject'] ; 
    $message = $_REQUEST['message'] ; 
    $to = "info@example.co.uk";
    $header = "From: info@example.co.uk" . PHP_EOL;
    $header .= "Reply to: $email" . PHP_EOL;
    if( mail($to, "Subject: $subject", $message, $header ) ) {
       echo "Message sent successfully";
    } else {
       echo "Message failed to send";
    }
    

    Also notice the error checking for the mail function, to make sure it actually sent successfully as far as the script is concerned.

      Thanks, Derokorian, trying it now. I'll let you know how I get on. I really appreciate this. It's been driving me nuts!

        Right... here's the problem: the mailer works fine. I'm having trouble linking the checkboxes and radio buttons to it so the choices made register and appear in the mailer.php I receive.

        I want a script that records which checkboxes and radio buttons are being selected and sends me the results. I've given you the code I wrote. It doesn't work because I've done something wrong in there.

        What have I done wrong?

          Oh I guess I didn't understand the problem. Looks like you are already processing the radios/checkboxes, you just need to add those values into the message.

          <?php
          function spamcheck($field) { 
             //filter_var() sanitizes the e-mail 
             //address using FILTER_SANITIZE_EMAIL 
             $field=filter_var($field, FILTER_SANITIZE_EMAIL); 
          
             //filter_var() validates the e-mail 
             //address using FILTER_VALIDATE_EMAIL 
             if(filter_var($field, FILTER_VALIDATE_EMAIL)) { 
                return TRUE; 
             } else { 
                return FALSE; 
             } 
          } 
          
          $social_media = '';
          
          if (isset($_POST['Social-mediaSubmit'])) { 
          
             if (isset($_POST['Facebook'])) { 
                if ($_POST['Facebook'] == 'Facebook') { 
                   $social_media .= "Facebook: checked\n"; 
                } else {
                   $social_media .= "Facebook: unchecked\n";
                }
             } 
          
             if (isset($_POST['Linked_In'])) { 
                if ($_POST['Linked_In'] == 'Linked_In') { 
                   $social_media .= "Linked In: checked\n"; 
                } else {
                   $social_media .= "Linked In: unchecked\n";
                }
             } 
          
             if (isset($_POST['Twitter'])) { 
                if ($_POST['Twitter'] == 'Twitter') {
                   $social_media .= "Twitter: checked\n";
                } else {
                   $social_media .= "Twitter: unchecked\n";
                }
             } 
          
             if (isset($_POST['Qype'])) {       
          if ($_POST['Qype'] == 'Qype') { $social_media .= "Qype: checked\n"; } else { $social_media .= "Qype: unchecked\n"; } } if (isset($_POST['Meengle'])) { if ($_POST['Meengle'] == 'Meengle') { $social_media .= "Meengle: checked\n"; } else { $social_media .= "Meengle: unckeched\n"; } } } $bt_tradespace = 'BT-Tradespace: '; if (isset($_POST['BT-TradespaceSubmit'])) { if ($_POST['BT-TradespaceSubmit'] == 'Bronze') { $bt_tradespace .= "Bronze\n"; } else if ($_POST['BT-TradespaceSubmit'] == 'Silver') { $bt_tradespace .= "Silver\n"; } else if ($_POST['BT-TradespaceSubmit'] == 'Gold') { $bt_tradespace .= "Gold\n"; } } if (isset($_REQUEST['email'])) { //if "email" is filled out, proceed //check if the email address is invalid $mailcheck = spamcheck($_REQUEST['email']); if ($mailcheck==FALSE) { echo "Invalid input"; } else { //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] . $social_media .$bt_tradespace; $to = "info@example.co.uk"; $header = "From: info@example.co.uk" . PHP_EOL; $header .= "Reply to: $email" . PHP_EOL; if( mail($to, "Subject: $subject", $message, $header ) ) { echo "Message sent successfully. "; echo "Thanks. I'll get back to you within twenty four hours."; } else { echo "Message failed to send"; } } } else { //if "email" is not filled out, display the form echo "<form method='post' action='social_m_prices-mailer.php'> Email: <input name='email' type='text' /><br /> Subject: <input name='subject' type='text' /><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea><br /> <input type='submit' /> </form>"; }

            Ah, thank you. Looking good so far: I didn't get a blank white page with nothing on it this time, which is always what happens to me when I mess up the code.

            I had to remember to put this guy at the end of the code:

            ?>

            because I get a blank white page if I don't.

            Thanks, I think this is working. I've sent myself a test email to make sure. I'll let you know what happens.

              Okay, I ticked a few check boxes and one radio button, and this is what happened:

              Gimme.BT-Tradespace:

              "Gimme" was the message I put in the text box because I had to put something in there, it's compulsory.

              The check boxes haven't registered.

              The radio buttons have recorded that something was clicked, but not which one.

              We're getting there... just another tweak or two and we'll have nailed it. Thanks, I really do appreciate this.

                Well here's comething I see as a problem social_media in the form is for a single item, but its used multiple times, and facebook is the value (for example). YOu might want to do

                <input type="checkbox" name="Facebook" value="Facebook" /> Facebook
                

                etc. Then look at them in the processor as is, or change the checkbox name to SocialMedia[] (creating an array) for all the check boxes. Then do:

                $Facebook = FALSE;
                $Qype = FALSE; 
                //.. etc.,
                foreach( $_POST['SocialMedia'] as $choice ) {
                      if ($choice == 'Facebook') { 
                         $Facebook = TRUE;
                      } elseif ($choice == 'Qype') { 
                         $Qype = TRUE; 
                      } elseif
                //... etc etc
                }
                      if ($Facebook == TRUE) { 
                         $social_media .= "Facebook: checked\n"; 
                      } else {
                         $social_media .= "Facebook: unchecked\n";
                      }
                

                  I think I got something wrong again; I've got a blank white page.

                  <?php
                  function spamcheck($field) {
                     //filter_var() sanitizes the e-mail
                     //address using FILTER_SANITIZE_EMAIL
                     $field=filter_var($field, FILTER_SANITIZE_EMAIL);
                  
                     //filter_var() validates the e-mail
                     //address using FILTER_VALIDATE_EMAIL
                     if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
                        return TRUE;
                     } else {
                        return FALSE;
                     }
                  }
                  
                  $social_media = '';
                  
                  foreach( $_POST['SocialMedia'] as $choice ) {
                        if ($choice == 'Facebook') {
                           $SocialMedia .= "Facebook: checked\n";
                        } else {
                           $SocialMedia .= "Facebook: unchecked\n";
                        }
                     if ($choice == 'Linked_In') {
                           $SocialMedia .= "Linked_In: checked\n";
                        } else {
                           $SocialMedia .= "Linked_In: unchecked\n";
                        }
                     if ($choice == 'Twitter') {
                           $SocialMedia .= "Twitter: checked\n";
                        } else {
                           $SocialMedia .= "Twitter: unchecked\n";
                        }
                        if ($choice == 'Qype') {
                           $SocialMedia .= "Qype: checked\n";
                        } else {
                           $SocialMedia .= "Qype: unchecked\n";
                        }
                     if ($choice == 'Meengle') {
                           $SocialMedia .= "Meengle: checked\n";
                        } else {
                           $SocialMedia .= "Meengle: unchecked\n";
                        }
                  }
                  
                  $bt_tradespace = '';
                  
                  foreach( $_POST['BT-Tradespace'] as $choice ) {
                        if ($choice == 'Gold') {
                           $BT-Tradespace .= "Gold: checked\n";
                        } else {
                           $BT-Tradespace .= "Gold: unchecked\n";
                        }
                     if ($choice == 'Silver') {
                           $BT-Tradespace .= "Silver: checked\n";
                        } else {
                           $BT-Tradespace .= "Silver: unchecked\n";
                        }
                     if ($choice == 'Bronze') {
                           $BT-Tradespace .= "Bronze: checked\n";
                        } else {
                           $BT-Tradespace .= "Bronze: unchecked\n";
                        }
                  }
                  
                  
                  if (isset($_REQUEST['email']))  {
                     //if "email" is filled out, proceed
                  
                     //check if the email address is invalid
                     $mailcheck = spamcheck($_REQUEST['email']);
                     if ($mailcheck==FALSE) {
                        echo "Oops! There has been a mistake. Please try again.";
                     } else {
                        //send email
                        $email = $_REQUEST['email'] ;
                        $subject = $_REQUEST['subject'] ;
                        $message = $_REQUEST['message'] . $social_media .$bt_tradespace;
                        $to = "info@wendy****croftwebdesign.com";
                        $header = "From: info@wendy****croftwebdesign.com" . PHP_EOL;
                        $header .= "Reply to: $email" . PHP_EOL;
                        if( mail($to, "Subject: $subject", $message, $header ) ) {
                           echo "Message sent successfully. ";
                           echo "Thanks. I'll get back to you within twenty four hours.";
                        } else {
                           echo "Sorry, something has gone wrong here. Please try again.";
                        }
                     }
                  } else {
                     //if "email" is not filled out, display the form
                     echo "<form method='post' action='social_m_prices-mailer.php'>
                     Email: <input name='email' type='text' /><br />
                     Subject: <input name='subject' type='text' /><br />
                     Message:<br />
                     <textarea name='message' rows='15' cols='40'>
                     </textarea><br />
                     <input type='submit' />
                  </form>";
                  }
                  ?>
                  
                  

                    Try These -
                    Form:

                    
                    <form method="post" action="social_m_prices-mailer.php">
                      <div>Name:<span class="ss-required-asterisk">*</span><br>
                      <input name="name" size="45" type="text"><br>
                      <br>
                    	Email:<span class="ss-required-asterisk">*</span><br>
                      <input name="email" size="45" type="text"><br>
                    
                      <br>
                      <input name="Facebook" value="Facebook" type="checkbox"> Facebook &nbsp;
                      <input name="Linked_In" value="Linked_In" type="checkbox"> Linked In &nbsp;
                      <input name="Twitter" value="Twitter" type="checkbox"> Twitter &nbsp;
                      <input name="Qype" value="Qype" type="checkbox"> Qype &nbsp;
                      <input name="Meengle" value="Meengle" type="checkbox"> Meengle<br><br>	  
                    <div style="text-align: center;"><big style="font-weight: bold;"><br> &pound;35 each or &pound;150 for all five.</big><br> </div> <br> Would you like to have a BT Tradespace account?<br> <br> <input name="BT-Tradespace" value="Bronze" type="radio">Bronze <input name="BT-Tradespace" value="Silver" type="radio">Silver <input name="BT-Tradespace" value="Gold" type="radio">Gold </div> <br> <br> <br> Comments:<span class="ss-required-asterisk">*</span><br> <br> <textarea rows="14" name="message" cols="45"> </textarea> <br> <br> <input value="Submit" name="submit" type="submit"></form>

                    Mailer.php:

                    <?php
                    function spamcheck($field) { 
                       //filter_var() sanitizes the e-mail 
                       //address using FILTER_SANITIZE_EMAIL 
                       $field=filter_var($field, FILTER_SANITIZE_EMAIL); 
                    
                       //filter_var() validates the e-mail 
                       //address using FILTER_VALIDATE_EMAIL 
                       if(filter_var($field, FILTER_VALIDATE_EMAIL)) { 
                          return TRUE; 
                       } else { 
                          return FALSE; 
                       } 
                    } 
                    
                    $social_media = '';
                    if (isset($_POST['Facebook']) &&$_POST['Facebook'] == 'Facebook') { 
                    	$social_media .= "Facebook: checked\n"; 
                    } else {
                    	$social_media .= "Facebook: unchecked\n";
                    }
                    if (isset($_POST['Linked_In']) && $_POST['Linked_In'] == 'Linked_In') { 
                    	$social_media .= "Linked In: checked\n"; 
                    } else {
                    	$social_media .= "Linked In: unchecked\n";
                    }
                    if (isset($_POST['Twitter']) && $_POST['Twitter'] == 'Twitter') {
                    	$social_media .= "Twitter: checked\n";
                    } else {
                    	$social_media .= "Twitter: unchecked\n";
                    }
                    if (isset($_POST['Qype']) && $_POST['Qype'] == 'Qype') {
                    	$social_media .= "Qype: checked\n";
                    } else {
                    	$social_media .= "Qype: unchecked\n";
                    }
                    if (isset($_POST['Meengle']) && $_POST['Meengle'] == 'Meengle') {
                    	$social_media .= "Meengle: checked\n";
                    } else {
                    	$social_media .= "Meengle: unckeched\n";
                    }
                    
                    $bt_tradespace = 'BT-Tradespace: ';
                    if (isset($_POST['BT-TradespaceSubmit'])) { 
                       if ($_POST['BT-TradespaceSubmit'] == 'Bronze') {
                          $bt_tradespace .= "Bronze\n";
                       } else if ($_POST['BT-TradespaceSubmit'] == 'Silver') {
                          $bt_tradespace .= "Silver\n";
                       } else if ($_POST['BT-TradespaceSubmit'] == 'Gold') {
                          $bt_tradespace .= "Gold\n";
                       }
                    } else {
                    	$bt_tradespace .= "Not Answered\n";
                    }
                    
                    if (isset($_REQUEST['email']))  {
                       //if "email" is filled out, proceed 
                    
                       //check if the email address is invalid 
                       $mailcheck = spamcheck($_REQUEST['email']); 
                       if ($mailcheck==FALSE) { 
                          echo "Invalid input"; 
                       } else {
                          //send email
                          $email = $_REQUEST['email'] ; 
                          $subject = $_REQUEST['subject'] ; 
                          $message = $_REQUEST['message'] . $social_media .$bt_tradespace; 
                          $to = "info@example.co.uk"; 
                          $header = "From: info@example.co.uk" . PHP_EOL; 
                          $header .= "Reply to: $email" . PHP_EOL; 
                          if( mail($to, "Subject: $subject", $message, $header ) ) { 
                             echo "Message sent successfully. "; 
                             echo "Thanks. I'll get back to you within twenty four hours."; 
                          } else { 
                             echo "Message failed to send"; 
                          } 
                       } 
                    } else {
                       //if "email" is not filled out, display the form 
                       echo "<form method='post' action='social_m_prices-mailer.php'> 
                       Email: <input name='email' type='text' /><br /> 
                       Subject: <input name='subject' type='text' /><br /> 
                       Message:<br /> 
                       <textarea name='message' rows='15' cols='40'> 
                       </textarea><br /> 
                       <input type='submit' /> 
                       </form>"; 
                    }
                    
                    ?>
                    

                      Okay, this is way better. I got a success message and the test email came back to me thus:

                      Want!Facebook: checked
                      Linked In: unchecked
                      Twitter: unchecked
                      Qype: unchecked
                      Meengle: unckeched
                      BT-Tradespace: Not Answered

                      The message I put in the text box was "Want!", and it hasn't been separated from the rest of the information. The radio buttons are recording no results even though I definitely selected one.

                      This is a much better result, though. Thanks for all your help.

                        Ok So we're almost there! Next let's change this line

                        $message = $_REQUEST['message'] . $social_media .$bt_tradespace; 
                        
                        // To:
                        
                        $message = $_REQUEST['message'] . "\n\n" . $social_media . $bt_tradespace;
                        

                        I'll try to help with the radio problem later tonight, gotta get ready for my cookout tho!

                        Back at midnight!

                          Actually change this section

                          $bt_tradespace = 'BT-Tradespace: '; 
                          if (isset($_POST['BT-TradespaceSubmit'])) { 
                             if ($_POST['BT-TradespaceSubmit'] == 'Bronze') { 
                                $bt_tradespace .= "Bronze\n"; 
                             } else if ($_POST['BT-TradespaceSubmit'] == 'Silver') { 
                                $bt_tradespace .= "Silver\n"; 
                             } else if ($_POST['BT-TradespaceSubmit'] == 'Gold') { 
                                $bt_tradespace .= "Gold\n"; 
                             } 
                          } else { 
                              $bt_tradespace .= "Not Answered\n"; 
                          } 
                          

                          To:

                          $bt_tradespace = 'BT-Tradespace: '; 
                          if (isset($_POST['BT-Tradespace'])) { 
                             if ($_POST['BT-Tradespace'] == 'Bronze') { 
                                $bt_tradespace .= "Bronze\n"; 
                             } else if ($_POST['BT-Tradespace'] == 'Silver') { 
                                $bt_tradespace .= "Silver\n"; 
                             } else if ($_POST['BT-Tradespace'] == 'Gold') { 
                                $bt_tradespace .= "Gold\n"; 
                             } 
                          } else { 
                              $bt_tradespace .= "Not Answered\n"; 
                          } 
                          

                          See if that works and let me know... BBL

                            Result! 🙂

                            Now I just need to know how to get a space between the message and the choices, and we're done. Thank you so much for this!

                              did you already try my suggestion in post #13? If so can you post the outcome of that?

                                I added this code

                                $message = $_REQUEST['message'] . "\n\n" . $social_media . $bt_tradespace; 
                                

                                and received a success message, then got this in the email:

                                I just want those.Facebook: checked
                                Linked In: checked
                                Twitter: checked
                                Qype: unchecked
                                Meengle: unchecked
                                BT-Tradespace: Silver

                                so it hasn't put any spaces in. I can live with it as it is because you've got me much further than my own efforts have, but wouldn't it be brilliant if we could get the spaces to work?

                                  $message = '
                                  Name: '. $_REQUEST['name'] .'
                                  Email: '. $_REQUEST['email'] .'
                                  Message: '. $_REQUEST['message'] .'
                                  
                                  Social Media:
                                  '. $social_media .'
                                  
                                  '. $bt_tradespace .'
                                  
                                  Contact received on: '. date('m/d/y h:i a');
                                  

                                  Give that a shot.

                                    Nope: there are still no spaces between paragraphs. Trying it with an \n.

                                      Didn't work. There's got to be a way to do this.