Dan152
Dan152

Reputation: 3

Php contact form with multiable radio buttons

I'm quite new to php and I am trying to code a contact form/questionnaire

I need the form to pull through the results of the radio button for example

EmployeeName: Ian Matthews Mark: ( what ever radio button they click ) Comments: What ever comments they leave in the text box.

but if other names are filled in, then create a new line with the above details

EmployeeName: Ian Matthews Mark: ( what ever radio button they click ) Comments: What ever comments they leave in the text box.

EmployeeName: Ciara Maguire Mark: ( what ever radio button they click ) Comments: What ever comments they leave in the text box.

I am trying to use "foreach" but don't think I am using it correctly

Hope someone can help

Thanks

currently I have this code

<?php

$EmailFrom = "";
$EmailTo = "";
$Subject = "Questionnaire";

$EmpName = Trim(stripslashes($_POST['EmployeeName']));
$Radio = Trim(stripslashes($_POST['Radio']));
$Comment = Trim(stripslashes($_POST['Com']));


foreach ($_POST ['Radio'] as $value) {
    $Body = "$EmpName, $Radio, $Comment\n";
  }


$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


if ($success){
  print "top work";
}
else{
  print "you have a error";
}

?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="10" colspan="7"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <h4>Excellent</h4>
    </td>
    <td>
      <h4>Very Good</h4>
    </td>
    <td>
      <h4>Good</h4>
    </td>
    <td>
      <h4>Satisfactory</h4>
    </td>
    <td>
      <h4>Poor</h4>
    </td>
    <td>
      <h4>N/A</h4>
    </td>
  </tr>
  <tr>
    <td height="30">
      <label>Ian Matthews
        <input type="hidden" name="EmployeeName" value="Ian Matthews" />
      </label>
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="10-Excellent" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="7-VeryGood" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="4-Good" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="0-Satisfactory" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="-5-Poor" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="N/A" />
    </td>
  </tr>
  <tr>
    <td height="30">
      <label class="colourGrey">Comments:</label>
    </td>
    <td colspan="6" align="center">
      <input class="teamCom" type="text" name="Com" />
    </td>
  </tr>
  <tr>
    <td height="10" colspan="7"></td>
  </tr>
  <tr>
    <td colspan="7" class="BorderLine"></td>
  </tr>
  <tr>
    <td height="10" colspan="7"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <h4>Excellent</h4>
    </td>
    <td>
      <h4>Very Good</h4>
    </td>
    <td>
      <h4>Good</h4>
    </td>
    <td>
      <h4>Satisfactory</h4>
    </td>
    <td>
      <h4>Poor</h4>
    </td>
    <td>
      <h4>N/A</h4>
    </td>
  </tr>
  <tr>
    <td height="30">
      <label>Ciara Maguire
        <input type="hidden" name="EmployeeName" value="Ciara Maguire" />
      </label>
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="10-Excellent" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="7-VeryGood" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="4-Good" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="0-Satisfactory" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="-5-Poor" />
    </td>
    <td align="center">
      <input type="radio" name="Radio" value="N/A" />
    </td>
  </tr>
  <tr>
    <td height="30">
      <label class="colourGrey">Comments:</label>
    </td>
    <td colspan="6" align="center">
      <input class="teamCom" type="text" name="Com" />
    </td>
  </tr>
  <tr>
    <td height="10" colspan="7"></td>
  </tr>
  <tr>
    <td colspan="7" class="BorderLine"></td>
  </tr>
  
  </table>

Upvotes: 0

Views: 400

Answers (3)

T.Shah
T.Shah

Reputation: 2768

I modified your code like this -

        <form method="post" >

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="10" colspan="7"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
          <h4>Excellent</h4>
        </td>
        <td>
          <h4>Very Good</h4>
        </td>
        <td>
          <h4>Good</h4>
        </td>
        <td>
          <h4>Satisfactory</h4>
        </td>
        <td>
          <h4>Poor</h4>
        </td>
        <td>
          <h4>N/A</h4>
        </td>
      </tr>
      <tr>
        <td height="30">
          <label>Ian Matthews
            <input type="hidden" name="EmployeeName1" value="Ian Matthews" />
          </label>
        </td>
        <td align="center">
          <input type="radio" name="Radio1" value="10-Excellent" />
        </td>
        <td align="center">
          <input type="radio" name="Radio1" value="7-VeryGood" />
        </td>
        <td align="center">
          <input type="radio" name="Radio1" value="4-Good" />
        </td>
        <td align="center">
          <input type="radio" name="Radio1" value="0-Satisfactory" />
        </td>
        <td align="center">
          <input type="radio" name="Radio1" value="-5-Poor" />
        </td>
        <td align="center">
          <input type="radio" name="Radio1" value="N/A" />
        </td>
      </tr>
      <tr>
        <td height="30">
          <label class="colourGrey">Comments:</label>
        </td>
        <td colspan="6" align="center">
          <input class="teamCom" type="text" name="Com1" />
        </td>
      </tr>
      <tr>
        <td height="10" colspan="7"></td>
      </tr>
      <tr>
        <td colspan="7" class="BorderLine"></td>
      </tr>
      <tr>
        <td height="10" colspan="7"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
          <h4>Excellent</h4>
        </td>
        <td>
          <h4>Very Good</h4>
        </td>
        <td>
          <h4>Good</h4>
        </td>
        <td>
          <h4>Satisfactory</h4>
        </td>
        <td>
          <h4>Poor</h4>
        </td>
        <td>
          <h4>N/A</h4>
        </td>
      </tr>
      <tr>
        <td height="30">
          <label>Ciara Maguire
            <input type="hidden" name="EmployeeName2" value="Ciara Maguire" />
          </label>
        </td>
        <td align="center">
          <input type="radio" name="Radio2" value="10-Excellent" />
        </td>
        <td align="center">
          <input type="radio" name="Radio2" value="7-VeryGood" />
        </td>
        <td align="center">
          <input type="radio" name="Radio2" value="4-Good" />
        </td>
        <td align="center">
          <input type="radio" name="Radio2" value="0-Satisfactory" />
        </td>
        <td align="center">
          <input type="radio" name="Radio2" value="-5-Poor" />
        </td>
        <td align="center">
          <input type="radio" name="Radio2" value="N/A" />
        </td>
      </tr>
      <tr>
        <td height="30">
          <label class="colourGrey">Comments:</label>
        </td>
        <td colspan="6" align="center">
          <input class="teamCom" type="text" name="Com2" />
        </td>
      </tr>
      <tr>
        <td height="10" colspan="7"></td>
      </tr>
      <tr>
        <td colspan="7" class="BorderLine"></td>
      </tr>

      </table>
                <input type="submit" value="Send Feedback" />

            </form>


     <?php

     if(isset($_POST)) {

    $EmailFrom = "";
    $EmailTo = "";
    $Subject = "Questionnaire";

    $Body = "";

    $noParaPerEmployee = 3;
    $n = 0;

    foreach ($_POST as $key=> $value) {
        //echo "$key => $value <br/>";
        //$Body .= "$EmpName, $Radio, $Comment\n";
        $Body .= "$value , ";
        $n++;
        if($n===3) {
            $Body = substr($Body,0, -2); // get rid of last comma..
            $Body .= "\n";
            $n = 0;
        }

      }

    //echo "$Body <br/>\n";
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


    if ($success){
      print "top work";
    }
    else{
      print "you have a error";
    }

     }

    ?>

Upvotes: 0

Obsidian Age
Obsidian Age

Reputation: 42354

$_POST['Radio'] contains a single value, whereas you want to send through multiple values. For that, the best way would be to assign the inputs to an array, as such:

<input name="Radio[]" value="Lorem" />
<input name="Radio[]" value="ipsum" />
<input name="Radio[]" value="dolor" />
<input name="Radio[]" value="sit" />
<input name="Radio[]" value="amet" />

Then you can retrieve the results by accessing them as an array:

$_POST['Radio'][0] == 'Lorem'
$_POST['Radio'][4] == 'amet'

Finally, you are overwriting the assignment to $Body each time in your loop. Instead of $Body = "$EmpName, $Radio, $Comment\n";, you'll want $Body .= "$EmpName, $Radio, $Comment\n";. Note the dot before the equals. That appends to a variable rather than overwrites it.

Assuming that you only have one employee submitting one comment in addition to the many radio buttons responses per POST, you could then use something like:

foreach ($_POST ['Radio'] as $value) {
    $Body .= "$EmpName, $value, $Comment\n";
}

Hope this helps!

Upvotes: 0

Alexi
Alexi

Reputation: 391

First at all, you are using the same input names for both employees. You have to set different names:

<input type="hidden" name="EmployeeName1" value="Ian Matthews" />
<input type="hidden" name="EmployeeName2" value="Ciara Maguire" />

And this with all inputs (Com1 and Com2, radio1 and radio2,..)

In your php file, you have to do something like

$Body = "";
$numEmployees = 2; // or the number you put
// Each iteration per employee, accesing to his form data
for ($i=1; $i<=$numEmployees; $i++)
{
  $EmpName = Trim(stripslashes($_POST['EmployeeName' .$i]));
  $Comment = Trim(stripslashes($_POST['Com' .$i]));
  $Radio = Trim(stripslashes($_POST['radio' .$i]));
  $Body .= "$EmpName, $Radio, $Comment\n";  // Note the dot in ".="
}  

and then

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

Of course, you have emailto and emailfrom empty, but I supose that you fill them in your final version. I don't execute this version, but I hope can help you to understand your mistake.

Upvotes: 2

Related Questions