PHP中未定义的变量

时间:2014-05-02 12:54:26

标签: php html

我意识到我不仅要为自己制造更复杂的东西,而且还在外部工作表中工作。因此,当我定义时,action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"表单正在页面上查找脚本。因此找不到变量。此外,你们中的一些人指出了区分大小写的错误,我也提到了这些错误。完整的脚本现在就像一个魅力,所以我再次感谢你的帮助。 :)

PHP解决方案

<?php
//*--/ variables /--*//
$emailSubject = 'Email Title';
$webMaster = 'php.test@outlook.com';

//*--/ define variables and set to empty values /--*//
$first_nameErr = $last_nameErr = $email_fieldErr = $tel_fieldErr = $select_optionErr = $enquiry_areaErr = "";
$first_name = $last_name = $email_field = $tel_field = $select_option = $enquiry_area = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
 $first_nameErr = "First name is a required field.";
} else {
 $first_name = test_input($_POST["firstname"]);
}

if (empty($_POST["lastname"])) {
 $last_nameErr = "Last name is a required field.";
} else {
 $last_name = test_input($_POST["lastname"]);
}

if (empty($_POST["email"])) {
 $email_fieldErr = "E-mail is a required field.";
} else {
 $email_field = test_input($_POST["email"]);
}

if (empty($_POST["tel"])) {
 $tel_fieldErr = "Tel No. is a required field.";
} else {
 $tel_field = test_input($_POST["tel"]);
}

if (empty($_POST["select"])) {
 $select_option= "";
} else {
 $select_option = test_input($_POST["select"]);
}

if (empty($_POST["enquiry"])) {
 $enquiry_areaErr = "Enquiry is a required field.";
} else {
 $enquiry_area = test_input($_POST["enquiry"]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$body = <<<EOD
<br><hr><br>
First Name: $first_name <br>
Last Name: $last_name <br>
Email: $email_field <br>
Tel: $tel_field <br>
I heard about you from; $select_option <br>
Enquiry: $enquiry_area <br> 
EOD;

$headers = "From: $email_field\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster,$emailSubject, $body, $headers);
?>

HTML解决方案     &#34;方法=&#34; POST&#34;&GT;     

    <!--/ form left /-->
    <div id="form-left">
        <input type="text" name="first_name" placeholder="First Name" maxlength="64"><span class="error">* <?php echo $first_nameErr; ?></span><br>
        <input type="text" name="last_name" placeholder="Last Name" maxlength="64"><span class="error">* <?php echo $last_nameErr; ?></span><br>
        <input type="text" name="email" placeholder="JohnDoe@example.com" maxlength="128"><span class="error">* <?php echo $email_fieldErr; ?></span><br>
        <input type="text" name="tel" placeholder="Tel No." maxlength="16"><span class="error">* <?php echo $tel_fieldErr; ?></span><br>
        <input type="reset">
    </div>

    <!--/ form right /-->
    <div id="form-right">
        <select>
            <option value="where" name="select" selected>How did you find us? ▼</option>
            <option value="facebook">Facbook</option>
            <option value="twitter">Twitter</option>
            <option value="event">An Event</option>
            <option value="friend">A Friend</option>
            <option value="partner">A Partner</option>
            <option value="other">Other</option>
        </select> <br>
        <textarea name="enquiry" placeholder="Have an enquiry?" maxlength="750"></textarea><span class="error">* <?php echo $enquiry_areaErr; ?></span>
    </div>
    <div id="form-submit">
    <input type="submit" onClick="alert('Thank you, your enquiry has been recieved.')">
    </div>
    </div>
    </form>

3 个答案:

答案 0 :(得分:1)

定义了非错误变量。如果检查PHP,只有在有POST操作时,变量才会得到一个值。

只需在PHP页面顶部定义每个变量,它就可以正常工作。

答案 1 :(得分:0)

在你的html代码中,你使用的是php变量,在加载html表单时没有定义它们。 所以你得到的错误是未定义的变量。

如果您不想看到这些错误,只需使用此

ini_set('display_errors', 0);

答案 2 :(得分:0)

您不能在PHP代码中使用好的变量。您已从html表单中影响了变量的值。 ----代码错误 $ body =&lt;&lt;



名字:$ first_Name
姓氏:$ last_Name
电子邮件:$ email_Field
电话:$ tel_Field
我听说过你; $ select_Option
查询:$ enquiry_Field
EOD;

----替换为 $ body =&lt;&lt;



名字:$ firstField
姓氏:$ lastField
电子邮件:$ emailField
电话:$ telField
我听说过你; $ selectField
查询:$ enquiryField
EOD;