Как получить значение переключателя в таблице — PHP

#php #html

#php #HTML

Вопрос:

Итак, у меня была форма, которая содержала 30 переключателей, 3 в группе с одинаковыми именами, чтобы выбранная из трех была той, которая использовалась. Я хочу, чтобы значение этого переключателя было установлено в переменную. Он работал нормально, пока я не решил вставить его в таблицу. Вот что у меня есть:

 <form method="post"><table style="width: 99%">
    <tr>
        <td style="width: 46px">amp;nbsp;</td>
        <td>Questions:</td>
        <td style="width: 85px">Yes</td>
        <td style="width: 84px">No</td>
        <td style="width: 87px">Don'tamp;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. У вас нет кнопки отправки

2. в чем именно ваша проблема? Я добавил кнопку отправки, и ваша форма работает нормально.

3. Вздох, я знал, что это будет что-то глупое. У меня была кнопка отправки, но у меня не было ее как части этой формы. Спасибо, ребята

Ответ №1:

         <form method="post"><table style="width: 99%">
            <tr>
                <td style="width: 46px">amp;nbsp;</td>
                <td>Questions:</td>
                <td style="width: 85px">Yes</td>
                <td style="width: 84px">No</td>
                <td style="width: 87px">Don'tamp;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;
        }
        ?>
  

Вы забыли добавить кнопку отправки в свою форму и не проверили, нажата ли кнопка отправки. Вышесказанное работает нормально.