繁体   English   中英

仅包含电子邮件字段的PHP表单不会发送,而完整表单可以正常工作

[英]PHP Form with only E-mail field won't send, while full form works fine

因此,我设法获得了一个包含名称,电子邮件和消息的表格,现在我正试图为另一个“敬请期待”的功能制作另一个表格,但是我无法一辈子使用它。 我希望有人可以指出(可能)公然的错误。

HTML:

<fieldset>
                                            <form class="subscriptionForm" method="post" action="mail.php">
                                                <input name="email" id="subscriptionForm" class="inputForm" type="text" value="Your Email" onFocus="if (this.value=='Your Email') this.value=''" onBlur="if (this.value==''){this.value='Enter Your Email Address'}" />
                                                <input name="submit" type="submit" id="submitButton" class="transition" value="Send">
                                            </form>
                                        </fieldset>

PHP:

   <?php
    $email = $_POST['email'];
    $from = 'From: Studio Westend'; 
    $to = 'danny@studiowestend.de'; 
    $subject = 'Studio Westend Mailing List';

    $body = "From: E-Mail: $email\n"; 

    if (mail ($to, $subject, $body, $from, "-f danny@studiowestend.de"))  
    echo '<p>Your E-mail has been added!</p>'
?>

为了比较其他形式的代码:

HTML:

<form method="post" action="contact.php" name="contactform" id="contactform">
                                                <input name="name" type="text" id="name"  onFocus="if(this.value == 'Name') { this.value = ''; }" onBlur="if(this.value == '') { this.value = 'Name'; }" value="Name" >
                                                <input name="email" type="text" id="email" onFocus="if(this.value == 'E-mail') { this.value = ''; }" onBlur="if(this.value == '') { this.value = 'E-mail'; }" value="E-mail" >            
                                                <textarea name="message"  id="message" onFocus="if(this.value == 'Message') { this.value = ''; }" onBlur="if(this.value == '') { this.value = 'Message'; }" >Message</textarea>                                     
                                                <input type="submit" class="send_message transition" id="submit" value="Send Message" />
                                            </form>

PHP:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Studio Westend'; 
    $to = 'rent@studiowestend.de'; 
    $subject = 'Studio Westend Rent Form';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 

    if (mail ($to, $subject, $body, $from, "-f rent@studiowestend.de"))  
    echo '<p>Your message has been sent!</p>'
?>
                                    <form class="subscriptionForm" method="post" action="mail.php">
                                        <input name="email" id="subscriptionForm" class="inputForm" type="text" value="Your Email" onFocus="if (this.value=='Your Email') this.value=''" onBlur="if (this.value==''){this.value='Enter Your Email Address'}" />
                                        <input name="form1" type="submit" id="submitButton" class="transition" value="Send">
                                    </form>
                                </fieldset>


<?php
    if(isset($_POST['form1'])){
    $email = $_POST['email'];
    $from = 'From: Studio Westend'; 
    $to = 'danny@studiowestend.de'; 
    $subject = 'Studio Westend Mailing List';

    $body = "From: E-Mail: $email\n"; 

    if (mail ($to, $subject, $body, $from, "-f danny@studiowestend.de"))  
    echo '<p>Your E-mail has been added!</p>'
    }
?>

============其他形式========

<form method="post" action="contact.php" name="contactform" id="contactform">
                                                <input name="name" type="text" id="name"  onFocus="if(this.value == 'Name') { this.value = ''; }" onBlur="if(this.value == '') { this.value = 'Name'; }" value="Name" >
                                                <input name="email" type="text" id="email" onFocus="if(this.value == 'E-mail') { this.value = ''; }" onBlur="if(this.value == '') { this.value = 'E-mail'; }" value="E-mail" >            
                                                <textarea name="message"  id="message" onFocus="if(this.value == 'Message') { this.value = ''; }" onBlur="if(this.value == '') { this.value = 'Message'; }" >Message</textarea>                                     
                                                <input type="submit" name="form2" class="send_message transition" id="submit" value="Send Message" />
                                            </form>
<?php
    if(isset($_POST['form2'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Studio Westend'; 
    $to = 'rent@studiowestend.de'; 
    $subject = 'Studio Westend Rent Form';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 

    if (mail ($to, $subject, $body, $from, "-f rent@studiowestend.de"))  
    echo '<p>Your message has been sent!</p>'
    }
?>

注意:您的代码不安全。 注意2:您应为每个表单的提交按钮指定一个不同的名称,并应使用if()确定按钮是否被单击。 这应该工作。

同一页面中没有2个字段应具有相同的名称和ID。 尝试为表单操作属性提供完整的url,即使用site url(例如site_url / page)。

<fieldset>
    <form class="subscriptionForm" method="post" action="mail.php">
        <input name="email" id="subscriptionForm" class="inputForm" type="text" 
              value="Your Email" 
              onFocus="if (this.value=='Your Email') this.value=''" 
              onBlur="if (this.value==''){this.value='Enter Your Email Address'}" />
        <input name="submit" type="submit" id="submitButton" class="transition" value="Send">
    </form>
</fieldset>


 <?php
    $email = $_POST['email'];
    $from = 'From: Studio Westend'; 
    $to = 'danny@studiowestend.de'; 
    $subject = 'Studio Westend Mailing List';
    $body = "From: E-Mail: $email\n"; 
    if (mail ($to, $subject, $body, $from, "-f danny@studiowestend.de"))  
    echo '<p>Your E-mail has been added!</p>'
?>

<form method="post" action="contact.php" name="contactform" id="contactform">
     <input name="name" type="text" id="name"  
            onFocus="if(this.value == 'Name') { this.value = ''; }" 
            onBlur="if(this.value == '') { this.value = 'Name'; }" value="Name" >
     <input name="contact_email" type="text" id="contact_email" 
            onFocus="if(this.value == 'E-mail') { this.value = ''; }" 
            onBlur="if(this.value == '') { this.value = 'E-mail'; }" value="E-mail" >            
     <textarea name="message"  id="message" 
            onFocus="if(this.value == 'Message') { this.value = ''; }" 
            onBlur="if(this.value == '') { this.value = 'Message'; }" >Message</textarea>                                     
     <input type="submit" class="send_message transition" id="submit" value="Send Message" />
</form>

<?php
    $name = $_POST['name'];
    $email = $_POST['contact_email'];
    $message = $_POST['message'];
    $from = 'From: Studio Westend'; 
    $to = 'rent@studiowestend.de'; 
    $subject = 'Studio Westend Rent Form';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 
    if (mail ($to, $subject, $body, $from, "-f rent@studiowestend.de"))  
    echo '<p>Your message has been sent!</p>'
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM