2017-07-04 90 views
1

我想知道是否可以在表單元素的action屬性中添加多個url?我知道很多人都在問,但我加入的是:一個PHP自己和一個mailto動作。這是一個反饋表,所以當任何人發送一些反饋時,應該檢查它的驗證的php,並在點擊提交按鈕的同時發送一封電子郵件。但我測試過,但是當我點擊提交按鈕時,它就成了一個錯誤頁面。誰能幫幫我嗎?Multipe action =「」屬性url

HTML:

<form action="<?php echo htmlspecialchars($_server['php_self']);?> 
    , mailto:[email protected]" 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> 
+1

,如果你要發送的郵件不,你不能給多個網址的手段,你可以做你的功能 – Nawin

回答

1

你不能在表單動作添加多個動作,你應該的形式發送到PHP文件,然後驗證後,由PHP mail發送電子郵件功能或其他庫,例如PHPMailer的等

1

簡單的回答:不,你不能爲標籤添加多個動作,你將如何區分哪一個選擇?

如果您需要根據表單選項(或其他)上的某些配置選擇不同的操作,請使用Javascript設置表單的新操作。

在你的情況下,當你通過驗證時,使用mail PHP函數從PHP腳本發送郵件。

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

,然後在PHP

mail('[email protected]', 'Subject', 'Body', optionalHeaders); 
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:[email protected]"); 
     myWindow.close(); 
     return true; 
    } else { 
     console.log("edit!"); 
     return false; 
    } 
} 

而改變形式的行動,比如,

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