是否可以发送单页电子邮件

时间:2011-01-29 16:57:37

标签: php javascript ajax

我的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> 

4 个答案:

答案 0 :(得分:3)

只需将操作更改为:

<?=$_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>

答案 1 :(得分:1)

正如@joren指出的那样,javascript / ajax是一个很好的工具。有关工作示例和其他说明,请参阅this jQuery example

当然它需要使用jQuery,但除非这是你将要编写的唯一的 Web表单,否则学习它(或另一个功能齐全的javascript库)可能值得投资:)< / p>

答案 2 :(得分:1)

以下是我如何在没有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”中)在那些未正确验证的输入的输入旁边,同时保留所有字段的先前输入。

答案 3 :(得分:0)

为避免离开当前页面而只是更改表单所在页面的部分,您可以使用javascript / ajax在表单中发送。

像jQuery这样的javascript库使ajax更容易,所以我建议你查看jQuery文档的.post()部分。

缺点是访问者需要启用javascript,但如果你已经有一个工作表单,它会降级到你现在的新页面加载,所以这没问题。