我有一个php表单,将信息保存到我的数据库并在完成后发送电子邮件。但是它不会验证字段以查看它们是否为空,而是打印set和not set选项。关于为什么会发生这种情况的任何想法?在我向其添加表单字段验证之前,它工作得很好。
作为旁注,它适用于FF和Chrome,因为需要html 5咏叹调,但不适用于IE
HTML
<form id="contact" name="contact" action="register1.php" method="post">
<label for='Cname'>Camper Name</label>
<input type="text" name="Cname" maxlength="50" value="" required aria-required=true />
<input type="hidden" id="action" name="action" value="submitform" />
<input type="submit" id="submit" name="submit" value="Continue to Camp Selction"/>
</form>
PHP
<?php
//include the connection file
require_once('connection.php');
//save the data on the DB and send the email
if(isset($_POST['action']) && $_POST['action'] == 'submitform')
{
//recieve the variables
$Cname = $_POST['Cname'];
//form validation (this is where it all breaks)
if (isset($Cname)) {
echo "This var is set so I will print.";
}
else {
echo '<script type="text/javascript">alert("please enter the required fields");</script>';
}
//save the data on the DB (this part works fine)
答案 0 :(得分:1)
<?php
$Cname = isset($_POST['Cname']) ? $_POST['Cname'] : null;
if (isset($Cname)) {
echo "This var is set so I will print.";
}
// OR
if (isset($_POST['Cname'])) {
// Perform your database action here...
}
?>
答案 1 :(得分:0)
考虑使用PHP的空函数
您可以将代码更新为以下内容:
if(!empty($Cname)) {
echo "This var is set so I will print.";
}
答案 2 :(得分:0)
你只需要在else中使用“exit()”吗?