여러 확인란에서 $ _POST 받기
여러 개의 확인란이있는 양식이 하나 있습니다 (각각 코드가 있음).
<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">
$row['Report ID']
데이터베이스의 기본 키는 어디에 있으므로 각 값이 다릅니다.
어떤 확인란이 선택되었는지 어떻게 알 수 있습니까? (아마도 여러)
이것은받은 편지함 시스템을위한 것이며 $row['Report ID']
확인란을 선택한 모든 메시지 (ids :)를 삭제하기 위해 버튼을 클릭 하십시오.
양식에서 이름을로 설정하면 check_list[]
모든 확인란에 배열 ( $_POST['check_list'][]
) 로 액세스 할 수 있습니다 .
요청한 샘플은 다음과 같습니다.
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
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.
}
}
?>
편집 아래의 주석에서 @Marc의 말을 반영합니다.
게시 된 모든 값을 반복 할 수 있습니다.
HTML :
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
PHP :
foreach($_POST['check_list'] as $item){
// query to delete where item = $item
}
확인란의 이름을 적절하게 지정해야합니다.
<input type="checkbox" name="check_list[]" value="…" />
그런 다음 모든 확인란에 액세스 할 수 있습니다
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
// do something
}
추신. 출력을 올바르게 이스케이프하십시오 ( htmlspecialchars()
)
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
그리고 포스트 후에, 당신은 그것들을 반복 할 수 있습니다 :
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $report_id){
echo "$report_id was checked! ";
}
}
또는 이전 페이지에서 게시 된 특정 값을 얻으십시오.
if(isset($_POST['check_list'][$report_id])){
echo $report_id . " was checked!<br/>";
}
죄송합니다, 오래된 주제이지만 이것은 @JamesRattray 및 다른 사람들이 문제를 겪었던 것에 대해 언급해야 할 중요한 부분입니다.
PHP 스크립트에 여러 값을 게시하려고 할 때 input
태그를 직접 닫지 마십시오 (를 사용하여 />
). 태그를 자체 닫으면 태그가 배열 정의를 종료하고 제출시 스크립트에 단일 값만 게시됩니다. 실제로 @Scone의 답변은 />
이 변경 될 때까지 작동하지 않을 수 있습니다 .
이 Warning: Invalid argument supplied for foreach() in /home1/...
경우 PHP 오류가 발생합니다 .
EDIT: As noted in the comments below, further research suggests that this is dependent on DOCTYPE. If you have the DOCTYPE set for XHTML, it will require that the input tag be closed. If you have DOCTYPE set of HTML5, it will require that the input tag not be closed. There is also a chance that this could be dictated by what browser the user is viewing the page in and how well it follows the DOCTYPE specified in the code.
It's pretty simple. Pay attention and you'll get it right away! :)
You will create a html array, which will be then sent to php array. Your html code will look like this:
<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
Where [1] [2] [3]
are the ID
s of your messages, meaning that you will echo
your $row['Report ID']
in their place.
Then, when you submit the form, your PHP array will look like this:
print_r($check_list)
[1] => checked [3] => checked
Depending on which were checked and which were not.
I'm sure you can continue from this point forward.
참고URL : https://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes
'Programming' 카테고리의 다른 글
PHP-FPM이 오류 로그에 기록하지 않습니다 (0) | 2020.06.09 |
---|---|
변수 안에있는 문자열에 큰 따옴표를 추가하는 방법은 무엇입니까? (0) | 2020.06.09 |
컨트롤에 툴팁을 어떻게 추가합니까? (0) | 2020.06.09 |
Git에서 하나의 파일 만 가져올 수 있습니까? (0) | 2020.06.09 |
SourceTree에서 두 가지에 시각적 차이를 얻는 방법이 있습니까? (0) | 2020.06.09 |