用PHP发送html表单问题

时间:2014-07-17 22:53:02

标签: php html forms

我从网上复制了这个PHP和HTML,并添加了几个变量......我似乎无法让它工作(该网站托管在JustHost - Linux上)。

我用谷歌搜索并阅读了我发现的所有内容,并尝试了所有内容。似乎没有什么对我有用。我想我一定要过分简单看看?

我知道那里有很多关于此的内容,但是非常感谢任何帮助!

php.ini有:

SMTP = localhost

smtp_port = 25

以下是错误,PHP和HTML表单。

错误:

"我们非常抱歉,但您提交的表单中发现了错误。这些错误显示在下面。

很抱歉,您提交的表单似乎有问题。

请返回并修正这些错误。"

PHP:

        <?php

        if(isset($_POST['email'])) {



            // EDIT THE 2 LINES BELOW AS REQUIRED

            $email_to = "jeff@selectionsllc.com";

            $email_subject = "I'm Interested In This Line!";





            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['first_name']) ||

                !isset($_POST['last_name']) ||

                !isset($_POST['store_name']) ||

                !isset($_POST['email']) ||

                !isset($_POST['telephone']) ||

                !isset($_POST['line']) ||

                !isset($_POST['comments'])) {

                died('We are sorry, but there appears to be a problem with the form you submitted.');

            }



            $first_name = $_POST['first_name']; // required

            $last_name = $_POST['last_name']; // required

            $store_name = $_POST['store_name']; // required

            $email_from = $_POST['email']; // required

            $telephone = $_POST['telephone']; // not required

            $line = $_POST['line']; // not required

            $comments = $_POST['comments']; // 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(!preg_match($string_exp,$store_name)) {

            $error_message .= 'The Store 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.\n\n";



            function clean_string($string) {

            $bad = array("content-type","bcc:","to:","cc:","href");

            return str_replace($bad,"",$string);

            }



            $email_message .= "First Name: ".clean_string($first_name)."\n";

            $email_message .= "Last Name: ".clean_string($last_name)."\n";

            $email_message .= "Store Name: ".clean_string($store_name)."\n";

            $email_message .= "Email: ".clean_string($email_from)."\n";

            $email_message .= "Telephone: ".clean_string($telephone)."\n";

            $email_message .= "Line: ".clean_string($line)."\n";

            $email_message .= "Comments: ".clean_string($comments)."\n";





        // create email headers

        $headers = 'From: '.$email_from."\r\n".

        'Reply-To: '.$email_from."\r\n" .

        'X-Mailer: PHP/' . phpversion();

        mail($email_to, $email_subject, $email_message, $headers);  

        ?>



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



        <?php

        }

        ?>

HTML表格:

  <form method="post" action="send_form_email.php" name="Inquiry" target="_new">
    <table width="450px">
      <tr>
        <td valign="top"><label for="first_name">First Name *</label></td>
        <td valign="top"><input  type="text" name="first_name" maxlength="50" size="30"></td>
      </tr>
      <tr>
        <td valign="top"><label for="last_name">Last Name *</label></td>
        <td valign="top"><input  type="text" name="last_name" maxlength="50" size="30"></td>
      </tr>
      <tr>
        <td valign="top"><label for="store_name">Store Name *</label></td>
        <td valign="top"><input  type="text" name="store_name" maxlength="50" size="30"></td>
      </tr>
      <tr>
        <td valign="top"><label for="email">Email Address *</label></td>
        <td valign="top"><input  type="text" name="email" maxlength="80" size="30"></td>
      </tr>
      <tr>
        <td valign="top"><label for="telephone">Telephone Number</label></td>
        <td valign="top"><input type="tel" name="telephone" pattern="^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$" required size="30" placeholder="555-555-5555"></td>
      </tr>
      <tr>
        <td valign="top"><label for="line">Line Name</label></td>
        <td valign="top"><input  type="text" name="line" value="JStar Jewelry Designs" maxlength="30" size="30" disabled></td>
      </tr>
        <td valign="top"><label for="comments">Comments *</label></td>
        <td valign="top"><textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea></td>
      </tr>
      <tr>
        <td colspan="2" style="text-align:center"><input name="submit" type="submit" id="submit" value="Submit"></td>
      </tr>
    </table>
  </form>

1 个答案:

答案 0 :(得分:0)

所有字段都被认为是必需的(在php文件中,您正在验证您正在接收的任何POST变量已设置)但看起来您没有将输入“telephone”和“line”视为强制性的HTML代码。如果填写表单中的所有输入,您是否尝试过会发生什么?

如果“line”和“telephone”是可选的,你应该在php上更改这些行:

        if(!isset($_POST['first_name']) ||

            !isset($_POST['last_name']) ||

            !isset($_POST['store_name']) ||

            !isset($_POST['email']) ||

            !isset($_POST['telephone']) ||

            !isset($_POST['line']) ||

            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');

        }

为此:

        if(!isset($_POST['first_name']) ||

            !isset($_POST['last_name']) ||

            !isset($_POST['store_name']) ||

            !isset($_POST['email']) ||

            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');

        }