php的html复选框

时间:2016-02-27 12:16:55

标签: php html checkbox fpdf

我在html文件中有以下代码:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
 <h1 align='center'>Welcome to YRRHELP Channel </h1>
</header>
<section align = 'left'>

<h2> Register </h2>

    <form action = "form.php" method = "post">
<p>
 <label>Name of Order :</label>
   <input type="text" name = "order" />
   </p>
   <p>
   <label>Quantity :</label>
   <input type="text" name="quantity" />
   </p>
    <p>

    <label>Country:</label>
    <select name="country">    
<option>Choose country</option>
             <option>NIG</option>
             <option>ABJ</option>
             <option>DXB</option>
             <option>SHJ</option>
             <option>KSA</option>
    </select>
    </p>
    <p>

   <label>Postcode :</label>
   <input type="text" name="dob" />
    </p>

<p>
     <label>Choose type of order :</label>

   <input type="checkbox" name="check_list[]" value="to1" > Energy Bar<br>
   <input type="checkbox" name="check_list[]" value="to2"> Tents <br>
   <input type="checkbox" name="check_list[]" value="to3"> Canned foods <br>

   </p>
    <p>
   <label>Date :</label>
   <input type="date" name="date" />
   </p>
   <p>
   <textarea rows="8" cols="50" name="comment">Enter more details...</textarea>

   </p>
   <input type="submit" value="Register" name="submit" />


   </form>
   </section>

   </body>

   </html>

我的整个PHP代码是:

<?php
define('FPDF_FONTPATH','/Applications/XAMPP/xamppfiles/lib/php'); 

if(!empty($_POST['submit']))
{

$order=$_POST['order'];
$quantity=$_POST['quantity'];
$country=$_POST['country'];
$post=$_POST['dob'];
$date=$_POST['date'];
$comment=$_POST['comment'];


require("fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont("Arial","B",16);

$pdf->Cell(10,10,"UNHRD",0,1,'C');

$pdf->Cell(40,10,"Order :",1,0);
$pdf->Cell(70,10,"$order",1,1,'C');

$pdf->Cell(40,10,"Quantity :",1,0);
$pdf->Cell(70,10,"$quantity",1,1,'C');

$pdf->Cell(40,10,"Country :",1,0);
$pdf->Cell(70,10,"$country",1,1,'C');

if(!empty($_POST['check_list'])) {

foreach($_POST['check_list'] 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.

$pdf->Cell(50,10,"Total order :",1,0);
$pdf->Cell( 70, 10, $check, 1, 1 );



    }
  }  

$pdf->Cell(40,10,"Date :",1,0);
$pdf->Cell(70,10,"$date",1,1);

$pdf->Cell(40,10,"Comment :",1,0);
$pdf->Cell(70,10,"$comment",1,1,'C');

$pdf->Output();

}

?>

没有错误可知,但复选框字段没有显示在我生成的pdf中,我使用的是fpdf:

enter image description here

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

您无法使用[]语法从数组中读取:[]是将新项目附加到现有数组的语法。

您的HTML代码name="check_list[]"发送三个POST解释为php的变量,因此 - 在php页面中 - 在$_POST[check_list]中,您将拥有一个包含三个元素的普通数组。

只需删除此行:

$t01=$_POST['check_list'][];

和 - 在foreach循环内 - 更改此行:

$pdf->Cell(50,10,"$t01",1,1);

以这种方式:

$pdf->Cell( 50, 10, $check, 1, 1 );

使用foreach$_POST['check_list'],您为每个数组项的值分配$check,因此您所需的值已经在$check变量中。

答案 1 :(得分:1)

根据错误[]不是为了阅读。它是用于分配值。

$t01=$_POST['check_list'];// no need for []

$t01将拥有$_POST['check_list']所拥有的

答案 2 :(得分:0)

您的HTML代码会发回值数组:

<input type="checkbox" name="check_list[]" value="to1" > Energy Bar<br>
<input type="checkbox" name="check_list[]" value="to2"> Tents <br>
<input type="checkbox" name="check_list[]" value="to3"> Canned foods <br>

所以在你的PHP中,你会使用类似的东西:

if (isset($_POST['check_list'])) 
{
    $t01 = $_POST['check_list'];
    $nCount = count($t01); // The count of boxes checked 
    echo "<p>";

    for ($k=0; $k < $nCount; $k++) {
        echo "Array element [".$k."] =".$t01[$k]."<br />";
    }
    echo "</p>";
}