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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度