Php通过两个提交按钮联系我们表格,并将使用三个不同的电子邮件地址

时间:2013-04-02 10:37:27

标签: php html

我创建了一个联系我们表单,但是你有两个提交按钮:

  1. 按钮名称提交将转到此电子邮件地址的数据:sm.musa.cse@gmail.com
  2. 非常紧急的按钮名称将转到这两个电子邮件地址sm_musa@hotmail.co.uks_m_musa@yahoo.com
  3. 的数据

    我在分页中编写了php并在索引页面中编写了表单,但它无法正常工作。以下是代码:

    HTML code:

    <form action="bin/MailHandler.php" id="ContactForm" method="post">
        <div class="success"> Contact form submitted!>
            <br />
            <strong>We will be in touch soon.</strong> 
        </div>
        <fieldset>
            <div class="wrapper">
                <label class="name">
                    <span class="bg">
                        <input type="text" name="name" value="Name:" class="input" />
                    </span>
                    <span class="error">*This is not a valid name.</span>
                    <span class="empty">*This field is required.</span>
                </label>
            </div>
            <div class="wrapper">
                <label class="email">
                    <span class="bg">
                        <input type="text" name="email" value="E-mail:" class="input" />
                    </span>
                    <span class="error">*This is not a valid email address.</span>
                    <span class="empty">*This field is required.</span>
                </label>
            </div>
            <div class="wrapper">
                <label class="phone">
                    <span class="bg">
                        <input type="tel" name="phone" value="Phone:" class="input" />
                    </span>
                    <span class="error">*This is not a valid phone number.</span>
                    <span class="empty">*This field is required.</span>
                </label>
            </div>
            <div class="wrapper">
                <label class="message">
                    <span class="bg">
                        <textarea rows="1" cols="1" name="message">Message:</textarea>
                    </span>
                    <span class="error">*The message is too short.</span>
                    <span class="empty">*This field is required.</span>
                </label>
            </div>
            <div class="btns">
                <input name="first_email_send" type="submit" value="submit" />
            </div>
            <div class="btns">
                <input name="second_email_send" type="submit" value="very urgent" />
            </div>
        </fieldset>
    </form>
    

    php代码:

    <?php
        $name= $_POST['name'];
        $email= $_POST['email'];
        $phone= $_POST['phone'];
        $message= $_POST['message'];
    
    
        $to1="sm.musa.cse@gmail.com";
        $to2_1="sm_musa@hotmail.co.uk";
        $to2_2="s_m_musa@yahoo.com";
    
        $subject="You Have Recieved Mail From Customer";
        $mess1= $name.$phone.$message."MUSA VAI JEITA ICCA SEITA LEKO";
        $mess2_1= $name.$phone.$message."MUSA VAI JEITA ICCA SEITA LEKO";
        $mess2_2= $name.$phone.$message."MUSA VAI JEITA ICCA SEITA LEKO";
    
    
        //$send_contact= mail( $to,$subject,$email,$mess);
        if(isset($_POST['first_email_send']))
        {
    
            $send_contact= mail( $to1,$subject,$email,$mess1);
            if($send_contact){
                echo "Thank for submitting you details we've received your Details. One of our sales member contact you as soon as possible";
            }
            else {
            echo "ERROR";
            }
        }
        if(isset($_POST['second_email_send']))
        {
            $send_contact1= mail( $to2_1,$subject,$email,$mess2_1);
            $send_contact2= mail( $to2_2,$subject,$email,$mess2_2);
            if($send_contact1 && $send_contact2){
                echo "Thank for submitting you details we've received your Details. One of our sales member contact you as soon as possible";
            }
            else {
                echo "ERROR";
            }
        }
        /*if($send_contact){
        echo "Thank for submitting you details we've received your Details. One of our sales member contact you as soon as possible";
        }
        else {
        echo "ERROR";
        }*/
    ?>  
    

1 个答案:

答案 0 :(得分:0)

问题在于您检查数据提交方式。你需要改变两件事

使提交按钮的名称相同,说“提交”

    <div class="btns">
        <input name="submit" type="submit" value="submit" />
    </div>
    <div class="btns">
        <input name="submit" type="submit" value="very urgent" />
    </div>

发布数据作为键值对提交。提交按钮的值将提示您单击了哪个按钮。

检查POST中'submit'参数的值,然后做出决定。即修改你的PHP代码

if($_POST['submit'] == 'submit')
{

    $send_contact= mail( $to1,$subject,$email,$mess1);
    if($send_contact){
        echo "Thank for submitting you details we've received your Details. One of our sales member contact you as soon as possible";
    }
    else {
         echo "ERROR";
    }
}
if($_POST['submit'] == 'very urgent')
{
    $send_contact1= mail( $to2_1,$subject,$email,$mess2_1);
    $send_contact2= mail( $to2_2,$subject,$email,$mess2_2);
    if($send_contact1 && $send_contact2){
        echo "Thank for submitting you details we've received your Details. One of our sales member contact you as soon as possible";
    }
    else {
        echo "ERROR";
    }
}

参考:https://stackoverflow.com/a/56/1523245