在一个页面上验证2个表单

时间:2014-04-13 12:17:19

标签: php html forms validation

我正在尝试将2个表单合并到一个页面中,但是php将2个表单视为一个,我很难过如何将它们分开。在此先感谢您的帮助。这是表单1的html:

<div id="message">
            <h3>Message Us</h3>
            <form action="?" method="POST" onsubmit="return saveScrollPositions(this);">
                <input type="hidden" name="scrollx" id="scrollx" value="0" />
                <input type="hidden" name="scrolly" id="scrolly" value="0" />
                <div id="msgbox1">
                    <label for="msgname">Name:</label>
                    <input type="text" id="msgname" name="msgname" placeholder=" Required" />

                    <label for="msgemail">Email:</label>
                    <input type="text" id="msgemail" name="msgemail" placeholder=" Required" />

                <label for="msgphone">Phone:</label>
                <input type="text" id="msgphone" name="msgphone" placeholder="" />

                    <label for= "msgtopic">Topic:</label>
                    <select id="msgtopic" name="msgtopic">
                        <option value="general">General</option>
                        <option value="stud">Property Price Enquiry</option>
                        <option value="sale">Bull Sale Enquiry</option> 
                    </select>
                </div><!--- msg box 1 -->
                <div id="msgbox2">    
                    <textarea id="emailmessage" name="emailmessage" rows="7" colums="25" placeholder="Your Message?"></textarea>
                    <input type="submit" value="Send message" />
                    <p id="feedback"><?php echo $feedback2; ?></p>
                </div><!--- msg box 2 -->
            </form><!--- end form -->
        </div><!--- end message -->

表单1的php:

  <?php 

$to = '418@hotmail.com.au';
$subject = 'Morrison Property Group website enquiry';

$msgname = $_POST ['msgname'];
$msgemail = $_POST ['msgemail'];
$msgphone = $_POST ['msgemail'];
$msgtopic = $_POST ['msgtopic'];
$message = $_POST ['emailmessage'];

$body = <<<EMAIL

This is a message from $msgname
Topic: $msgtopic 

Message: $emailmessage

From $name
Phone: $msgphone 
Email: $msgemail

EMAIL;

$header = ' From: $msgemail ';

if($_POST){
    if($msgname == '' || $msgemail == '' || $emailmessage == ''){
        $feedback = '*Please fill out all the fields'; 
    }else {
        mail($to, $subject, $body, $header);
        $feedback2 = 'Thanks for the message. <br /> We will contact you soon!';
    }
}
?> 

表单2的html:

<div id="mail">
                <h3>Sign Up to MPG mail...</h3>
                <div id= "form">
                    <form action="?" method="POST" onsubmit="return saveScrollPositions(this);">
                        <input type="hidden" name="scrollx" id="scrollx" value="0" />
                        <input type="hidden" name="scrolly" id="scrolly" value="0" />
                        <fieldset>
                            <input type="text" id="name" name="name" placeholder="Name" />
                            <input type="text" id="email" name="email" placeholder="Email" />
                            <input type="submit" name="submit" value="Sign Up" />
                        </fieldset>                    
                    </form><!--End Form -->
                    <p id="feedback"><?php echo $feedback; ?></p>
                </div><!-- End Form -->
            </div><!-- End mail --> 

表单2的php:

    <?php 

$to = '@morrisonpg.com.au';
$subject = 'Sign Up';

$name = $_POST ['name'];
$email = $_POST ['email'];

$body = <<<EMAIL

Please add $name with the email $email to your mailing list.

EMAIL;

$header = ' From: $name ';

if($_POST){
    if($name == '' || $email == ''){
        $feedback = '*Please fill out all the fields'; 
    }else {
        mail($to, $subject, $body, $header);
        $feedback = 'Thanks for signing up. <br /> You will receive correspondence soon!';
    }
}
?> 

2 个答案:

答案 0 :(得分:0)

为每个表单指定不同的名称,并在每个表单中放置一个隐藏元素,如;

 <input type="hidden" name="form_type" value="1" />

表格2;

 <input type="hidden" name="form_type" value="2" />

在你的php中检查form_type并为每个表单执行操作。

 $type = $_POST["form_type"];
 if ($type == 1) {
    // Code for form 1
 } else if ($type == 2){
    // Code for form 2
 } else {
    die("Invalid form");
 }

答案 1 :(得分:0)

您应该简单地添加提交按钮的名称:

<input type="submit" name="submit" value="Sign Up" />

在PHP中:

<?php if($_POST['submit']=='Sign Up'){ /* signup code */}else{ /* other code */}?>