2010-04-16 76 views
1

我有一個表格,它採用以下輸入:
名稱:IBM
表面(平方公尺):9
樓層:(Checkbox1)
電話:(Checkbox2)
網絡:(複選框3)
按鈕發送到下一個PHP頁面。PHP表格複選框問題

當我按下提交按鈕時,上面的所有值都在表中表示。
前兩個(名字和姓氏)在表格中正確顯示。
問題在於複選框。如果我選擇第一個複選框,表中的值應該顯示爲1.如果未選中,表中的值應該爲空。

echo "<td>$Name</td>"; // works properly 
echo "<td>$Surface</td>"; // works properly 
echo "<td>....no idea for the checkboxes</td>; 

我的PHP代碼的一些部分與變量:

<?php 
if (!empty($_POST)) 
{ 
$name= $_POST["name"]; 
$surface= $_POST["surface"]; 
$floor= $_POST["floor"]; 
$phone= $_POST["telefoon"]; 
$network= $_POST["netwerk"]; 


if (is_numeric($surface)) 
{ 
    $_SESSION["name"]=$name; 
    $_SESSION["surface"]=$surface; 
    header("Location:ExpoOverzicht.php"); 
} 
else 
{ 
    echo "<h1>Wrong input, Pleasee fill in again</h1>"; 
} 

if(!empty($floor) && ($phone) && ($network)) 
{ 
    $_SESSION["floor"]=$floor; 
    $_SESSION["phone"]=$phone; 
    $_SESSION["network"]=$network; 
    header("Location:ExpoOverzicht.php"); 
}  
} 

?> 

第二頁有表:

<?php 

$name= $_SESSION["name"]; 
$surface= $_SESSION["surface"]; 
$floor= $_SESSION["floor"]; 
$phone= $_SESSION["phone"]; 
$network= $_SESSION["network"]; 

echo "<table class=\"tableExpo\">"; 

echo "<th>name</th>"; 
echo "<th>surface</th>"; 
echo "<th>floor</th>"; 
echo "<th>phone</th>"; 
echo "<th>network</th>"; 
echo "<th>total price</th>"; 

for($i=0; $i <= $_SESSION["name"]; $i++) 
{ 
    echo "<tr>"; 

     echo "<td>$name</td>"; // gives right output 
     echo "<td>$surface</td>"; // gives right output 
     echo "<td>...</td>"; //wrong output (ment for checkbox 1) 
     echo "<td>...</td>"; //wrong output (ment for checkbox 2) 
     echo "<td>...</td>"; //wrong output (ment for checkbox 3) 
     echo "<td>....</td>"; 

    echo "</tr>;"; 
} 
echo "</table>"; 



<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1"> 
<h1>Vul de gegevens in</h1> 
<table> 
    <tr> 
     <td>Name:</td> 
     <td><input type="text" name="name" size="18"/></td> 
    </tr> 
    <tr> 
     <td>Surface(in m^2):</td> 
     <td><input type="text" name="surface" size="6"/></td> 
    </tr> 
    <tr> 
     <td>Floor:</td> 
     <td><input type="checkbox" name="floor" value="floor"/></td> 
    </tr> 
    <tr> 
     <td>Phone:</td> 
     <td><input type="checkbox" name="phone" value="phone"/></td> 
    </tr> 
    <tr> 
     <td>Network:</td> 
     <td><input type="checkbox" name="network" value="network"/></td> 
    </tr> 
    <tr> 
     <td><input type="submit" name="verzenden" value="Verzenden"/></td> 
    </tr> 
</table> 

,因爲我不得不把它翻譯有可能有幾個拼寫錯誤。 此致敬禮。

+0

的解決方案,你的HTML代碼看起來像??? – lfx 2010-04-16 09:15:35

+0

添加了表單html代碼。一切都在2個PHP文件。 – Sef 2010-04-16 09:21:43

回答

2

而不是直接分配的複選框變量,看他們是否已檢查或不先。

$verdieping = isset($_POST["floor"]) ? $_POST["floor"] : 0; 
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0; 
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0; 

這樣,如果用戶沒有選中一個複選框,你必須分配給它,而不是一個未定義的變量的「0」的數值。

+0

謝謝,這正是我正在尋找。 – Sef 2010-04-16 09:30:49

1

如果要聲明一個複選框有:

<input type="checkbox" name="mycheckbox" value="1"> 

您可以通過提交表單後檢查值:

if(!empty($_POST["mycheckbox"])) { 
    // checkbox was checked 
} 
else { 
    // checkbox was not checked 
} 
+0

你好,在我的第一個php文件中有。但是,究竟我放在我的表td元素來調用該值? – Sef 2010-04-16 09:23:52

1

在這個PHP頁面,您可以這樣寫,也可能是你的問題

if (!empty($_POST)) 
{ 
$standnaam = $_POST["name"]; 
$oppervlakte = $_POST["surface"]; 
$verdieping = $_POST["floor"]; 
$telefoon = $_POST["telefoon"]; 
$netwerk = $_POST["netwerk"]; 


if (is_numeric($oppervlakte)) 
{ 
    $_SESSION["name"]=$standnaam; 
    $_SESSION["surface"]=$oppervlakte; 
    header("Location:ExpoOverzicht.php"); 
} 
else 
{ 
    echo "<h1>Wrong input, Pleasee fill in again</h1>"; 
} 

if(!empty($verdieping) && ($telefoon) && ($netwerk)) 
{ 
    $_SESSION["floor"]=$verdieping; 
    $_SESSION["phone"]=$telefoon; 
    $_SESSION["network"]=$netwerk; 
    header("Location:ExpoOverzicht.php"); 
}  
}