【问题标题】:Simple Contact form issue [duplicate]简单的联系表格问题[重复]
【发布时间】:2015-12-07 03:27:46
【问题描述】:

我没有发现此联系表单存在问题。这是一个简单的电子邮件联系表格。任何帮助,将不胜感激。表单通过,但没有提交任何内容,也没有发送电子邮件。

电子邮件通过的页面上也不会显示任何响应。但显然所有的 PHP 都正确解析,因为我没有收到错误。

<h2>Contact AirKrete</h2>
<?php 
$action=$_REQUEST['action']; 
if ($action=="")    /* display the contact form */ 
{ 
?>
<form action="" enctype="multipart/form-data" method="post"><p><label><input type="checkbox" name="locate " id="locate "><strong>Locate an Installer</strong></label><strong><label><input type="checkbox" name="become" id="become">Become an Installer</label></strong></p>
    <div class="left">Name:<div>
      <input name="name" type="text" id="name" size="40" />
    </div></div>
<div class="left">Email:<div><input name="mailfrom" type="text" size="40" /></div></div>
<div class="left">Street:<div><input name="street" type="text" id="street" size="40" /></div></div>
<div class="left">City:<div><input name="city" type="text" id="city" size="40" /></div></div>
<div class="left">State:<div><input name="state" type="text" id="state" size="10" /> Zip:
  <input name="zip" type="text" id="zip" size="15" />
</div></div>
<div class="left">Country:<div><input name="country" type="text" id="country" size="40" /></div></div>
<div class="left">Phone:<div><input name="phone" type="text" id="phone" size="40" /></div></div></p>

<p>The fields above are REQUIRED<br>
<strong>How did you hear about AirKrete<span style="font-size:11.0pt; ">®</span>? <br>
Please make a selection.<span id="sprycheckbox1"><span class="checkboxRequiredMsg"></span></span></strong></p><div class="radiox"><label><input type="radio" name="Connection" value="friend" id="Connection_0">Friend</label></div>
<div class="radiox"><label><input type="radio" name="Connection" value="jobspec" id="Connection_1">Job Specification</label></div>
<div class="radiox"><label><input type="radio" name="Connection" value="hgtv" id="Connection_2">HGTV Promotion</label></div>
<div class="radiox"><label><input type="radio" name="Connection" value="web" id="Connection_3">Web Browsing</label></div>
<div class="radiox"><label><input type="radio" name="Connection" value="radio" id="Connection_4">Radio</label></div>
<div class="radiox"><label><input type="radio" name="Connection" value="tv" id="Connection_5">TV</label></div>
<div class="radiox"><label><input type="radio" name="Connection" value="other" id="Connection_6">Other</label></div></p>
<p class="clear-fix">&nbsp;</p>
<div><p><strong>Comments:</strong><br></p></div>
<p><textarea name="Comments" cols="43" rows="10" id="Comments"></textarea></p>
<p><label>If you have Attachments....</label></p>
<p><input name="file_attachment" type="file" size="30" /></p>
<p>When completed please click Send Email Button</p>
   <input type="submit" value="Send Email" />

   <input type="hidden" name="redirect" value="http://www.airkrete.com/airkrete_thankyou.php" /><p>Thank You</p>
    </form>
<?php 
}  
else                /* send the submitted data */ 
{
$locate=$_REQUEST['locate'];
$become=$_REQUEST['become'];
$mailfrom=$_REQUEST['mailfrom'];
$street=$_REQUEST['street'];
$city=$_REQUEST['city'];
$state=$_REQUEST['state'];
$zip=$_REQUEST['zip'];
$country=$_REQUEST['country'];
$phone=$_REQUEST['phone'];
$connection=$_REQUEST['Connection'];
$comments=$_REQUEST['Comments'];
$name=$_REQUEST['name']; 
if ((($locate=="")&&($become==""))||($mailfrom=="")||($street=="")||($city=="")||($state=="")||($zip=="")||($country=="")||($phone=="")||($connection=="")||($name==""))
    { 
    echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
    } 
else{   
    if($locate==""){      
        $message==$name+" from "+$street+" "+city+" "+$state+" "+$zip+" "+$country+" sent you an email via the contact form on the airkrete website. "+$name+" would like to become an installer. "+$name+" Their phone number is "+$phone+". They heard about you from: "+$connection+" And left you the following comment: "+$comments;
    }else if($become==""){
        $message==$name+" from "+$street+" "+city+" "+$state+" "+$zip+" "+$country+" sent you an email via the contact form on the airkrete website. "+$name+" would like to locate an installer. "+$name+" Their phone number is "+$phone+". They heard about you from: "+$connection+" And left you the following comment: "+$comments;
    }else{
        $message==$name+" from "+$street+" "+city+" "+$state+" "+$zip+" "+$country+" sent you an email via the contact form on the airkrete website. "+$name+" would like to become an installer and would also like to locate one. "+$name+" Their phone number is "+$phone+". They heard about you from: "+$connection+" And left you the following comment: "+$comments;
    }
    $from="From: $name<$email>\r\nReturn-path: $email"; 
    $subject="Message sent using your contact form"; 
    mail("joseph.roberts@usa.com", $subject, $message, $from); 
    echo "Email sent!"; 
    } 
}   
?> 

【问题讨论】:

  • + 在 PHP 中不连接。 php.net/manual/en/language.operators.string.phpThe first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments.
  • 我把所有的加号都改成了'.'。然而仍然没有任何变化。
  • 您没有名为 action 的元素。所以if ($action=="") 总是正确的,所以你应该总是得到 HTML 表单。如果是白页,则表示语法错误;检查您的错误日志..
  • @chris85 好吧,我添加了 但我仍然一无所获。现在,当我提交时,页面甚至不会刷新。奇怪的是,即使没有那行,我也没有收到语法错误。
  • 这不是语法错误,PHP 只是不知道你想要发生什么。下面的答案对你没有帮助? &lt;input type="hidden" name="action" value="submit"&gt; 只是 HTML 所以无论如何都不应该影响 PHP..

标签: php forms email webforms


【解决方案1】:

为您解决的问题如下:

1) 代码中没有名为“action”的元素。 将&lt;input type="submit" value="Send Email" /&gt; 更改为&lt;input type="submit" value="Send Email" name="action" /&gt;

2) + 在 PHP 中不用于连接。请改用.

3) == 不用于赋值。请改用= 即使用$message = 而不是$message ==

另外,您使用了未定义的$mail 变量。请改用 $mailfrom,您已为其分配了 email 值。

另外,元素名称中的空格 locate 也将其删除。

我也怀疑邮件功能的工作,因为您在发送邮件之前没有设置任何标题。

请查看以下链接以获取邮件功能在 PHP 中的工作原理以及它需要哪些参数的完整详细信息。

http://www.w3schools.com/php/func_mail_mail.asp

http://php.net/manual/en/function.mail.php

试试下面的代码:

<h2>Contact AirKrete</h2>
<?php 
    $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : "" ; 
    if ($action=="")    /* display the contact form */ 
    {
    ?>
        <form action="" enctype="multipart/form-data" method="post">
            <p>
                <label>
                    <input type="checkbox" name="locate" id="locate">
                    <strong>Locate an Installer</strong>
                </label>
                <strong>
                    <label>
                        <input type="checkbox" name="become" id="become">Become an Installer
                    </label>
                </strong>
            </p>
            <div class="left">
                Name:
                <div>
                    <input name="name" type="text" id="name" size="40" />
                </div>
            </div>
            <div class="left">
                Email:
                <div>
                    <input name="mailfrom" type="text" size="40" />
                </div>
            </div>
            <div class="left">
                Street:
                <div>
                    <input name="street" type="text" id="street" size="40" />
                </div>
            </div>
            <div class="left">
                City:
                <div>
                    <input name="city" type="text" id="city" size="40" />
                </div>
            </div>
            <div class="left">
                State:
                <div>
                    <input name="state" type="text" id="state" size="10" /> Zip:
                    <input name="zip" type="text" id="zip" size="15" />
                </div>
            </div>
            <div class="left">
                Country:
                <div>
                    <input name="country" type="text" id="country" size="40" />
                </div>
            </div>
            <div class="left">
                Phone:
                <div>
                    <input name="phone" type="text" id="phone" size="40" />
                </div>
            </div>
            <p>The fields above are REQUIRED<br>
                <strong>
                    How did you hear about AirKrete<span style="font-size:11.0pt; ">®</span>? <br>Please make a selection.
                    <span id="sprycheckbox1">
                        <span class="checkboxRequiredMsg"></span>
                    </span>
                </strong>
            </p>
            <div class="radiox"><label><input type="radio" name="Connection" value="friend" id="Connection_0">Friend</label></div>
            <div class="radiox"><label><input type="radio" name="Connection" value="jobspec" id="Connection_1">Job Specification</label></div>
            <div class="radiox"><label><input type="radio" name="Connection" value="hgtv" id="Connection_2">HGTV Promotion</label></div>
            <div class="radiox"><label><input type="radio" name="Connection" value="web" id="Connection_3">Web Browsing</label></div>
            <div class="radiox"><label><input type="radio" name="Connection" value="radio" id="Connection_4">Radio</label></div>
            <div class="radiox"><label><input type="radio" name="Connection" value="tv" id="Connection_5">TV</label></div>
            <div class="radiox"><label><input type="radio" name="Connection" value="other" id="Connection_6">Other</label></div></p>
            <p class="clear-fix">&nbsp;</p>
            <div><p><strong>Comments:</strong><br></p></div>
            <p><textarea name="Comments" cols="43" rows="10" id="Comments"></textarea></p>
            <p><label>If you have Attachments....</label></p>
            <p><input name="file_attachment" type="file" size="30" /></p>
            <p>When completed please click Send Email Button</p>
            <input type="submit" value="Send Email" name="action" />
            <input type="hidden" name="redirect" value="http://www.airkrete.com/airkrete_thankyou.php" /><p>Thank You</p>
        </form>
    <?php 
    } 
    else                /* send the submitted data */ 
    {
        $locate = isset($_REQUEST['locate']) ? $_REQUEST['locate'] : "" ;
        $become = isset($_REQUEST['become']) ? $_REQUEST['become'] : "" ;
        $mailfrom = $_REQUEST['mailfrom'];
        $street = $_REQUEST['street'];
        $city = $_REQUEST['city'];
        $state = $_REQUEST['state'];
        $zip = $_REQUEST['zip'];
        $country = $_REQUEST['country'];
        $phone = $_REQUEST['phone'];
        $connection = $_REQUEST['Connection'];
        $comments = $_REQUEST['Comments'];
        $name = $_REQUEST['name']; 

        if ((($locate == "") && ($become == "")) || ($mailfrom == "") || ($street == "") || ($city == "") || ($state == "") || ($zip == "") || ($country == "") || ($phone == "") || ($connection == "") || ($name == ""))
        { 
            echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
        } 
        else
        {   
            if($locate == "")
            {      
                $message = $name." from ".$street." ".$city." ".$state." ".$zip." ".$country." sent you an email via the contact form on the airkrete website. ".$name." would like to become an installer. ".$name." Their phone number is ".$phone.". They heard about you from: ".$connection." And left you the following comment: ".$comments;
            }
            else if($become == "")
            {
                $message = $name." from ".$street." ".$city." ".$state." ".$zip." ".$country." sent you an email via the contact form on the airkrete website. ".$name." would like to locate an installer. ".$name." Their phone number is ".$phone.". They heard about you from: ".$connection." And left you the following comment: ".$comments;
            }
            else
            {
                $message = $name." from ".$street." ".$city." ".$state." ".$zip." ".$country." sent you an email via the contact form on the airkrete website. ".$name." would like to become an installer and would also like to locate one. ".$name." Their phone number is ".$phone.". They heard about you from: ".$connection." And left you the following comment: ".$comments;
            }
            $from = "From: $name<$mailfrom>\r\nReturn-path: $mailfrom"; 
            $subject = "Message sent using your contact form";

            if(mail("joseph.roberts@usa.com", $subject, $message, $from)) 
                echo "Email sent!";
            else
                echo "Email not sent.";
        } 
    }   
?> 

【讨论】:

  • 为什么 OP 应该尝试以下代码?答案应该向 OP 解释什么是固定的,最好是为什么..
  • @chris85 答案已编辑.. :)
  • @Suyog 谢谢你的作品。我遇到的最后一个问题是,如果 $locate 和 $become 都等于“”,它应该只返回应该填写所有字段,但是如果它们中的任何一个等于“”,它就会返回。跨度>
  • @JRobz 您必须检查是否设置了值。请参阅编辑后的答案。我对前两个字段使用了三元运算符。将该检查添加到所有其他值。还要检查邮件功能是否已发送邮件
猜你喜欢
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2016-06-23
  • 1970-01-01
相关资源
最近更新 更多