【问题标题】:Is it possible to use <input> tags inside a php function which is then inside <form> tags?是否可以在 <form> 标签内的 php 函数中使用 <input> 标签?
【发布时间】:2018-05-30 17:58:41
【问题描述】:

我对 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 . '
  • 您好,我明白了,您每次只想更改name,从“Q1”变为“Q2”?
  • 分配给单选按钮的值是否关键 - 即:0,1,2,4
  • @TheCodesee 是的,这是我唯一想改变的部分
  • @RamRaider 它们在 index.php 脚本中用于根据问题的答案计算分数。我也知道我发布的功能代码存在一些问题,所以谢谢,我会尝试修复它

标签: php html forms input


【解决方案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>

【讨论】:

  • 嗨,Ivan,感谢您回复我。据我所知,您的代码可以满足我的需要,但我也可以看到您正在尝试使用表格。这可能是我编码的方式,但无线电圈不会出现在任何类型的表格中,但我不介意,因为它有效。再次感谢!
  • 该表格旨在替换您列出的 &lt;br/&gt; 标记。同样通过这种方式,您可以将CSS 样式放在tablerowscells 上,并且每个radio 都有单独的容器。顺便提一下,table 边框/线条默认是不可见的。
  • 这会打印 56 个表格。
【解决方案2】:

我将把它写成答案而不是评论,因为里面的代码太多了。

你应该做什么(在这种学习状态下):

让函数返回 稍后回显的内容。其次让我们纠正一个小错误:

<?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
}
?>

【讨论】:

  • 嗨,杰夫,感谢您指出一些语法错误,我现在意识到这些很重要。至于代码,函数部分看懂了,第二部分看不懂。如果我在我的问题下方输入 ,则不会显示单选圆圈,并且我的 WAMPServer 页面顶部有 2 个零用于测试它。
  • 重要的是echo inputs(whatever)。现在我们正在返回要回显的 html,这更加优雅。
【解决方案3】:

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

绝对不是!代码太多了!

如果我正确理解了您的问题,您只需更改 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,让 counter 等于问题编号
  • 一旦我这样做了,我可以确认它确实可以达到我想要的效果,谢谢!我知道重复代码会太多,但我终其一生都想不出另一种方法。
【解决方案4】:

也许你可以这样做:

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>";
}

【讨论】:

  • 非常感谢您!代码的第二部分非常适合我想做的事情!
【解决方案5】:

这个功能做你需要的。

<?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

【讨论】:

  • 感谢您的代码,但这对我来说并不特别适用,因为我需要始终拥有所有标签。我添加的是一个名为 $values 的数组(其中 $values = array(0,1,2,4);) 和 done value=" '.$values[$n].' ";
  • yoy 可以在这个数组 $labels 中放置你想要的任何标签,它会自动创建。
  • 亲爱的,谢谢! [此外,对于任何其他读者,此代码确实可以生成可以在表单中读取的值。我也会测试其他人]
  • 从我得到的答案中,我选择了这个答案。对我来说很简单,就像一种享受。非常感谢! [此评论适用于以后阅读本文的任何其他读者]
  • @samehanwar 你应该在$questions type="radio" name="'. $questions[0] .'" 中用$n 替换0
猜你喜欢
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 2013-11-14
  • 2018-12-06
  • 2016-06-28
  • 1970-01-01
相关资源
最近更新 更多