2012-06-18 31 views
-3

我有這個代碼,我打印表格行和複選框與他們,我需要在另一個PHP文件中打印選中的行..我該怎麼做?從複選框中檢索信息php/mysql

我需要的東西,如SQL =選擇$ checkbox1,checkbox2或但它是更好地做到這一點....

<form action='report.php' method='post'> 

<?php // Script 12.7 - sopping.php 

$db = mysql_connect('localhost', 'root', ''); 
mysql_select_db('db_up', $db); 

echo "<table border='1' class='tabtext'>"; 

$result = mysql_query("SELECT * FROM hostess"); 
$numrows = mysql_num_rows($result); 
$numfields = mysql_num_fields($result); 

// show headers 
echo '<thead><tr>'; 
for ($field = 0; $field < $numfields; $field++) { 
    $field_name = mysql_field_name($result, $field); // instead of $i 
    echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>'; 
} 

echo '</tr></thead>'; 

echo '<tbody>'; 
for ($row = 0; $row < $numrows; $row++) { 
    $data = mysql_fetch_assoc($result); 
    echo '<tr>'; 
    for ($field = 0; $field < $numfields; $field++) { 
     $field_name = mysql_field_name($result, $field); 
     if (isset($_POST['checkbox'][$field_name])) { 
      echo '<td>' . $data[$field_name] . '</td>'; 
     } 
    } 
    echo '</tr>'; 
} 
echo '</tbody>'; 
echo '</table>'; 


?> 
<input type='submit' value='Submit' /> 
</form> 
+1

Umm ...提交表單,從$ _POST檢索複選框的值,使用這些ID檢索有關這些複選框的信息,打印出信息,...並完成? –

+0

我還沒有嘗試過任何東西......因爲我不知道從哪裏開始,沒有任何相似的東西..我在網上搜索過.. – Landi

+0

是的,這就是我需要做的Marc B – Landi

回答

1

確定。所以首先你應該只有這個文件生成表單。根據您佈置表格的方式,您希望有兩行,第一行是字段名稱,第二行包含複選框本身。因此,這裏的是:

<form action='report.php' method='post'> 
<?php // Script 12.7 - sopping.php 

$db = mysql_connect('localhost', 'root', ''); 
mysql_select_db('db_up', $db); 

echo "<table border='1' class='tabtext'>"; 

$result = mysql_query("SELECT * FROM hostess"); 
$numrows = mysql_num_rows($result); 
$numfields = mysql_num_fields($result); 

// show headers 
echo '<thead><tr>'; 
for ($field = 0; $field < $numfields; $field++) { 
    $field_name = mysql_field_name($result, $field); 
    echo '<th>'. $field_name . '</th>'; // only the field name 
}  
echo '</tr></thead>'; 
echo '<tbody><tr>'; 
for ($field = 0; $field < $numfields; $field++) { 
    $field_name = mysql_field_name($result, $field); 
    echo 
     '<td> 
      <input type="checkbox" name="checkbox['.$field_name.']" value="1"/> 
     </td>'; 
} 
echo '</tr></tbody>'; 
echo '</table>'; 
?> 
<input type='submit' value='Submit' /> 
</form> 

然後在您所提交的表單(report.php),你會抓住你張貼的東西,並顯示只顯示您所提交的複選框新表的文件。以下是你可以做的一個例子。

<?php 
// within report.php (THIS IS AN EXAMPLE ONLY) 

// check if the checkbox fields were submitted 
// and if not empty we know that items have been checked. 
if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])){ 
    // iterate through the checked items. 
    // this is an associative array because you gave the items a key 
    foreach($_POST['checkbox'] as $field => $value){ 
    // do some stuff 
    echo "<p>Checked Field: $field<br/>Value:$value</br></p>"; 
    } 
} else { 
    // display a message saying that nothing was submitted 
    // you could also display some error or redirect back to the form etc. 
    echo '<p>No Check boxes have been checked</p>'; 
}?> 

我希望這足以讓你的球滾動。試一下,運行你的代碼,看看它的行爲。確保您的表單以您想要的方式顯示,並且提交正確運行並至少向您顯示某些內容。如果需要的話,只需使用我的示例代碼,如果你看到的東西意味着它到達那裏。然後,您可以將該示例代碼替換爲您真正想要顯示的內容。不僅如此,我基本上會爲你寫代碼。試一試。祝你好運。