在位于<form>标签内的php函数中使用<input>标签是否可行?

3

我很新于php和HTML,最近几天一直在尝试理解它们。我正在尝试创建一个包含56个单选按钮的问卷调查。

这是我目前的通用布局:

<form action="index.php" name="My Form" method="POST">
      <ol>
        <li>
          <p>Do you eat cheese?</p><br />
          <input type="radio" name="Q1" value="0"/>Never<br />
          <input type="radio" name="Q1" value="1"/>In the past<br />
          <input type="radio" name="Q1" value="2"/>Sometimes<br />
          <input type="radio" name="Q1" value="4"/>Often<br /><br />
        </li>
            <input type="submit" name="submit" value="Submit the questionnaire"></input>
      </ol>
  </form>

现在,我需要每个问题都有4个按钮,但我不想重复写56次只改变名称(计划更改为“Q1”,“Q2”等)。所以,我想知道是否有一种方法可以创建一个函数,帮助我节省时间不必反复重复这个过程。
我尝试了这种变体
<html>
<body>
    <?php

        function inputs($counter)
            $questions = array("Q1","Q2");

            echo '
            <input type="radio" name=.$questions[$counter]; value="0" 
            />Never
            <br />
            <input type="radio" name=.$questions[$counter]; value="1" />In 
            the past
            <br />
            <input type="radio" name=.$questions[$counter]; value="2" 
            />Sometimes
            <br /> 
            <input type="radio" name=.$questions[$counter]; value="4" 
            />Often
            <br />
            <br /> ';
    ?>
</body>
</html>

为了在列表项中像这样进行操作

<p>Do you eat cheese?<p><br />
<?php inputs(0);?>

(包含函数的文件已经被包含)

曾经尝试过将其打印在页面上(所有内容都正确,但没有转移到index.php进行计算)。我怀疑我应该将inputs(0)设置为某些东西,但我不知道。

所以我的问题是 ->

  1. 是否可以像这样将输入放入php函数,并使它们输出一个值用于表单(稍后可用于index.php)?
  2. 如果不行,是否有一种方法创建子表单函数,其中每个问题都是自己的表单,并将值输出到更大的表单中?
  3. 我应该勇敢地写下这些问题吗?

干杯!


你的函数中存在一些引号或转义符的错误 - 因为你在使用单引号作为字符串属性值的引号,所以字符串内的值应该用双引号括起来(一般而言),而 PHP 变量需要用单引号和点号进行转义(例如:' . $var . ')。 - Professor Abronsius
嗨,只是为了我理解,你每次想更改的只是name,从'Q1'变成'Q2'吗? - The Codesee
单选按钮分配的值是否关键 - 即:0,1,2,4 - Professor Abronsius
@TheCodesee 是的,那是我想要改变的唯一部分。 - HBARL
@RamRaider 这些变量用于 index.php 脚本中,根据问题的答案计算得分。我也知道我发布的函数代码存在一些问题,所以谢谢你,我会稍后尝试修复它。 - HBARL
没问题 - 但是如果您看一下我发布的第二段代码,它应该将这些值分配为单选按钮的值 - 其名称根据调用函数时传递的整数i而更改为q1、q2、q3... - Professor Abronsius
5个回答

1
使用这个:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body>
<?php

    // Make sure you have 56 elements in the array
    $questions = array("Do you always eat cheese?", "Are you human?",
                       "Do you have a dog?", "Mr. Murphy is funny?",
                       ...);

    for($i=1; $i < 57; $i++)
    {
        echo '<table>
              <tr>
                  <td><p>'.$questions[$i-1].'</p></td>
              </tr>
              <tr>
                  <td><input type="radio" name="Q'.$i.'" value="0"/>Never</td>
              </tr>
              <tr>
                  <td><input type="radio" name="Q'.$i.'" value="1"/>In the past</td>
              </tr>
              <tr>
                  <td><input type="radio" name="Q'.$i.'" value="2"/>Sometimes</td>
              </tr>
              <tr>
                  <td><input type="radio" name="Q'.$i.'" value="4"/>Often</td>
              </tr>
              </table>';
   }
?>
</body>
</html>

嗨,伊万,感谢你回复我。据我所知,你的代码可以满足我的需求,但我也能看到你试图使用一个表格。可能是我编码的方式不同,但无论如何,单选按钮并没有出现在任何表格中,但我并不介意,因为它能正常工作。再次感谢! - HBARL
该表格旨在替换您列出的 <br/> 标记。而且,这样您就可以为 tablerowscells 放置 CSS 样式,并且每个 radio 都有单独的容器。另外要提一下,默认情况下,table 的边框/线是不可见的。 - Ivan86
这将打印出56张桌子。 - Ivan86

1

我会将这个作为答案写出来,而不是评论,因为其中包含了太多的代码。

在你目前的学习阶段,你应该做以下几点:

让函数返回一些东西,稍后再进行回显。其次,让我们纠正一个小错误:

<?php

function inputs($question) { // you were missing these { here

                           // missing ' here          and here + the dot  
    $html = '<input type="radio" name='.$question.' value="0"/>Never';
    // add some more html                 // better with " around the string
    $html += '<br/><input type="radio" name="'.$question.'" value="1" />In the past';
    // add whatever you like to add to that...

    // at the end let's return all we've got to use it later:
    return $html;
}


// and now you can do:
$questions = array("Q1","Q2");
for($i=0;$i<count($questions);$i++) {
     echo inputs($questions[$i]); // send only one question to the function
}
?>

嗨Jeff,感谢您指出一些语法错误,我现在意识到它们非常重要。至于代码,我理解函数部分,但是我不理解第二部分。如果我在我的问题下输入<?php inputs(1);?>,那么单选圆圈不会显示,并且我在用于测试的WAMPServer页面顶部有2个零。 - HBARL
重要的是 echo inputs(whatever)。现在我们返回 HTML 以便稍后回显,这更加优雅。 - Jeff

1

我应该硬着头皮写问题吗?

绝对不!那太多代码了!

如果我理解你的问题正确,你只是想让每个56个不同问题的name发生变化。虽然你用数组的思路是正确的,但我认为每次递增$i会更容易,像这样:

function inputs($counter) {
   $i = 0
   while($i < 57) {
      echo'
      <input type="radio" name="Q'.$i.'" value="0" 
      />Never
      <br />
      <input type="radio" name="Q'.$i.'" value="1" />In 
      the past
      <br />
      <input type="radio" name="Q'.$i.'" value="2" 
      />Sometimes
      <br /> 
      <input type="radio" name="Q'.$i.'" value="4" 
      />Often
      <br />
      <br /> 
      ';
      $i++
   }
}

这种方法是可行的,但问题在于我目前想把INPUTS函数放在每个问题下面,这样我最终会有56个输入区域。不过我能看出你的方法很有用,所以谢谢你!我认为我会做出改变-->只需摆脱while循环和$i,并使计数器等于问题编号即可。 - HBARL
一旦我这样做了,我就可以确认它对我想要的工作有效,谢谢!我知道重复太多代码会很麻烦,但就是找不到另一种方法。 - HBARL

1
您可以像这样做:

也许:

function inputs( $i ){
    $answers=array(
        'Never','In the past','Sometimes','Often'
    );
    foreach( $answers as $key => $answer ) echo "<div><input type='radio' name='Q{$i}' value='{$key}' />{$answer}</div>";
}

<p>Do you eat cheese?<p>
<?php inputs(0);?>

<p>Do you eat bananas?<p>
<?php inputs(1);?>

假设每个单选按钮分配的值很重要(0,1,2,4),那么与其像以前一样使用默认的数字索引($key),不如
function inputs( $i ){
    $answers=array(
        0   =>  'Never',
        1   =>  'In the past',
        2   =>  'Sometimes',
        4   =>  'Often'
    );
    foreach( $answers as $key => $answer ) echo "<div><input type='radio' name='Q{$i}' value='{$key}' />{$answer}</div>";
}

太棒了,谢谢!代码的第二部分完全符合我的需求! - HBARL

1
这个函数会做你需要的事情。
<?php
 function inputs($counter){
         $labels = ['Never','In the past','Sometimes','Often'];
         $questions = array("Q1","Q2");

    for($n=0; $n < $counter; $n++){

    echo '<input type="radio" name="'. $questions[0] .'" value="'. $n .'" />  '. $labels[$n] .' <br /> ';
    }
  }
?>

<!--  html -->

<p> Do you eat cheese? <p> <br />
<?php echo inputs(5); ?>

//where 5 is the number of inputs you need

你可以在这个数组$labels里放任何标签,它将会自动创建。 - samehanwar
从我得到的答案中,这个是我选择的。对我来说足够简单,而且非常有效。非常感谢![这条评论是给任何在以后阅读此内容的读者] - HBARL
@samehanwar,你应该在$questions中的type="radio" name="'. $questions[0] .'"中用$n替换0 - Ivan86
@Ivan86 不应该。再检查一遍。 - samehanwar
你说得没错,但现在我们来到了第二个问题...他所有的“单选按钮”都将具有相同的名称“Q1”,因为$questions[0]始终被执行为[0]。这对于同一组的单选按钮是必需的,但他提到了56个组,所有其他组也将具有“Q1”。我说得对吗? - Ivan86
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接