多重行动=""属性网址

时间:2017-07-04 06:51:00

标签: javascript php html forms action

我想知道你是否可以在表单元素的action属性中添加多个url?我知道很多人都在问这个,但我加入的是:一个php自我和一个mailto动作。这是一个反馈表单,所以当任何人发送一些反馈时,它应该检查它的验证,并在点击提交按钮后同时发送电子邮件。但我测试了但是当我点击提交按钮时它出现了一个错误页面。任何人都可以帮助我吗?

HTML:

<form action="<?php echo htmlspecialchars($_server['php_self']);?>
    , mailto:example@gmail.com" method="post" name="feedbackForm" id="ff" class="feedback"> <label for="first">First Name:</label>
    <input type="text" id="first" name="fname" class="namef" placeholder="First Name" required="required"/><span class="error"><?php echo $fnameErr;?>
    </span><br/>
    <label for="last">Last Name:</label>
    <input type="text" id="last" name="lname" class="namel" placeholder="Last Name" required="required"/><span class="error"><?php echo $lnameErr;?>
    </span><br/>
    <label for="mail">Email:</label>
    <input type="email" id="mail" name="email" class="u-email" placeholder="any email is fine!" required="required"/><span class="error"><?php echo 
    $emailErr;?>
    </span><br/>
    <label for="yearLevel">Year Level:</label>
    <select name="yearLevel" id="yearLevel" onchange="MM_jumpMenu('parent',this,0)">
        <option>Year 8</option>
        <option>Year 9</option>
        <option>Year 10</option>
        <option>Year 11</option>
        <option>Year 12</option>
        <option>Uni Student</option>
        <option>Other</option>
    </select>
    <label for"comment">Comment:</label>
    <textarea id="comment" name="comment" class="userComment" rows="12" cols="" "55" ">
    </textarea><br/><br/>
    <input type="submit" name="submitFeed" id="subff" class="sub" onclick="ask()" style="margin-left:20%;"/>
</form>

PHP:

<?php
//feedback form validation code
//start

//variables
$fnameErr = $lnameErr = $yearleErr = $emailErr = "";
$firstName = $lastName = $comment = $yearLevel = $email = "";


//the function of validation
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["fname"])) {
        $fnameErr = "* Your first name is required!";
    } else {
        $firstName = test_input($_POST["fname"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z]*$/", $firstName)) {
            $fnameErr = "* Only letters and white space allowed!";
        }
    }

    if (empty($_POST["lname"])) {
        $lnameErr = "* Your last name is required!";
    } else {
        $lastName = test_input($_POST["lname"]);
        if (!preg_match("/^[a-zA-Z]*$/", $lastName)) {
            $lnameErr = "* Only letters and white space allowed!";
        }
    }

    if(empty($_POST["comment"])) {
        $comment = "* the comment box is required!";
    } else {
        $comment = test_input($_post["comment"]);
    }


    if (empty($_POST["email"])) {
        $emailErr = "* Your email is required!";
    } else {
        $email = test_input($_POST["email"]);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
           $emailErr = "* Invalid Email Format!";
        }
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
//end
?>

用于在用户单击提交时进行确认的JS脚本。所以它对形式有意义。

<script type="text/javascript">
//confrim before submitting.
function ask() {
    var box = confirm("Are you sure you want to send this feeback? If yes 
that you are sure click ok or if not then click canel to edit it.");
    if (box == true) {
        document.getElementById('firstPart').style.display = "none";
        document.getElementById('nextPart').style.display = "block";
        console.log("Thanks for sending your feedback");
        return true;
    } else {
        console.log("edit!");
        return false;
    }
}

</script>

3 个答案:

答案 0 :(得分:1)

简单回答:不,你不能为标签添加多个动作,你将如何区分选择哪一个?

如果您需要根据表单选择(或其他)的某些配置选择不同的操作,请使用 Javascript 设置表单的新操作。

在您的情况下,使用mail PHP函数在您通过验证时从PHP脚本发送邮件。

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" name="feedbackForm" id="ff" class="feedback">

然后在PHP中

mail('somebody@example.com', 'Subject', 'Body', optionalHeaders);

答案 1 :(得分:1)

您无法在表单操作中添加多个操作,您应该将表单发送到PHP文件,然后在验证后,通过PHP mail函数或其他库(如PHPmailer等)发送电子邮件。 / p>

答案 2 :(得分:0)

你不能这样做,但你可以在你的mailto内拨打ask()

function ask() {
    var box = confirm("Are you sure you want to send this feeback? If yes 
that you are sure click ok or if not then click canel to edit it.");
    if (box == true) {
        document.getElementById('firstPart').style.display = "none";
        document.getElementById('nextPart').style.display = "block";
        console.log("Thanks for sending your feedback");
        // add mailto here
        myWindow=window.open("mailto:example@gmail.com");
        myWindow.close();
        return true;
    } else {
        console.log("edit!");
        return false;
    }
}

更改表单操作,

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" ...>
  ....
</form>