是否可以在php函数中使用标签,然后在里面 tags?

问题描述 投票:3回答:5

我对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进行计算)。我怀疑我应该把输入(0)等于某事,但我不知道。

所以我的问题是 - >

  1. 是否可以将输入放入这样的php函数中并让它们输出表单的值(以后可以用于index.php)
  2. 如果没有,有没有办法创建一个子表单表单函数,其中每个问题都是自己的形式,并将值输出到更大的形式?
  3. 我应该咬紧牙关写下问题吗?

干杯

php html forms input
5个回答
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

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

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

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

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>
© www.soinside.com 2019 - 2024. All rights reserved.