带有复选框值的表单从数据库填充并插入到数据库中

时间:2017-12-18 11:11:36

标签: php html

我试图搜索谷歌,但我真的不能确定这条件真的适用于我的。 我需要的是复选框数据来自table1所以我可以在需要时添加更多区域。同时,我怎么能分析用户选择了哪些框,因为我需要存储到table2中。

<label>Area:</label>
	<input type="checkbox" name="area"> Ayer Keroh
	<input type="checkbox" name="area" >Bukit Beruang
	<input type="checkbox" name="area"> MITC
	<input type="checkbox" name="area">Malim Jaya
	<input type="checkbox" name="area">Kota Laksamana<br>
Others:
	<textarea rows="2" cols="50" name="comment" placeholder="Enter if area            is not listed" ></textarea>	

1 个答案:

答案 0 :(得分:1)

    <form action="test.php" method="post">
        <input type="checkbox" name="area[]" value="value 1">value 1
        <input type="checkbox" name="area[]" value="value 2">value 2
        <input type="checkbox" name="area[]" value="value 3">value 3
        <input type="checkbox" name="area[]" value="value 4">value 4
        <input type="checkbox" name="area[]" value="value 5">value 5
        <input type="checkbox" value="Other" name="area[]">Other<br>
        <textarea rows="2" cols="50" name="comment">write here
      </textarea>
        <input type="submit" />
        </form>
        <?php
        if(!empty($_POST['area'])) {
            foreach($_POST['area'] as $check) {
                    echo $check; //echoes the value set in the HTML form for each checked checkbox.
                                 //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                                 //in your case, it would echo whatever $row['Report ID'] is equivalent to.
        if($check=='other'){//if the user selected other checkbox
           echo$comment=$_REQUEST['comment'];
        }
            }
        }
        ?>

了解更多信息,您可以参与其中 Get $_POST from multiple checkboxes

如何从sql中检索数据

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT AreaName FROM Areas";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<input type='checkbox' name='area[]' value='".$row['AreaName']."'>".$row['AreaName'];
    }
} else {
    echo "0 results";
}

$conn->close();

?>
相关问题