只有电子邮件字段的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。尝试为表单操作属性提供完整的网址,即网站网址 ex site_url/页面。

<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>'
?>