[英]php checkbox input update mysql database
如果有人能指出我正確的方向,將不勝感激。 由於某些原因,基於從復選框接收到的輸入,數據庫不會更新。 以下是我用來完成此任務的代碼片段。
我不知道為什么,無論是否選中此復選框,數據庫中的內容都會保持相同的狀態。
這是HTML代碼
<form method="post" action="">
<fieldset>
<input type="hidden" name="Jack" value="<?php echo $post_id; ?>">
<input type="checkbox" name="drink" value="1"> <span>Did you drink today?</span>
<input type="submit" name="post_content" value="Submit">
</div>
</fieldset>
</form>
這是PHP
<?php
if ( isset($_POST['button']) && ( ($_POST['drink']) == 1 ) ) {
$post_id = $_POST['Jack'];
$my_post = array(
'ID' => $post_id,
'post_content' => 'Just whiskey'
);
wp_update_post( $my_post );
}
; ?>
非常感謝你們的投入。
您沒有名為button
的輸入
代碼執行的成功取決於整個條件語句。
if ( isset($_POST['button']) && ( ($_POST['drink']) == 1 ) ) {...}
^^^^^^
它應該是:
if ( isset($_POST['post_content']) && ( ($_POST['drink']) == 1 ) ){...}
根據您提交按鈕的名稱:
<input type="submit" name="post_content" value="Submit">
^^^^^^^^^^^^
打開<?php
標記后,立即將錯誤報告添加到文件頂部。
error_reporting(E_ALL);
ini_set('display_errors', 1);
這將引發Undefined index button...
警告。
您應該檢查$_POST['post_content']
而不是$_POST['button']
,因為您的頁面上沒有按鈕。您應該使用error_reporting
。
如果未選中,則不會設置$_POST['drink']
。 因此,請檢查是否已檢查。
$drink = isset($_POST['drink']) ? $_POST['drink'] : 0;
這樣的事情,然后
if ( isset($_POST['button']) && ( $drink' == 1 ) ) {
<form method="post" action="">
<fieldset>
<input type="hidden" name="Jack" value="<?php echo $post_id; ?>">
<input type="hidden" name="drink">
<input type="checkbox" class='check_or_not'> <span>Did you drink today?</span>
<input type="submit" name="post_content" value="Submit">
</fieldset>
</form>
<script>
$(".check_or_not").click(function(){
if($(this).attr('checked') == 'checked'){
$(this).prev().val(1);
}else{
$(this).prev().val(0);
}
})
</script>
像上面那樣更改您的html,復選框不會傳遞您所除外的值。我假設您正在使用jquery
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.