簡體   English   中英

從多個復選框獲取表單數據

[英]to get the form data from multiple checkbox

因為我想獲取表單數據並想處理該數據並將其存儲到數據庫中,所以我獲取了所有復選框值,但是盡管我在$ _POST ['text-name']中使用了$ _POST ['text-name']代碼...請幫助我得到錯誤..我的代碼如下

 if(isset($_POST['give-score'])&&!empty($_POST['checked'])){
        $employeedetails  = $_POST['checked'];
        $score = $_POST['score'];
        $username = $employeedetails[1];
        $workname = $employeedetails[2];
        changeworkstatus($username,$workname,$con);
        $workname  = $employeedetails[2];
        addscorepoints($workname,$score,$con);
}else{
    echo "";
}

而我的表單html代碼在下面

   <td><input type="checkbox"  name="checked[]" id="employeework" value="" style="align: center"></td>
            <td><input type="checkbox" name="checked[]" id="employeework"value="<?php  echo $results['username']; ?>"><?php  echo $results['username']; ?></td>
            <td><input type="checkbox" name="checked[]" id="employeework"value="<?php echo $results['work_name'];?>"><?php echo $results['work_name'];?></td>
            <td><input type="text" name="score" id="score" placeholder="Your Score Here"></td>
            <td><input type="submit" name="give-score"></td>

該表中使用的PHP部分工作正常..但是input [type = text]我沒有得到那個值。

PHP數組索引從0而不是1開始。那么您必須更改以下行:

$username = $employeedetails[0];
$workname = $employeedetails[1];

希望這對您有所幫助!

我認為即使您提供了以下我嘗試過的代碼,您的代碼也沒有問題。 我從表單元素獲取所有值。

html文件的代碼

<html>
<body>
<form action="test.php" method="POST"> Checked value:
    <td><input type="checkbox" name="checked[]" id="t1" value="test1">test1</td>
    <td><input type="checkbox" name="checked[]" id="t2" value="test2">test2</td>
    <td><input type="checkbox" name="checked[]" id="t3" value="test3">test3</td>
    <td><input type="text" name="score" id="score" placeholder="Your Score Here">        </td>
    <td><input type="submit" name="give-score"></td>
</form>
</body>
</html>

PHP文件代碼

<?php
if(isset($_POST['give-score'])&&!empty($_POST['checked'])){
$employeedetails  = $_POST['checked'];
echo $score = $_POST['score']."<br>";
echo $username = $employeedetails[0]."<br>";
echo $workname = $employeedetails[1]."<br>";
echo $workname = $employeedetails[2]."<br>";
}else{
echo "No data found";
}
?>

我希望這能幫到您!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM