簡體   English   中英

如何查看 PHP 中選中了哪些復選框?

[英]How can I see which check boxes are checked in PHP?

我正在顯示數據庫中的一些列,並在每行的末尾放置一個復選框。

while($row = $result->fetch_assoc()){
    echo "<tr><td>" . $row['Computer Name'] . '</td><td>' . $row['Computer IP'];
    echo ($checkbox)?('<td><input type="checkbox" name="selectSystem"/></td>'):'' . "</td></tr>";
}

(忽略我的$checkbox變量,這只是一個 function 參數,允許我在顯示這些行時打開/關閉復選框。)

上面的代碼工作正常,但我不知道如何 output 這些復選框的值(選中或未選中)。 據我了解,所有復選框都具有相同的名稱( selectSystem ),並且它們都應該位於selectSystem數組中。

也許我以錯誤的方式解決這個問題......我只是希望用戶能夠 select 他們想要的行以保留計算機系統。 有沒有更簡單的方法可以做到這一點,還是我應該繼續使用在單擊提交按鈕后聚集起來的愚蠢復選框?

正如 Dharman 所建議的,在您的 while 循環中使用迭代器來創建您的 name 屬性,以便每個名稱都不同。 然后,一旦您的用戶點擊提交按鈕,全局 $_POST 變量將保存他們選擇的復選框的值。

考慮以下代碼示例..

// define a variable to hold your output in...
$output = null;

// as I do not have your database result, I will use an example array of values
// to get a length to use in a while loop to show the iteration using a declared number
// that is iterated within your while loop until the length of the array is reached
$array = [1,2,3,4,5,6,7,8]; //--> Here we have 8 values in the array
// define a number to use as iterator or a counter
$i = 1;
// while loop iterates through array while our counter is less than the number within  our array
while($i < count($array)){
    // concatenate our output variable to include the next input 
    // Each time we iterate through to the next value in our array
    // we concatenate the next numbers iterated value by 1 to our name attribute
    // now your name attribute is uniquely named 

 
    $output .= '<input type="checkbox" name="selectSystem'.$i.'"/>';
    $i++;
}

OUTPUT:

<input type="checkbox" name="selectSystem1"/>
<input type="checkbox" name="selectSystem2"/>
<input type="checkbox" name="selectSystem3"/>
<input type="checkbox" name="selectSystem4"/>
<input type="checkbox" name="selectSystem5"/>
<input type="checkbox" name="selectSystem6"/>
<input type="checkbox" name="selectSystem7"/>
<input type="checkbox" name="selectSystem8"/>

現在,一旦您想在用戶按下表單上的提交按鈕后訪問這些值,該數組就存在於您的 $_POST 數組中。 只需檢查'on'的嚴格條件以訪問已選中復選框的值...

// we define a variable to hold our results and assign a null value to it so when not set it shows nothing...
$stmt = null;
// if our $_POST isset...
if(isset($_POST)){
    // loop through results and use a strict conditional on the $value each loop through
    foreach($_POST as $key => $value){
        if($value === 'on'){
            // concatenate the variable to hold the key and value foreach result
            $stmt .= $key.' -> '.$value.'<br>';
        }
    };
}

<form method="post">
    <?=$output?><!--/ Echo your $output which holds the inputs/-->
    <input type="submit" value="submit" name="submit">
</form>

<?=$stmt?><!--/ Echo your $stmt which holds the $_POST array values of your checkboxes that are 'on', if this variable is null it shows nothing in your html /-->

處理 forms 的最簡單方法是通過 JavaScript 而不是直接在 php 中,因為您必須使用昂貴的工作來解決表單驗證和安全性等問題

If you not using some method of sanitizing the form input you open your self up to SQL injection attacks so it good to handle form values in JS and use a rest api for transferring the results to the PHP server

我曾經像你一樣做 forms 但我意識到 A:它需要更多的工作和 B:你沒有機會以可預測的方式格式化表單響應

 //get the button event const btn = document.getElementById('btn'); //event btn.onclick = () => { // get the element and see if it is checked let cb = document.getElementById('accept'); console.log(cb.checked); /// once all your form data is prepped send to the server through an XHR request //PHP rest API should handle the receiving side };
 <form> <input type="checkbox" id="accept" checked> Accept <input type="button" id="btn" value="Submit"> </form>

這樣做的另一個主要原因是,如果您的服務器正忙於處理多個用戶、數據庫查詢或制作 PDF 文檔等長查詢......

您的客戶端瀏覽器將掛起並等待服務器完成....但是通過使用帶有 xhr 請求的 JS 和諸如 await sinc 之類的東西,如果服務器正忙於繁重的計算任務,客戶端瀏覽器不會凍結。 以這種方式做事將確保您的用戶界面始終處於活動狀態並增加程序的流暢性

我相信這對你有幫助

暫無
暫無

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

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