多个 action="" 属性 url

Multipe action="" attribute urls

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

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>

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

如果您需要根据表单选择(或其他)的某些配置select 不同的操作,请使用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);

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

你做不到,但你可以在你的ask()里面调用mailto,比如,

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>