如何在提交

时间:2016-02-16 13:24:55

标签: php post radio-button

我有以下PHP代码,在单选按钮中显示问题集和多个答案。

foreach ($cursor as $obj)
{
    echo ' '.$result["question"].'<br><br> &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" name="cl[]"> '.$result["ch1"].'<br>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" name="cl[]"> '.$result["ch2"].'<br>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="radio" name="cl[]"> '.$result["ch3"].'</p>';
}

<button type="submit"  >Submit</button>

我尝试使用无效的代码,

<?php
if (isset($_POST['submit'])) {
  foreach($_POST['cl'] as $selected) {
    echo "<p>".$selected ."</p>";
    }
}
?>

如何在数组中存储这些多个值?  如何在提交时将这些数组值导航到名为page1的另一个页面?  如何在另一个名为page1的页面中访问这些值?

格式如下所示

Part of main memory where it stores data temporarirly

     ROM
     FLOPPY DISK
     RAM

It is a component of operating system that exposes function to user and applications

     Shell
     Kernel
     Shell and Kernel

2 个答案:

答案 0 :(得分:1)

要进行多项选择,您必须使用复选框类型的输入而不是无线电。这是一个例子:

HTML文件:

<form action="my_file.php" method="post">
    <input type="checkbox" name="check[]" value="1">
    <input type="checkbox" name="check[]" value="2">
    <input type="checkbox" name="check[]" value="3">
    <input type="checkbox" name="check[]" value="4">
    <input type="checkbox" name="check[]" value="5">
    <input type="submit" />
</form>

在你的PHP文件中:

foreach($_POST['check'] as $check) {
        echo $check; 
}

$_POST['check']中,您拥有所有选择的值

修改多个问题

第一种方法:使用不同的变量名foreach问题:

 <form action="test.php" method="post">
    <p>
        Part of main memory where it stores data temporarirly :
        <p>
            <input type="checkbox" name="answer1[]" value="ROM"> ROM
            <input type="checkbox" name="answer1[]" value="FLOPPY DISK"> FLOPPY DISK
            <input type="checkbox" name="answer1[]" value="RAM"> RAM
        </p>
    </p>
    <p>
        It is a component of operating system that exposes function to user and applications :
        <p>
            <input type="checkbox" name="answer2[]" value="Shell"> Shell
            <input type="checkbox" name="answer2[]" value="Kernel"> Kernel
            <input type="checkbox" name="answer2[]" value="Shell and Kernel"> Shell and Kernel
        </p>
    </p>
    <input type="submit"/>
</form>

所以在这种情况下,您可以在$_POST['answer1']中找到第一个问题的答案,$_POST['answer2']中第二个问题的答案......

第二种方法:多维数组

<form action="test.php" method="post">
    <p>
        Part of main memory where it stores data temporarirly :
        <p>
            <input type="checkbox" name="answer[1][]" value="ROM"> ROM
            <input type="checkbox" name="answer[1][]" value="FLOPPY DISK"> FLOPPY DISK
            <input type="checkbox" name="answer[1][]" value="RAM"> RAM
        </p>
    </p>
    <p>
        It is a component of operating system that exposes function to user and applications :
        <p>
            <input type="checkbox" name="answer[2][]" value="Shell"> Shell
            <input type="checkbox" name="answer[2][]" value="Kernel"> Kernel
            <input type="checkbox" name="answer[2][]" value="Shell and Kernel"> Shell and Kernel
        </p>
    </p>
    <input type="submit"/>
</form>

在这种情况下,您的答案都在多维数组$_POST['answer']中。但要在第一个问题的答案中访问,您必须$_POST['answer'][1]。这也是一个数组,其中包含您为第一个问题选择的所有答案。

答案 1 :(得分:1)

1)在表单按钮中添加name="submit"

<button type="submit" name="submit">Submit</button>

现在你可以检查

if (isset($_POST['submit'])) { 
 // do stuff
}

2)你在PHP中完全错误地使用了单选按钮。 使用单选按钮,您只能在同一组中选择一个不超过一个的值

您可以通过以下方式在PHP中使用单选按钮: -

<form action="" method="post">
<input type="radio" name="radio" value="Radio 1">Radio 1
<input type="radio" name="radio" value="Radio 2">Radio 2
<input type="radio" name="radio" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</form>

if (isset($_POST['submit'])) {
  if(isset($_POST['radio'])){
  echo "You have selected :".$_POST['radio'];  //  Displaying Selected Value
  }
}

如果您想选择多个值,请使用复选框。

<form action="checkbox.php" method="post">
    <input type="checkbox" name="flavours[]" value="strawberry">
    <input type="checkbox" name="flavours[]" value="vanilla">
    <input type="checkbox" name="flavours[]" value="chocolate">
    <input type="submit" value="Submit!">
</form> 


<?php  // checkbox.php

echo "<pre>";
var_dump($_POST);
exit;


// submitting the form with a random selection of flavours will give:


array(2) {
  ["flavours"]=>
  array(2) {
    [0]=>
    string(10) "strawberry"
    [1]=>
    string(7) "vanilla"
  }
}

// If you leave all checkboxes unselected, $_POST['flavours'] will not be set.