2012-06-26 16 views
0

我有一个php表单,它将信息保存到我的数据库并在完成时发送一封电子邮件。但是它不会验证这些字段是否为空,而是打印set和not set选项。任何想法为什么这可能会发生?它在我将表单字段验证添加到它之前完美运行。php表单验证问题 - 无法打印到标题

作为一个侧面说明,它工作在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

考虑使用PHP的空功能

PHP.Net Manual Empty()

您可以更新您的代码如下:

if(!empty($Cname)) { 
    echo "This var is set so I will print."; 
} 
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... 
    } 
?> 
+0

它仍然抛出这个错误:警告:不能更改头信息 - 头已经发出(输出开始register1.php:37)在register1.php线105 – Keith

+0

确定在顶部开始你的输出缓冲器register1.php像'<?php ob_start(); ?>在任何其他元素之前。 –

+0

这有助于我将冲洗添加到最后 – Keith