2016-02-27 226 views
0

我已經在HTML文件複選框下面的代碼:HTML複選框到PHP

<!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(); 

} 

?> 

沒有錯誤認識,但複選框場沒有按「T在我的PDF顯示生成的,我使用FPDF爲:

enter image description here

我該如何解決這個問題?

+0

發表您的完整的HTML表單 –

+0

完整的HTML現在又增加了 – compcrk

+0

<形式缺少收盤> –

回答

1

根據錯誤[]不適用於讀取。它是爲了分配值。

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

$t01會有什麼$_POST['check_list']

2

不能使用[]語法從排列讀:[]是語法一個新的項目添加到現有陣列。

您的HTML代碼name="check_list[]"發送php解釋爲數組的三個POST變量,因此 - 在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變量中。

+0

我已經做了以下編輯,沒有錯誤沒有,但我有一個問題,我已經編輯了我的問題上面, – compcrk

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>"; 
} 
+0

如果你會得到所有的檢查三個複選框? – C2486

+0

給我一個錯誤:注意:未定義的索引:在 – compcrk

+0

中的check_list這個答案是完全錯誤的。他們需要括號作爲數組和'foreach'。 –