PHP复选框即使未选中也会发布

时间:2017-12-11 23:24:02

标签: javascript php forms checkbox

我在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>

1 个答案:

答案 0 :(得分: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";
}