表格没有取名字

时间:2012-10-25 14:08:10

标签: php html post html-form

我无法弄清楚为什么我的PHP代码不会从我的表单中取出名字,我希望你们能帮忙。

其他所有内容都通过电子邮件发送,只是第一个名称字段是element_1_1。

这是PHP:

$<?php
$errors = ''; 
$myemail = 'example@example.co.uk';
if( empty($_POST['element_1_1'])  ||
empty($_POST['element_1_2'])  ||
empty($_POST['element_3']) ||
empty($_POST['element_2'])  ||
empty($_POST['element_5'])  ||
empty($_POST['element_4']))

$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3']; 
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address \n";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "";
$email_body = "".

"\n First Name:   $name \n  \n Second Name:   $lastname \n  \n Email:   $email_address \n  \n Phone Number:   $number \n \n Area(s):   $area \n \n Discount:   $discount \n".


$headers = "From: example@example.co.uk\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: http://www.example.co.uk');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
echo "";
echo " \n <a href='http://www.example.com'>Go     Back</a> \n";
?>


</body>
</html>

以下是表格:

<div id="bmvform-form_container">
    <form id="form_507993" class="appnitro"  method="post" action="formsend.php">
                <div class="form_description">
        <h2>Want To Receive Our Properties?</h2>
        <p><em>At Pure Acquisitions, we respect your privacy. Any details provided will NOT be sent to any third party without your consent.</em></p>
    </div>                      
        <ul >

    <li id="li_1" >
    <label class="description" for="element_1">Name </label>
    <span>
        <input id="element_1_1" name="element_1_1" class="element text" maxlength="255" size="14" value=""/>
        <label>First</label>
    </span>
    <span>
        <input id="element_1_2" name="element_1_2" class="element text" maxlength="255" size="14" value=""/>
        <label>Last</label>
    </span><p class="guidelines" id="guide_1"><small>We don't want to be rude!</small></p> 
    </li>       <li id="li_3" >
    <label class="description" for="element_3">Email Address </label>
    <div>
        <input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/> 
    </div><p class="guidelines" id="guide_3"><small>To send you important stuff!</small></p> 
    </li>       <li id="li_2" >
    <label class="description" for="element_2">Phone Number </label>
    <div>
        <input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/> 
    </div><p class="guidelines" id="guide_2"><small>We need to let you know about our properties!</small></p> 
    </li>       <li id="li_5" >
    <label class="description" for="element_5">Locations You Are Interested In </label>
    <span>
        <input id="element_5_1" name="element_5" class="element radio" type="radio" value="All" />
        <label class="choice" for="element_5_1">All</label>
        <input id="element_5_2" name="element_5" class="element radio" type="radio" value="London" />
        <label class="choice" for="element_5_2">London</label>
        <input id="element_5_3" name="element_5" class="element radio" type="radio" value="South of England" />
        <label class="choice" for="element_5_3">South of England</label>
        <input id="element_5_4" name="element_5" class="element radio" type="radio" value="West Midlands" />
        <label class="choice" for="element_5_4">West Midlands</label>
        <input id="element_5_5" name="element_5" class="element radio" type="radio" value="East Midlands" />
        <label class="choice" for="element_5_5">East Midlands</label>
        <input id="element_5_6" name="element_5" class="element radio" type="radio" value="North West" />
        <label class="choice" for="element_5_6">North West</label>
        <input id="element_5_7" name="element_5" class="element radio" type="radio" value="North East" />
        <label class="choice" for="element_5_7">North East</label>
        <input id="element_5_8" name="element_5" class="element radio" type="radio" value="Yorkshire & Humber" />
        <label class="choice" for="element_5_8">Yorkshire & Humber</label>
        <input id="element_5_9" name="element_5" class="element radio" type="radio" value="Wales" />
        <label class="choice" for="element_5_9">Wales</label>
        <input id="element_5_10" name="element_5" class="element radio" type="radio" value="Scotland" />
        <label class="choice" for="element_5_10">Scotland</label>

    </span><p class="guidelines" id="guide_5"><small>Let us know where in the country you are looking for property.</small></p> 
    </li>       <li id="li_4" >
    <label class="description" for="element_4">Ideal Discount From Market Value </label>
    <div>
        <input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/> 
    </div><p class="guidelines" id="guide_4"><small>How much discount (in percent) are you looking for?</small></p> 
    </li>

                <li class="buttons">
            <input type="hidden" name="form_id" value="507993" />

            <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
    </li>
        </ul>
    </form> 
</div>

2 个答案:

答案 0 :(得分:3)

if( empty($_POST['element_1_1'])  ||
empty($_POST['element_1_2'])  ||
empty($_POST['element_3']) ||
empty($_POST['element_2'])  ||
empty($_POST['element_5'])  ||
empty($_POST['element_4']))

$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3']; 
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];

你错过了这里的curley括号吗?我会假设这个或类似于curley括号,取决于你想做什么。

if( empty($_POST['element_1_1'])  ||
empty($_POST['element_1_2'])  ||
empty($_POST['element_3']) ||
empty($_POST['element_2'])  ||
empty($_POST['element_5'])  ||
empty($_POST['element_4']))
{
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3']; 
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
}

你还想用if语句做什么?你正在检查其中一个是否为空,然后将它们全部设置好?或者您应该检查其中一个是否为空,然后向用户发送一条消息,其中一个字段需要填写?然后你应该把它改成这样的东西:

if( empty($_POST['element_1_1'])  ||
    empty($_POST['element_1_2'])  ||
    empty($_POST['element_3']) ||
    empty($_POST['element_2'])  ||
    empty($_POST['element_5'])  ||
    empty($_POST['element_4']))
    {
    do something;    }
else {
$name = $_POST['element_1_1'];
    $lastname = $_POST['element_1_2'];
    $email_address = $_POST['element_3']; 
    $number = $_POST['element_2'];
    $area = $_POST['element_5'];
    $discount = $_POST['element_4'];

}

答案 1 :(得分:0)

你的if语句在做什么?

if( empty($_POST['element_1_1'])  ||
empty($_POST['element_1_2'])  ||
empty($_POST['element_3']) ||
empty($_POST['element_2'])  ||
empty($_POST['element_5'])  ||
empty($_POST['element_4']))

这似乎与任何东西无关,但是如果它们都不为空,那么$ name没有设置(它是if语句之后的第一行,只有在if语句为真时才会被调用)

我认为你应该执行所有的行,这需要围绕它们的花括号。