所有字段都需要PHP错误 - 但插入了所有字段

时间:2014-05-20 19:06:33

标签: php html forms requiredfieldvalidator

我正在使用php处理程序(signup-form-handler.php)创建一个注册表单(signup.html)。 但似乎我做错了什么。

这是我的代码:

$errors = '';
$myemail = 'example@domain.com';//<-----Put Your email address here.
if(empty($_POST['FirstName'])  || 
   empty($_POST['LastName'])  || 
   empty($_POST['City'])  || 
   empty($_POST['Country']))
{
    $errors .= "\n Error: all fields are required";
}

$FirstName = $_POST['FirstName']; 
$LastName = $_POST['LastName'];
$City = $_POST['City'];
$Country = $_POST['Country'];
$Int1 = $_POST['Int1'];       // <---- Here's what seems to be the issue
$Int2 = $_POST['Int2'];       // <---- Here's what seems to be the issue 

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Udfyldelse af test på Gamification: $FirstName";
    $email_body = "Du har fået en ny besvarelse. ".
    "Her er besvarelserne:\n Fornavn: $FirstName \n Efternavn: $LastName \n By: $City \n Land $Country \n".
    "De fire personlige interesser: $Int1 og $Int2"; 

    $headers = "From: $myemail\n"; 

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 

以下是相关的HTML代码:

<form id="contact_form" name="contact_form" action="signup-form-handler.php">


    <!-- Personlige oplysninger -->
    <div class="next">

        <h3>Personlige oplysninger<a class="info" href="#" title="Testing"><img style="height: 15px; width: 15px; margin-left: 5px;" alt="Time" src="img/signup_form/info_simple.png"></a></h3>


        <b>Mit navn er</b>
        <input class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" type="text" maxlength="60" />
        <input class="user_field" placeholder="efternavn" id="LastName" name="LastName" type="text" maxlength="60" />
        </br>


        <b>og bor i</b>
        <input class="user_field" placeholder="indsæt by" id="City" name="City" type="text" maxlength="60" />
        <b>,</b>
        <input class="user_field" placeholder="indsæt land" id="Country" name="Country" type="text" maxlength="60" />
        </br>

        </br>
        <b>Beskriv dig selv med 4 interesser</b>
        </br>
        <input id="Int1" name="Int1" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 1" />
        <input id="Int2" name="Int2" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 2" />
        <input class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 3" />
        <input class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 4" />

    </div>

从我的代码示例中可以看出,我正在尝试从我的html表单中获取输入。我已经评论了问题发生的代码。如果我从Int1和Int2检索输入,我得到答案,需要填写所有必填字段,但我不想要求Int1和Int2。另外我没有在开头添加Int1和Int2,我检查其他变量是否为空。 如果我删除Int1和Int2它一切正常。

当我在插入所有输入后发送表单时,我收到以下消息: “错误:所有字段都是必需的”

我做错了什么?

希望我提供足够的信息来找到问题,谢谢!

3 个答案:

答案 0 :(得分:1)

默认方法是GET。将表单方法设置为POST:

<form id="contact_form" name="contact_form" action="signup-form-handler.php" method="POST">

所以基本上你没有将值传递给脚本。如果您想使用GET方法,请将$ _POST的所有实例切换为$ _GET,您可以保留表单。

答案 1 :(得分:0)

在输入Int1和Int2中,缺少属性 类型

答案 2 :(得分:0)

好的,对于初学者来说,&#34;&lt; b&gt;&#34;对于粗体标签进行删除,使用&#34;&lt;强大&gt; &LT; / strong&gt; &#34;

如果你将它添加到你的php页面它可以工作,玩那个表格虽然因为我无法理解丹麦语知道什么是什么。这是一个单页解决方案,不需要HTML文件

<?php
$form = '<form id="contact_form" name="contact_form" action="'.$_SERVER['PHP_SELF'].'" method="post" >


<!-- Personlige oplysninger -->


    <h3>Personlige oplysninger<a class="info" href="#" title="Testing"><img style="height: 15px; width: 15px; margin-left: 5px;" alt="Time" src="img/signup_form/info_simple.png"></a></h3>


    <strong>Mit navn er</strong>
    <input class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" type="text" maxlength="60">
    <input class="user_field" placeholder="efternavn" id="LastName" name="LastName" type="text" maxlength="60" >
    <input type="text" class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" >
    <strong>og bor i</strong>
    <input class="user_field" placeholder="indsæt by" id="City" name="City" type="text" maxlength="60" >
    <strong>,</strong>
    <input class="user_field" placeholder="indsæt land" id="Country" name="Country" type="text" maxlength="60" >
    <strong>Beskriv dig selv med 4 interesser</strong>
    <input type="text" id="Int1" name="Int1" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 1" >
    <input type="text" id="Int2" name="Int2" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 2" >

    <input  type="text" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 3" >

    <input type="text" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 4" >
    <input type="submit" name="submit" value="Enter">
</form>';
if (isset ($_POST['submit'])){
$errors = '';
$myemail = 'example@domain.com';//<-----Put Your email address here.
if(empty($_POST['FirstName'])  || 
empty($_POST['LastName'])  || 
empty($_POST['City'])  || 
empty($_POST['Country']))
{
$errors .= "\n Error: all fields are required";
$display = $errors.$form;
}else{
$FirstName = $_POST['FirstName']; 
$LastName = $_POST['LastName'];
$City = $_POST['City'];
$Country = $_POST['Country'];
$Int1 = $_POST['Int1'];       // <---- Here's what seems to be the issue
$Int2 = $_POST['Int2'];       // <---- Here's what seems to be the issue 

$to = $myemail; 
$email_subject = "Udfyldelse af test på Gamification: $FirstName";
$email_body = "Du har fået en ny besvarelse. ".
"Her er besvarelserne:\n Fornavn: $FirstName \n Efternavn: $LastName \n By: $City \n Land $Country \n".
"De fire personlige interesser: $Int1 og $Int2"; 

$headers = "From: $myemail\n"; 

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
}else{
$display = $errors.$form;   
}

echo $display;
?>