Skip to main content
Known Participant
April 18, 2017
Answered

PHP form reporting empty inputs.

  • April 18, 2017
  • 2 replies
  • 1697 views

I am building a form that will have over 70 radio button choices, I would like to only report the buttons with selections made so I don't have dozens of empty lines in the email when I submit the form. Here is a example of my form, the script, and one example of an output when all radio buttons are not selected:

<snip>

<form action="TEST_mailscript.php" method="post" name="ContactForm" id="ContactForm">

  <table align="center">

    <tbody>

      <tr>

        <td><label for="Subject">Subject:</label>

        <br>

          <input name="Subject" type="text" id="Subject" form="ContactForm" />

        </td>

      </tr>

      <tr>

        <td><label for="Message">Message:</label><br>

          <textarea name="Message" cols="45" rows="5" id="Message" form="ContactForm"></textarea>

        </td>

      </tr>

      <tr>

          <td>Radio Group 1<br>

            <label>

            <input type="radio" name="RadioGroup1" value="R1_Option1" id="RadioGroup1_1" form="ContactForm">

            R1op1</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup1" value="R1_Option2" id="RadioGroup1_2" form="ContactForm">

            R1op2</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup1" value="R1_Option3" id="RadioGroup1_3" form="ContactForm">

           R1op3

          </label>

        </td>

       </tr>

      

       <tr>

       <td> 

      

       </td>

       </tr>

      

       <tr>

          <td>Radio Group 2<br>

            <label>

            <input type="radio" name="RadioGroup2" value="R2_Option1" id="RadioGroup2_1" form="ContactForm">

            R2op1</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup2" value="R2_Option2" id="RadioGroup2_2" form="ContactForm">

            R2op2</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup2" value="R2_Option3" id="RadioGroup2_3" form="ContactForm">

           R2op3

          </label>

        </td>

       </tr>

      

       <tr>

       <td> 

      

       </td>

       </tr>

      

       <tr>

          <td>Radio Group 3<br>

            <label>

            <input type="radio" name="RadioGroup3" value="R3_Option1" id="RadioGroup3_1" form="ContactForm">

            R3op1</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup3" value="R3_Option2" id="RadioGroup3_2" form="ContactForm">

            R3op2</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup3" value="R3_Option3" id="RadioGroup3_3" form="ContactForm">

           R3op3

          </label>

        </td>

       </tr>

      

       <tr>

       <td> 

      

       </td>

       </tr>

      

       <tr>

          <td>Radio Group 4<br>

            <label>

            <input type="radio" name="RadioGroup4" value="R4_Option1" id="RadioGroup4_1" form="ContactForm">

            R4op1</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup4" value="R4_Option2" id="RadioGroup4_2" form="ContactForm">

            R4op2</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup4" value="R4_Option3" id="RadioGroup4_3" form="ContactForm">

           R4op3

          </label>

        </td>

       </tr>

      

       <tr>

       <td> 

      

       </td>

       </tr>

      

       <tr>

          <td>Radio Group 5<br>

            <label>

            <input type="radio" name="RadioGroup5" value="R5_Option1" id="RadioGroup5_1" form="ContactForm">

            R5op1</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup5" value="R5_Option2" id="RadioGroup5_2" form="ContactForm">

            R5op2</label>

          <br>

          <label>

            <input type="radio" name="RadioGroup5" value="R5_Option3" id="RadioGroup5_3" form="ContactForm">

           R5op3

          </label>

        </td>

       </tr>

      

       <tr>

       <td> 

      

       </td>

       </tr>

         

        <td><input name="SubmitButton" type="submit" id="Submit Button" form="ContactForm" value="Send Message" />

        </td>

    

    </tbody>

  </table>

  </form>

</snip>

And here is my send script:

<snip>

<?php

$from="noreply@anymailuser.com";

$email="info@anymailuser.com";

$subject=$_POST['Subject'];

$message=$_POST['']."

Message from sender = ".$_POST['Message']."

".$_POST['<hr>']."

Sender = ".$_POST['Sender']."

Radio Group 1 = ".$_POST['RadioGroup1']."

Radio Group 2 = ".$_POST['RadioGroup2']."

Radio Group 3 = ".$_POST['RadioGroup3']."

Radio Group 4 = ".$_POST['RadioGroup4']."

Radio Group 5 = ".$_POST['RadioGroup5'];

mail ( $email, $subject, $message, "From:".$from );

?>

</snip>

Right now my script would put out an input like this if I only selected a radio group 5 option:

<snip>

Message from sender = Testing for empty fields.

Sender = 

Radio Group 1 =

Radio Group 2 =

Radio Group 3 =

Radio Group 4 =

Radio Group 5 = R5_Option1

</snip>

I would like for the email to only report radio group 5 in this instance. Thanks again, Scott

This topic has been closed for replies.
Correct answer bregent

A loop is a great idea, especially for a form with that many fields. But why not just loop through the post array - then it doesn't matter what the group names are and you can apply more meaningful naming conventions to the form elements.

$FormArray = filter_input_array(INPUT_POST);

ForEach ($FormArray as $key =>$element) {

    $message .= $key .": " .$element . "\r\n";

   

}

2 replies

Legend
April 18, 2017

If you are going to stick to a naming convention like RadioGroup1, RadioGroup2, RadioGroup3 etc then you could make the php simple for yourself. Rather than typing that all out you could just loop through however many RadioGroups you need. (Although I can't help thinking this is just a test and you will be using more descriptive names in production).

<?php

if(isset($_POST['SubmitButton'])) {

$from="noreply@anymailuser.com";

$email="info@anymailuser.com";

$subject = $_POST['Subject'];

$message = $_POST['Message']. "\r\n";

?>

<?php for($i = 1; $i <= 5; $i++) {

if (!empty($_POST['RadioGroup'.$i])){

$message .= "Radio Group $i : ".$_POST['RadioGroup'.$i]. "\r\n";

}

}

mail ($email, $subject, $message, "From:".$from );

}

?>

bregentCorrect answer
Participating Frequently
April 18, 2017

A loop is a great idea, especially for a form with that many fields. But why not just loop through the post array - then it doesn't matter what the group names are and you can apply more meaningful naming conventions to the form elements.

$FormArray = filter_input_array(INPUT_POST);

ForEach ($FormArray as $key =>$element) {

    $message .= $key .": " .$element . "\r\n";

   

}

Rob Hecker2
Legend
April 18, 2017

if (!empty($_POST['RadioGroup1'])){

$message .= "Radio Group1 ".trim(strip_tags($_POST['RadioGroup1']));

}

if (!empty($_POST['RadioGroup2'])){

$message .= "Radio Group2 ".trim(strip_tags($_POST['RadioGroup2']));

}

. . .and so on. You put this in the post processing part of the script, not the form part

Note that when you use dot equal instead of just equal, the new string is added to the existing variable

Known Participant
April 19, 2017

Thanks Rob, you have been a big help through this and thanks to the others too. I just got in from a double double so I will be looking at these options after a bit of sleep. Will test and respond to the suggestions and report back asap. Scott