【问题标题】:PHP checkboxes posting even when not checked即使未选中,PHP 复选框也会发布
【发布时间】:2017-12-11 23:24:01
【问题描述】:

我正在使用 PHP。 表单中的所有内容(复选框除外)都有效,包括错误更正、写入的文本文件和通知电子邮件。

为简洁起见,我只包含了相关代码。

文本框的目标: 1. 如果选中,则在纠错期间应保持选中状态。 2. 如果选中,$Newvar_ 应在电子邮件和文本文件中张贴“X”。

目前,无论是否选中,他们总是发布“X”。

提前感谢您的任何建议。

<?php

/// php error checking of required submitter ID fields here ///

{

// Change the checkbox 'on' to an "X"
// These fields and not required or error checked
if($Form_F_029 == 'on') $Newvar_029 = &$Form_F_029; $Newvar_029 = "X";
if($Form_F_032 == 'on') $Newvar_032 = &$Form_F_032; $Newvar_032 = "X";
if($Form_F_033 == 'on') $Newvar_033 = &$Form_F_033; $Newvar_033 = "X";


// Format the fields for the email notification
fwrite($file,

/// Formatted submitter ID fields here ///

Form F-029:\t $Newvar_029
Form F-032:\t $Newvar_032
Form F-033:\t $Newvar_033

fclose($file);
}

// Format and send email notice of successful submittal
if ($_POST['submit']) {

/// email dialogue here ///

}
?>

<!DOCTYPE HTML>
<html>
<title>Page Title</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" media="screen" />
</head>
<body>

<form action="<?=$_SERVER['PHP_SELF']?>" name="inquiry" method="post">

/// required submitter ID Fields here ///

<div class="field third first">
   <input type="checkbox" id="Form_F_029" name="Form_F_029" <?php if($Form_F_029 == "on"){echo "CHECKED";}?> />
   <label for="F_029"><a href="../../../forms_resources/order-forms/pdf/Form_F_029.pdf">Form F 029</a>
   </label>
</div>
<div class="field third">
   <input type="checkbox" id="Form_F_032" name="Form_F_032" <?php if($Form_F_032 == "on"){echo "CHECKED";}?> />
   <label for="Form_F_032"><a href="../../../forms_resources/order-forms/pdf/Form_F_032.pdf">Form F 032</a>
   </label>
</div>
<div class="field third">
   <input type="checkbox" id="Form_F_033" name="Form_F_033" <?php if($Form_F_033 == "on"){echo "CHECKED";}?> />
   <label for="Form_F_033"><a href="../../../forms_resources/order-forms/pdf/Form_F_033.pdf">Form F 033</a>
   </label>
</div>

<ul class="actions">
<li><input type="submit" class="button small" name="submit" id="submit" value="Send Inquiry" /></li>
</ul>

</form>

<div id="error" title="Form Errors:">
<?php
if (!empty($errors))
{
echo "<div style=\"padding:15px 15px 0 15px\">";
echo "<ul style=\"margin-bottom:20px\">";
foreach ($errors as $error)
echo "<li style=\"font-size:15px; padding:5px\">$error</li>";
echo "</ul></div>";
}
?>
</div>

<!-- JS at the end -->

<script src="/assets/js/jquery.min.js"></script><!-- ver: 1.11.3 -->

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>  

<script>
$('#error').dialog({
height: 380,
width: 260,
modal: true,
resizable: false,
dialogClass: 'no-close error-dialog'
});
</script>

</body>
</html>

【问题讨论】:

  • 问题出在你省略的代码上。
  • 您是否包含或排除了相关代码?你能说明$Form_F_029是如何设置的吗?

标签: javascript php forms checkbox


【解决方案1】:
if($Form_F_029 == 'on') $Newvar_029 = &$Form_F_029; $Newvar_029 = "X";

可以改写为:

if($Form_F_029 == 'on') 
    $Newvar_029 = &$Form_F_029; 
$Newvar_029 = "X";

或

if($Form_F_029 == 'on') {
    $Newvar_029 = &$Form_F_029; 
}
$Newvar_029 = "X";

您尚未将行的最后一部分作为if 结果的一部分。

如果您希望多个语句是有条件的,那么您需要一个显式块:

if($Form_F_029 == 'on') {
    $Newvar_029 = &$Form_F_029; 
    $Newvar_029 = "X";
}

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 2016-06-25
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多