如何在表中获取单选按钮的值 - PHP

时间:2014-06-27 14:25:53

标签: php html

所以我有一个包含30个单选按钮的表单,在一个具有相同名称的组中有3个,因此三个中选定的一个将是使用的那个。我想要的是将该单选按钮的值设置为变量。它工作正常,直到我决定把它放在桌子上。这就是我所拥有的:

<form method="post"><table style="width: 99%">
    <tr>
        <td style="width: 46px">&nbsp;</td>
        <td>Questions:</td>
        <td style="width: 85px">Yes</td>
        <td style="width: 84px">No</td>
        <td style="width: 87px">Don't&nbsp;Know</td>
    </tr>
    <tr>
        <td style="width: 46px">
<span class="auto-style2">
<label id="Q1">
        Q1.</label></span></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
<input type="radio" name="question1" value="5" class="auto-style2"></td>
        <td style="width: 84px">
<input type="radio" name="question1" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question1" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q2.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question2" value="5" class="auto-style2"></td>
        <td style="width: 84px">
<input type="radio" name="question2" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question2" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q3.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question3" value="5" class="auto-style2"></td>
        <td style="width: 84px">
<input type="radio" name="question3" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question3" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q4.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question4" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question4" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio"  name="question4" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q5. </label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question5" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question5" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question5" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q6.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question6" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question6" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question6" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q7.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question7" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question7" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question7" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q8.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question8" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question8" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question8" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1">Q9.</label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question9" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question9" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question9" value="0" checked="true" class="auto-style2"></td>
    </tr>
    <tr>
        <td style="width: 46px" class="auto-style2">
<label id="Q1" style="height: 21px">Q10. </label></td>
        <td>--------------------------------------------</td>
        <td style="width: 85px">
        <input type="radio" name="question10" value="5" class="auto-style2"></td>
        <td style="width: 84px"> 
<input type="radio" name="question10" value="2" class="auto-style2"></td>
        <td style="width: 87px" class="auto-style30">
<input type="radio" name="question10" value="0" checked="true" class="auto-style2"></td>
    </tr>
</table>
</form>
<p>Company name  <span class="error">*</span></p>
<input type="text" name="name" class="auto-style2">
<input type="submit" name="submit" value="Submit" class="auto-style2"><span class="auto-style2"></span>

<?php
$score1 = $_POST['question1'];
$score2 = $_POST['question2'];
$score3 = $_POST['question3'];
$score4 = $_POST['question4'];
$score5 = $_POST['question5'];
$score6 = $_POST['question6'];
$score7 = $_POST['question7'];
$score8 = $_POST['question8'];
$score9 = $_POST['question9'];
$score10 = $_POST['question10'];
$weight1 = 1;
$weight2 = 2;
$weight3 = 3;
$scoretotal1 = $score1*$weight3;
$scoretotal2 = $score2*$weight2;
$scoretotal3 = $score3*$weight2;
$scoretotal4 = $score4*$weight1;
$scoretotal5 = $score5*$weight1;
$scoretotal6 = $score6*$weight3;
$scoretotal7 = $score7*$weight2;
$scoretotal8 = $score8*$weight2;
$scoretotal9 = $score9*$weight1;
$scoretotal10 = $score10*$weight1;
$score = $scoretotal1 + $scoretotal2 + $scoretotal3 + $scoretotal4 + $scoretotal5 + $scoretotal6 + $scoretotal7 + $scoretotal8 + $scoretotal9 + $scoretotal10;
?>

我很清楚我的代码可能存在一些问题,我非常喜欢新手。问题是单选按钮值未设置为这些变量。我通过回应变量证实了这一点。

非常感谢任何帮助

由于

编辑:

轻松修复;只需将我的提交按钮移动到该表单中,当我添加表格标签时,我完全忘记了提交按钮:)

代码已编辑。

1 个答案:

答案 0 :(得分:1)

        <form method="post"><table style="width: 99%">
            <tr>
                <td style="width: 46px">&nbsp;</td>
                <td>Questions:</td>
                <td style="width: 85px">Yes</td>
                <td style="width: 84px">No</td>
                <td style="width: 87px">Don't&nbsp;Know</td>
            </tr>
            <tr>
                <td style="width: 46px">
        <span class="auto-style2">
        <label id="Q1">
                Q1.</label></span></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
        <input type="radio" name="question1" value="5" class="auto-style2"></td>
                <td style="width: 84px">
        <input type="radio" name="question1" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question1" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q2.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question2" value="5" class="auto-style2"></td>
                <td style="width: 84px">
        <input type="radio" name="question2" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question2" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q3.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question3" value="5" class="auto-style2"></td>
                <td style="width: 84px">
        <input type="radio" name="question3" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question3" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q4.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question4" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question4" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio"  name="question4" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q5. </label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question5" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question5" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question5" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q6.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question6" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question6" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question6" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q7.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question7" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question7" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question7" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q8.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question8" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question8" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question8" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1">Q9.</label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question9" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question9" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question9" value="0" checked="true" class="auto-style2"></td>
            </tr>
            <tr>
                <td style="width: 46px" class="auto-style2">
        <label id="Q1" style="height: 21px">Q10. </label></td>
                <td>--------------------------------------------</td>
                <td style="width: 85px">
                <input type="radio" name="question10" value="5" class="auto-style2"></td>
                <td style="width: 84px"> 
        <input type="radio" name="question10" value="2" class="auto-style2"></td>
                <td style="width: 87px" class="auto-style30">
        <input type="radio" name="question10" value="0" checked="true" class="auto-style2"></td>
            </tr>
        <tr><input type="submit" name="submit"></input  ></tr>  
        </table>
        </form>

        <?php
        if(isset($_POST['submit']))
        {
        if(isset($_POST['question1']))
        {
        $score1 = $_POST['question1'];
        echo "<br>".$score1."<br>";
        }

        $score2 = $_POST['question2'];
        $score3 = $_POST['question3'];
        $score4 = $_POST['question4'];
        $score5 = $_POST['question5'];
        $score6 = $_POST['question6'];
        $score7 = $_POST['question7'];
        $score8 = $_POST['question8'];
        $score9 = $_POST['question9'];
        $score10 = $_POST['question10'];
        $weight1 = 1;
        $weight2 = 2;
        $weight3 = 3;
        $scoretotal1 = $score1*$weight3;
        $scoretotal2 = $score2*$weight2;
        $scoretotal3 = $score3*$weight2;
        $scoretotal4 = $score4*$weight1;
        $scoretotal5 = $score5*$weight1;
        $scoretotal6 = $score6*$weight3;
        $scoretotal7 = $score7*$weight2;
        $scoretotal8 = $score8*$weight2;
        $scoretotal9 = $score9*$weight1;
        $scoretotal10 = $score10*$weight1;
        $score = $scoretotal1 + $scoretotal2 + $scoretotal3 + $scoretotal4 + $scoretotal5 + $scoretotal6 + $scoretotal7 + $scoretotal8 + $scoretotal9 + $scoretotal10;
        echo $score;
        }
        ?>

您忘记在表单中添加提交按钮,并且未检查是否单击了提交按钮。以上工作正常。