簡體   English   中英

是否可以單頁發送電子郵件

[英]Is it possible to send email with single page

我的php表單的操作轉到另一頁,它看起來不太好,我想顯示表單,每當用戶填寫從同一頁面發送的表單時,某些輸入為空,我想給出一個錯誤。

這是我的

       <form method="post" action="#" name="emailform">
                <div class="rel">
            <div class="confor nab"><input class="cname" type="text" name="name"  value=""/></div>
            <div class="confor emb"><input class="cmail" type="text" name="email"  value=""/></div>
            <div class="confor phb"><input class="cphone" type="text" name="phone"  value=""/></div>
            <div class="confor evb"><input class="ceven" type="text" name="event"  value=""/></div>
            <div class="confor wdb"><input class="cwed" type="text" name="wdate"  value=""/></div>
            <div class="confor whb"><input class="chow" type="text" name="where"  value=""/></div>
            <div class="confort nob"><textarea class="ctho" name="notes" cols="" rows=""></textarea></div>
            <input name="submit" class="contactsend" type="submit" value="SEND" /></div>
         </form> 

只需將操作更改為:

<?=$_SERVER['PHP_SELF'];?>

因為這會將表單發送到它所在的同一頁面,所以您可以在頂部進行PHP驗證和電子郵件發送代碼,如下所示:

<?
// Here's your PHP validation and email sending code
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <link type="text/css" rel="stylesheet" href="/assets/css/styles.css" />
        <title></title>
    </head>
    <body>
        <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
            <!-- Here's all your form items -->
        </form>
    </body>
</html>

正如@joren指出的那樣,javascript / ajax是一個很好的工具。 有關工作示例和其他說明,請參見此jQuery示例

當然,它需要使用jQuery,但除非這是您將要編寫的唯一 Web表單,否則學習它(或另一個功能齊全的javascript庫:)可能值得投資。

如果不通過JavaScript驗證,我會這樣做,因為我不會為您編寫全部內容,因此簡而言之:

<?php
//various form validation functions for checking if an input is numeric, or a valid email, or whatever

function Post_Error(&$error,$name)
{
     if(is_array($error) && array_key_exists($name,$error))
     {
         echo '<span class="error">' . $error[$name] . '</span>';
     }
}
/* or you could do this
function Post_Error($name)
{
    global $error;
    if(is_array($error) && array_key_exists($name,$error))
    {
        //yadda yadda
    }
}
and then not pass the $error array each time*/

function Post_Fill($field)
{
     if(is_array($_POST) && array_key_exists($field,$_POST))
     {
          echo $_POST[$field];
     }
}

$error = array();  //Initialize error array outside of if statements, important because we are gonna be passing it below if there are errors or not, and if it is created inside one of the ifs then it might not be there when needed.
if($_POST['submit'])
{
     if(!Valid_Name($_POST['cname'])) //Valid_Name() is one of the fictional validation functions from above, could also do it manually
     {
          $error['name'] = 'Name not valid.  Field Required.  Blah blah blah.';
     }
     if(!Valid_Email($_POST['cmail']))  //Could also have a single validate() function that validates everything sent through $_POST, and returns an array of errors
     {
          $error['email'] = 'Email not valid.  Field Required.  Must be longer than 5 characters. Whatever.';
     }
     //etc. etc. for all the fields
}

if($_POST['submit'] && count($error) == 0)
{
     //Send Mail
     echo '<h1>Mail Sent!</h1>';  //Or whatever message to user that mail was sent
}
else
{
?>
       <form method="post" action="#" name="emailform">
       <div class="rel">
            <div class="confor nab"><input class="cname" type="text" name="name"  value="<?php Post_Fill('name');?>"/><?php Post_Error($error,'name');?></div>
            <div class="confor emb"><input class="cmail" type="text" name="email"  value="<?php Post_Fill('email');?>"/><?php Post_Error($error,'email');?></div>
            <div class="confor phb"><input class="cphone" type="text" name="phone"  value="<?php Post_Fill('phone');?>"/><?php Post_Error($error,'phone');?></div>
            <div class="confor evb"><input class="ceven" type="text" name="event"  value="<?php Post_Fill('event');?>"/><?php Post_Error($error,'event');?></div>
            <div class="confor wdb"><input class="cwed" type="text" name="wdate"  value="<?php Post_Fill('wdate');?>"/><?php Post_Error($error,'wdate');?></div>
            <div class="confor whb"><input class="chow" type="text" name="where"  value="<?php Post_Fill('where');?>"/><?php Post_Error($error,'where');?></div>
            <div class="confort nob"><textarea class="ctho" name="notes" cols="" rows=""><?php Post_Fill('notes');?></textarea><?php Post_Error($error,'notes');?></div>
            <input name="submit" class="contactsend" type="submit" value="SEND" /></div>
         </form> 
<?php
} //Close else
?>

哦,也是,這是一種插入式的東西,您將其包含在表單將要進入的頁面中,因此您已經擁有了所有<head>內容以及頁面外部的布局。形式PHP。

此代碼將執行的操作是,將表單提交到同一頁面,執行驗證,然后刪除表單並給出一條消息,表明如果輸入沒有錯誤,則郵件已發送給用戶,或者顯示錯誤(顯示為在保留所有字段的先前輸入的情況下,未正確驗證的輸入的輸入旁邊會出現span class =“ error”)。

為了避免離開當前頁面而只更改表單所在的頁面部分,您可以使用javascript / ajax發送表單。

像jQuery這樣的javascript庫使ajax更加容易,因此我建議您查閱jQuery文檔的.post()部分。

缺點是訪問者需要啟用javascript,但是如果您已經有一個有效的表單,它將降級為您現在擁有的新頁面加載,因此這沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM