0

我在一个 HTML 页面中有 2 个表单。一个表单发布到数据库,另一个表单发送一封电子邮件,其中包含表单中的详细信息。当我想到它时,这很简单,但尝试了很长时间没有成功。

HTML:

    <ul class="slides">
        <li>
            <div id="contactFormContainer" class="container">
                <div class="left">
                    <h1 class="uppercase">do you  have project inquiry??</h1>

                    <form id="contactForm" method="POST" name="contactForm" action="">
                        <fieldset>
                            <p> 
                                <label for="fullname">Fullname</label> 
                                <input name="fullname" id="fullname" type="text">
                            </p>
                            <p> 
                                <label for="email">Email</label> 
                                <input name="email" id="contact_email" type="text">
                            </p>
                            <p> 
                                <label for="fullname">Subject</label> 
                                <input name="fullname" id="fullname" type="text">
                            </p>
                            <p> 
                                <label for="fullname">Project Description</label> 
                                <textarea name="message" id="message" rows="2" cols="20"></textarea>
                            </p>
                            <p> 
                                <input class="btn" id="contactSubmit" type="submit" value="Submit">
                            </p>
                        </fieldset>
                    </form>     
                </div> <!-- end left -->

                <div class="right">
                    <span class="head">Twitter Feeds</span>
                    <div class="twitter">
                        <!-- twitter feed will be generated -->
                    </div>
                </div> <!-- end right -->
            </div> <!-- end contact form container -->
        </li>

        <li>
            <div class="head container">
                <h2>coming soon...</h2>
            </div>

            <div class="body container">
                <h1 class="uppercase">Subash.com is coming with new face.</h1>
                Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Donec id elit non mi porta gravida at eget metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Cum sociis natoque penatibus et magnis dis parturient.
            </div>

            <div class="footer container">
                <p class="signup"><span class="cursive">sign up</span> ..be the first to be notified</p>

                <div class="inputbox">
                    <form accept-charset="UTF-8" action="" name="subscribeForm" id="subscribeForm" method="post">

                        <input class="email_field" id="email" name="email" placeholder="Type your email address..." type="email">
                        <input class="submit_button" type="submit">

                    </form>  
                    <div class="status">
                        <?php 
                            if(isset($status)){
                                echo '<p>' . $status . '</p>';
                            } else {
                                echo '<p class="spam">We will never spam you.</p>';
                            }
                        ?>
                    </div>
                 </div>
            </div>
        </li>
    </ul>

PHP

    <?php
require_once('includes/db.php');
require_once('includes/functions.php');
require_once('includes/validate.php');

if($_SERVER['REQUEST_METHOD'] == 'POST'){
// subscribe form
$email = trim($_POST['email']);

if(empty($email) || !valid_email($email)){
    $status = 'Please provide valid email address.';
} else {
    if(email_exist($email)){
        $status = 'You are already subscribed.';
    } else {
        add_email($email);
        $status = 'Thank you for subscribing.';
    }
}   
 }

require 'index.tmpl.php';
?> 

这段代码完全符合预期。但现在我需要提交另一个表格。我尝试在 REQUEST_METHOD 检查以下添加此代码但没有成功:

    $fullname = $_POST['fullname'];

if(!empty($fullname)){
    $status = 'sucess';
    echo $status;
} else {
    echo $fullname;
}

我也尝试过:

if (!empty($_POST['contactForm'])) {
   //do something here;
}

if (!empty($_POST['subscribeForm'])) {
   //do something here;
}

有什么我想念的吗?

4

3 回答 3

1

fullname您为和字段使用了相同的 id 和名称subject,这可能会导致一些问题......

于 2012-07-16T12:12:38.143 回答
0

为提交按钮使用名称属性,如果单击了按钮,请检查 php。

<input class="btn" id="contactSubmit" name ="contactSubmit"
type="submit" value="Submit">

<input class="submit_button" name="submitEmail" id="submitEmail" type="submit">

然后在 php

if(isset($_REQUEST['contactSubmit']))
{
       // contact submit clicked do action based on that
} 
if(isset($_REQUEST['submitEmail']))
{
    // Send email
} 
于 2012-07-16T12:11:39.583 回答
0

仅发送表单(以及与其关联的值)。所以你不能用正常的方式来做。

您可能需要一些 JavaScript 来“单击”另一个提交按钮。

还要检查这个问题:提交多个表格

于 2012-07-16T12:13:39.863 回答