douzong2206 2014-05-02 12:54
浏览 128
已采纳

PHP中未定义的变量[关闭]

I realized that not only was i trying to make things more complicated for myself I was also working in an external sheet. So when I defined, action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" The form was looking on the page for the script. Therefore could not locate the variables. Additionally, some of you pointed out case sensitive errors, which I have also addressed. The complete script now works like a charm, so i thank you again for your help. :)

PHP Solution

<?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
";
$headers .= "Content-type: text/html
";
$success = mail($webMaster,$emailSubject, $body, $headers);
?>

HTML Solution " method="POST">

    <!--/ 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条回答 默认 最新

  • dougong2306 2014-05-02 13:07
    关注

    in your html code, you are using php variables, they are not defined when html form is loaded. so you are getting errors saying undefined variables.

    if you dont want to see those errors simply use this

    ini_set('display_errors', 0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大