使用PHP表单提供的电子邮件中的字段为空

时间:2014-08-22 17:58:28

标签: php forms

如果这是一个简单的解决方案,我提前道歉,因为我对学习PHP有些新意。我找到了相关问题并尝试了这些答案,但没有一个适用于我的问题。

我收到了一封来自我的联系表格的电子邮件,其中一些字段显示,一些字段不显示。在地址字段中,我只收到国家/地区,而对于可用日期字段,我只收到年份。这些是当前的问题。我想也许它可能是输入id,但现在只有那两个给我一个问题。

我真诚地感谢您提供的任何帮助。

PHP:

    <?php

$myemail  = "myemail@gmail.com";
$subject = "Employment Application Submission from Your Website";

/* Check all form inputs using check_input function */
$position   = check_input($_POST['position']);
$refer    = check_input($_POST['refer']);
$name = check_input($_POST['name'], "Enter your name");
$address    = check_input($_POST['address']);
$phone    = check_input($_POST['phone']);
$email    = check_input($_POST['email']);
$dateavail    = check_input($_POST['dateavail']);
$social    = check_input($_POST['social']);
$dob    = check_input($_POST['dob']);


/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}


/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Position Applied For: $position
Referred by: $refer
Name: $name
Address: $address 
Phone: $phone
E-mail: $email
Date available: $dateavail
SSN: $social
DOB: $dob

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>

   <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>

<?php
exit();
}
?>

HTML:

<form id="form_884913" method="post" action="send_form_email1.php" class="appnitro">



            <ul >

                    <li id="li_1" >
        <label class="description" for="position">Position Applied For </label>
        <div>
            <input id="element_1" name="position" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_2" >
        <label class="description" for="refer">How were you referred to us? </label>
        <div>
            <input id="element_2" name="refer" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_3" >
        <label class="description" for="name">Full Name (first, middle, last) </label>
        <div>
            <input id="element_3" name="name" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_4" >
        <label class="description" for="address">Address </label>

        <div>
            <input id="element_4_1" name="address" class="element text large" value="" type="text">
            <label for="address" style="font-size: 12px;">Street Address</label>
        </div>

        <div>
            <input id="element_4_2" name="address" class="element text large" value="" type="text">
            <label for="address" style="font-size: 12px;">Address Line 2</label>
        </div>

        <div class="left">
            <input id="element_4_3" name="address" class="element text medium" value="" type="text">
            <label for="address" style="font-size: 12px;">City</label>
        </div>

        <div class="right">
            <input id="element_4_4" name="address" class="element text medium" value="" type="text">
            <label for="address" style="font-size: 12px;">State / Province / Region</label>
        </div>

        <div class="left">
            <input id="element_4_5" name="address" class="element text medium" maxlength="15" value="" type="text">
            <label for="address" style="font-size: 12px;">Postal / Zip Code</label>
        </div>

        <div class="right">
            <select class="element select medium" id="element_4_6" name="address"> 
            <option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
            </select>
        <label for="address" style="font-size: 12px;">Country</label>
    </div> 
        </li>       <li id="li_5" >
        <label class="description" for="phone">Phone </label>
        <span>
            <input id="element_5_1" name="phone" class="element text" size="3" maxlength="3" value="" type="text"> -
            <label for="element_5_1">(###)</label>
        </span>
        <span>
            <input id="element_5_2" name="phone" class="element text" size="3" maxlength="3" value="" type="text"> -
            <label for="element_5_2">###</label>
        </span>
        <span>
            <input id="element_5_3" name="phone" class="element text" size="4" maxlength="4" value="" type="text">
            <label for="element_5_3">####</label>
        </span>

        </li>       <li id="li_6" >
        <label class="description" for="email">E-Mail Address </label>
        <div>
            <input id="element_6" name="email" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_7" >
        <label class="description" for="dateavail">Date Available to Start </label>
        <span>
            <input id="element_7_1" name="dateavail" class="element text" size="2" maxlength="2" value="" type="text"> /
            <label for="dateavail">MM</label>
        </span>
        <span>
            <input id="element_7_2" name="dateavail" class="element text" size="2" maxlength="2" value="" type="text"> /
            <label for="dateavail">DD</label>
        </span>
        <span>
            <input id="element_7_3" name="dateavail" class="element text" size="4" maxlength="4" value="" type="text">
            <label for="dateavail">YYYY</label>
        </span>

        <span id="calendar_7">
            <img id="cal_img_7" class="datepicker" src="calendar.gif" alt="Pick a date.">   
        </span>
        <script type="text/javascript">
            Calendar.setup({
            inputField   : "element_7_3",
            baseField    : "element_7",
            displayArea  : "calendar_7",
            button       : "cal_img_7",
            ifFormat     : "%B %e, %Y",
            onSelect     : selectDate
            });
        </script>

        </li>       <li id="li_8" >
        <label class="description" for="social">Social Security Number </label>
        <div>
            <input id="element_8" name="social" class="element text small" type="text" maxlength="255" value=""/> 
        </div> 

(我没有为长度目的显示整个表格)

3 个答案:

答案 0 :(得分:1)

您有几个输入具有相同的name。您需要为每个输入元素指定唯一的名称。如果您有多个具有相同名称的元素,则只会将最后一个元素发送到服务器。

一个简单的解决方案:

更改您的PHP:

$address = check_input($_POST['address']." ".$_POST['address2']." ".$_POST['address3']." ".$_POST['address4']." ".$_POST['address5']);

更改您的HTML:

<div>
    <input id="element_4_1" name="address" class="element text large" value="" type="text">
    <label for="address" style="font-size: 12px;">Street Address</label>
</div>

<div>
    <input id="element_4_2" name="address2" class="element text large" value="" type="text">
    <label for="address2" style="font-size: 12px;">Address Line 2</label>
</div>

<div class="left">
    <input id="element_4_3" name="address3" class="element text medium" value="" type="text">
    <label for="address3" style="font-size: 12px;">City</label>
</div>

<div class="right">
    <input id="element_4_4" name="address4" class="element text medium" value="" type="text">
    <label for="address4" style="font-size: 12px;">State / Province / Region</label>
</div>

<div class="left">
    <input id="element_4_5" name="address5" class="element text medium" maxlength="15" value="" type="text">
    <label for="address5" style="font-size: 12px;">Postal / Zip Code</label>
</div>

答案 1 :(得分:0)

您有多个<input>具有相同的名称:

<input id="element_4_1" name="address" class="element text large" value="" type="text">
<input id="element_4_2" name="address" class="element text large" value="" type="text">

由于name显示为$_POST中的关键字,因此您基本上会使用表单中显示的最后name="address"覆盖所有输入。

每个输入必须具有唯一名称。您已经在$foo = $_POST[...]中使用了哪些内容,那么为什么您的表单中没有匹配的名称是个谜。

答案 2 :(得分:0)

您对输入字段使用相同的名称..在输入字段中为名称属性指定不同的名称。

 <input id="element_4_1" name="streetaddress" class="element text medium" value="" type="text">
  <input id="element_4_2" name="addressline2" class="element text medium" value="" type="text">

在php中接收..一个接一个。示例

$doorNo = $_POST['streetaddress'];
$streetName= $_POST['addressline2'];

对于所有领域都是明智的..同样适用于所有其他问题。