简体   繁体   English

是否可以在php函数中使用<input>标签,然后在<form>标签内?

[英]Is it possible to use <input> tags inside a php function which is then inside <form> tags?

I am very new to both php and HTML and have been trying to wrap my head around them for the past few days. 我对php和HTML都很陌生,过去几天一直试图绕过它们。 I am trying to create a questionnaire with radio buttons that is 56 questions long. 我正在尝试使用长达56个问题的单选按钮创建调查问卷。

This is the generic layout I have right now 这是我现在的通用布局

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

Now, I need the 4 buttons on each question but I would prefer not to have to write it out 56 times with only the name changing (the plan is to change the name to "Q1", "Q2" etc). 现在,我需要在每个问题上使用4个按钮,但我不想在只更改名称的情况下将其写出56次(计划是将名称更改为“Q1”,“Q2”等)。 So, I was wondering if there is a way to create a function that saves me having to repeat it so often. 所以,我想知道是否有一种方法来创建一个能够让我不得不经常重复它的功能。

I tried variants of this 我试过这个的变种

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

With the intention of then going like this in the list item 然后打算在列表项中这样做

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

(with the file containing the function already included) (包含已包含功能的文件)

And did at one point manage to get it to print out to the page (all correct but didn't transfer over into the index.php for calculations). 并且曾经设法让它打印到页面(所有正确但没有转移到index.php进行计算)。 I have a suspicion I should make inputs(0) equal to something, but I don't know. 我怀疑我应该把输入(0)等于某事,但我不知道。

So my questions are -> 所以我的问题是 - >

  1. Is it possible to put input into php functions like this and get them to output a value for the form (which can be later used for index.php) 是否可以将输入放入这样的php函数中并让它们输出表单的值(以后可以用于index.php)
  2. If not, is there a way to create a subform form function, where each question is its own form and outputs a value into the larger form? 如果没有,有没有办法创建一个子表单表单函数,其中每个问题都是自己的形式,并将值输出到更大的形式?
  3. Should I just bite the bullet and write the questions? 我应该咬紧牙关写下问题吗?

Cheers 干杯

Use this: 用这个:

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

I'll write this as an answer instead of a comment, because it will be too much code in it. 我会把它写成答案而不是评论,因为它中的代码太多了。

What you should do (at this state of learning): 你应该做什么(在这种学习状态):

Let the function return something to be echoed later. 让函数返回稍后要回显的内容。 Second let's correct a small misstake: 第二,让我们纠正一个小错误:

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

Should I just bite the bullet and write the questions? 我应该咬紧牙关写下问题吗?

Definitely not! 当然不! That's way too much code! 那个代码太多了!

If I understand your question correctly, you simply want the name to change for each of the 56 different questions. 如果我正确理解您的问题,您只需要为56个不同的问题中的每一个更改name While you were on the right track with an array, I think it would be easier to increment $i each time, like this: 当你使用数组在正确的轨道上时,我认为每次增加$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++
   }
}

You could do like this perhaps: 你也许可以这样做:

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

Assuming the values assign to each radio button are important ( 0,1,2,4 ) then rather than using the default numerical index ( $key ) as before then 假设分配给每个单选按钮的值很重要( 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>";
}

this function do what you need . 这个功能做你需要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM