简体   繁体   中英

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

So I managed to get a form with name, email and a message working and now Im trying to make another one for a sort-of "Stay tuned" function, but I can't get it to work for the life of me. I hope someone can point out the (probably) blatant mistake.

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

And for comparison the code of the other form:

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

============Other form========

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

Note: Your code is not safe. Note2: You should give to every form's submit button a different name, and you should use if() to determine if the button is clicked. This should work.

No 2 fields in the same page should have the same name and id. Try giving full url for form action attribute,ie with site url ex 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>'
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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