在一页上验证两个表单


Validate 2 forms on one page

我试图将两个表单合并到一个页面中,但php将这两个表单视为一个表单,我不知道如何将它们分离。提前感谢您的帮助。这是我的表单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!';
    }
}
?> 

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

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

在PHP中:

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

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

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

对于中二;

 <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");
 }