dongwupei7803 2019-04-29 02:08
浏览 11

如何更正此PHP代码,以便发送电子邮件而不生成错误代码[重复]

This question already has an answer here:

I am trying to create a HTML form able to send an email with job application information to my email address so I can respond to perspective applicants. I will provide the form code as well as the PHP Code, when I click the submit button I want an email to be sent to email email address with the information the user entered, however I am getting error messages, however I believe I have checked for the most common erros and none of these are showing up so I am guessing there is some sort of logic error somehwere in my code:

Here is the form code:

<div class = "form-group purple">
                    <form name = "applyhere" method = "post" action = "jobs.php">
                        <label for = "fullname">Enter Name Here:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "name">
                        </center>
                        <label for = "streetaddress">Street Address:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "address">
                        </center>
                        <label for = "city">City:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "city">
                        </center>
                        <label for = "state">State:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "state">
                        </center>
                        <label for "zip">Zip Code:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "zip" maxlength = "5">
                        </center>
                        <label for = "phone">Phone Number:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "phonenumber">
                        </center>
                        <label for = "timetocall">Best Time to Contact:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "timetocall">
                        </center>
                        <label for = "email">Email Address:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "email">
                        </center>
                        <label for = "eligible">Are you Eligible to Work In United States:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "eligible">
                        </center>
                        <label for = "speciality">Speciality:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "speciality">
                        </center>
                        <label for = "yearsexp">Years of Experience:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "yearexp">
                        </center>
                        <label for = "workexp">Work Experience:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "workexp">
                        </center>
                        <label for = "aboutyou">Tell Us About Yourself:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "aboutyou"></textarea>
                        </center>
                        <label for = "education">Enter Education History College/Years Attended/Major:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "education"></textarea>
                        </center>
                        <label for = "tradeschool">Enter Trade School History: School Attended/Years Attended/Major:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "tradeschool"></textarea>
                        </center>
                        <label for = "jobtitle">Job Title Applying For:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "jobtitle">
                        </center>
                        <label for = "availability">Enter Job Availability:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "availability">
                        </center>
                        <label for = "startdate">When Can you Start Working:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "startdate">
                        </center>
                        <label for = "workhistory">Enter Your Work History Here<i>Names,
                            Addresses, Phone Number of Previous Employee, Supervisors Name,
                            Dates of Employment, Reason for Leaving</i>:</label>
                        <center>
                            <textarea class = "TxtCustomForm" rows = "5" name = "workhistory"></textarea>
                        </center>
                        <label for = "reference1">Enter First Reference (Name / Phone Number / Job Title / Relationship:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "reference1">
                        </center>
                        <label for = "reference2">Enter Second Reference (Name / Phone Number / Job Title / Relationship:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "reference2">
                        </center>
                        <label for = "reference3">Enter Third Reference (Name / Phone Number / Job Title / Relationship:</label>
                        <center>
                            <input class = "TxtCustomForm" type = "text" name = "reference3">
                        </center>
                        <center>
                            <input class = "TxtCustomForm BtnSubmit" type = "submit" name = "Submit Form">
                        </center>
                    </form>
                </div>

And Here is my PHP Code that cleans the answers and should send them in an email to me:

<?php

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "*****.***.****@gmail.com";
    $email_subject = "Job Application";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['fullname']) ||
        !isset($_POST['address']) ||
        !isset($_POST['city']) ||
        !isset($_POST['state']) ||
        !isset($_POST['zip'])  ||
        !isset($_POST['phone'])  ||
        !isset($_POST['timetocall'])  ||
        !isset($_POST['email'])  ||
        !isset($_POST['eligible'])  ||
        !isset($_POST['speciality'])  ||
        !isset($_POST['yearexp'])  ||
        !isset($_POST['workexp'])  ||
        !isset($_POST['aboutyou']) ||
        !isset($_POST['education']) ||
        !isset($_POST['tradeschool']) ||
        !isset($_POST['jobtitle']) ||
        !isset($_POST['availability']) ||
        !isset($_POST['startdate']) ||
        !isset($_POST['workhistory']) ||
        !isset($_POST['reference1']) ||
        !isset($_POST['reference2']) ||
        !isset($_POST['reference3'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $name = $_POST['name']; // required
    $address = $_POST['address']; // required
    $city = $_POST['city']; // required
    $state = $_POST['state']; // not required
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $timetocall = $_POST['timetocall'];
    $email = $_POST['email'];
    $eligible = $_POST['eligible'];
    $speciality = $_POST['speciality'];
    $yearsexp = $_POST['yearsexp'];
    $workexp = $_POST['workexp'];
    $aboutyou = $_POST['aboutyou'];
    $education = $_POST['education'];
    $tradeschool = $_POST['tradeschool'];
    $jobtitle = $_POST['jobtitle'];
    $availability = $_POST['availability'];
    $startdate = $_POST['startdate'];
    $workhistory = $_POST['workhistory'];
    $ref1 = $_POST['reference1'];
    $ref2 = $_POST['reference2'];
    $ref3 = $_POST['reference3'];

     // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.

";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "Name: ".clean_string($name)."
";
    $email_message .= "Address: ".clean_string($address)."
";
    $email_message .= "City: ".clean_string($city)."
";
    $email_message .= "State: ".clean_string($state)."
";
    $email_message .= "Zip: ".clean_string($zip)."
";
    $email_message .= "Phone: ".clean_string($phone)."
";
    $email_message .= "When To Call: ".clean_string($timetocall)."
";
    $email_message .= "Email: ".clean_string($email)."
";
    $email_message .= "Work In US?: ".clean_string($eligible)."
";
    $email_message .= "Speciality: ".clean_string($speciality)."
";
    $email_message .= "Years Exp: ".clean_string($yearexp)."
";
    $email_message .= "Work Exp: ".clean_string($workexp)."
";
    $email_message .= "Education: ".clean_string($education)."
";
    $email_message .= "Trade School: ".clean_string($tradeschool)."
";
    $email_message .= "Job Applied For: ".clean_string($jobtitle)."
";
    $email_message .= "Availability: ".clean_string($availability)."
";
    $email_message .= "Start Date: ".clean_string($startdate)."
";
    $email_message .= "workhistory: ".clean_string($workhistory)."
";
    $email_message .= "Reference 1: ".clean_string($ref1)."
";
    $email_message .= "Reference 2: ".clean_string($ref2)."
";
    $email_message .= "Reference 3: ".clean_string($ref3)."
";

// create email headers
$headers = 'From: '.$email."
".
'Reply-To: '.$email."
" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

?>

PLEASE NOTE, I AM NEW TO PHP TYPICALLY ONLY CODE STATIC WEBPAGES AND I AM UNSURE OF MY NEXT STEPS.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类