更新特定于电子邮件提交按钮上的此代码

Posted

技术标签:

【中文标题】更新特定于电子邮件提交按钮上的此代码【英文标题】:UPDATED specific to this code on emailer submit button 【发布时间】:2015-12-29 20:06:00 【问题描述】:

好的,所以在我阅读了您最初的帖子后,根据我的理解,我对代码进行了更新(以使其阅读起来更简单),因为我还是这方面的新手。现在回想起来,这可能就是问题所在。所以我感谢你的耐心;请不要杀我...这是我将其更改为并在服务器上测试的内容:

这是实际网页(ContactsUs.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="PSStyles.css" rel="stylesheet" type="text/css">
<title>Contact Us Form</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">

$(document).ready(function() 
$("#qForm").validate(
rules: 
firstname: "required",
lastname: "required",
email: 
required: true,
email: true
,
comments: "required"
,
messages: 
firstname: "First Name Required",
lastname: "Last Name Required",
email: 
required: "Email Required",
email: "Invalid Email Address"
,
comments: "You must write a message"

);
);
</script>

</head>

<body>

<div id="wrapper">
<?php include 'header1.php'?>
</div>




<div id="ripmain">
<div id="menuet">

<nav>
<ul id="menubar">
<li><a href="index.php">Home</a></li>
<li><a href="AboutUs.php">About</a></li>
<li><a href="Location.php">Location</a></li>
<li><a href="GroomingServices.php">Grooming</a></li>
<li><a href="ContactUs.php">Contact Us</a></li>

</ul>
</nav>

</div>
</div>


<form method="POST" action="contact.php" id="qForm">
<fieldset >
<legend>Contact Us Form</legend>


<p>First Name: <input type="text" size="32" name="firstname" /></p>
<p>Last Name: <input type="text" size="32" name="lastname" /></p>
<p>Email: <input type="text" size="32" id="email" name="email" /></p>
<div id="rsp_email"><!-- --></div>


<td>Comments: </td>
<td>
<textarea name="Comments" cols="40" rows="3" wrap="virtual"></textarea>
</td>

<input type="hidden" name="subject" value="online_submission" />
<p><input type="submit" value="submit"></p>
</fieldset>
</form>

<?php include 'footer1.php';?>

</div>
</body>
</html>

然后我将操作文件(contact.php)更改为:

<?php
if(isset($_POST['firstname'])) 
$contact    =   validate_inputs($_POST);
if(in_array(false, $contact) === true) 
echo process_errors($contact);
exit;

else 
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!

Your contact form has been submitted by:

First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>

Comments:
<?php echo $contact['comments']; ?>

End of message
<?php
$message    =   ob_get_contents();
ob_end_clean();

// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) 
header('Location: thanks.html');
exit();

else
die("An error occurred while sending. Please contact the administrator.");



?>

所以,今天早上我已经按照您的建议返回并应用了实际更改。问题是我在结束 html 标记之后的 php 标记中的第 140 行出现语法错误。它的一个右括号有问题。这是新网页(ContactForm.php)的代码:

function error_codes($code = false)

$valid['firstname'] =   "Enter your name";
$valid['lastname']  =  "Enter your name";
$valid['subject']   =  "Write a subject";
$valid['email']     =  "Invalid email";
$valid['comments']  =   "Write your comments";

return (isset($valid[$code]))? $valid[$code] : false;

// Run the validation and return populated array
function validate_inputs($REQUEST)

/* Check all form inputs using check_input function */
$valid['firstname'] =   check_input($REQUEST['firstname']);
$valid['lastname']  =   check_input($REQUEST['lastname']);
$valid['subject']   =   check_input($REQUEST['subject']);
$valid['email']     =   check_input($REQUEST['email'],"email");
$valid['comments']  =   check_input($REQUEST['comments']);

return $valid;

// Modify your validate function a bit to do only validation, no returning of errors
function check_input($data = false, $type = false)

if($type == 'email')
return (filter_var($data,FILTER_VALIDATE_EMAIL))? $data : false;

$data   =   trim($data);
$data   =   stripslashes($data);
$data   =   htmlspecialchars($data);

return (!empty($data))? $data : false;

// This will loop through returned values and populate errors based on empty
function process_errors($array = false)

if(!is_array($array))
return $array;

foreach($array as $key => $value) 
if(empty($value))
$errors[]   =   error_codes($key);


return (!empty($errors))? show_error($errors) : false;

// display errors via buffer output
function show_error($myError)

ob_start();


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/css/default.css" rel="stylesheet">
<title>Contact Us Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() 
$("#qForm").validate(
rules: 
firstname: "required",
lastname: "required",
email: 
required: true,
email: true
,
comments: "required"
,
messages: 
firstname: "First Name Required",
lastname: "Last Name Required",
email: 
required: "Email Required",
email: "Invalid Email Address"
,
comments: "You must write a message"

);
);
</script>
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo">
<h1 id="sitename"><img src="Images/logo.jpg"    /></span></h1>
<h2 class="description">The home for pampered pets.</h2>
</div>
<div id="headercontent">
<h2>Happy Pets-timonials</h2>
<p>My owner took me to Sandy's for a bath and I got the 'spaw' treatment. - Rover</p>
</div>
<div id="sitecaption"> Satisfaction <span class="bigger">Guaranteed</span> </div>
</div>
<div id="ripmain">
<div id="menuet">
<nav>
<ul id="menubar">
<li><a href="PSTP.html">Home</a></li>
<li><a href="AboutUs.html">About</a></li>
<li><a href="Location.html">Location</a></li>
<li><a href="GroomingServices.html">Grooming</a></li>
<li><a href="ContactUs.html">Contact Us</a></li>
</ul>
</nav>
</div>

<form method="POST" action="ContactProcess.php" id="qForm">
<fieldset>
<legend>Contact Us Form</legend>
<p>First Name: <input type="text" size="32" name="firstname" /></p>
<p>Last Name: <input type="text" size="32" name="lastname" /></p>
<p>Email: <input type="text" size="32" id="email" name="email" /></p>
<div id="rsp_email"><!-- --></div>
<td>Comments: </td>
<td>
<textarea name="comments" cols="40" rows="3" wrap="virtual"></textarea>
</td>
<input type="hidden" name="subject" value="online_submission" />
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<div id="footer"> &copy; Copyright 2015 Time Live, Inc. All rights reserved. <br>
Hours: Mon-Fri: 6 am to 11 pm; Sat & Sun: 8 am to 10pm <br>
Links to other local services:   <li><a href="http://www.hillsidevetclinic.org">Hillside Vet Clinic</a></li> <li><a href="http://www.petsmart.com">PetSmart Stores</a></li> <li><a href="http://www.poochhotel.com">Pooch Hotel</a> </div>
</body>
</html>

<?php
$data   =   ob_get_contents();
ob_end_clean();

return $data;


function send_email($settings = false)

$to         =   (!empty($settings['to']))? $settings['to']:false;
$from       =   (!empty($settings['from']))? "From:".$settings['from'].PHP_EOL:false;
$subject    =   (!empty($settings['subject']))? $settings['subject']:false;
$message    =   (!empty($settings['message']))? $settings['message']:false;

if(in_array(false, $settings) === true)
return false;

return (mail($to,$subject,$message));

?>

根据您的建议,这是新的帖子文件 (ContactProcess.php):

<?php
if(isset($_POST['firstname'])) 
$contact    =   validate_inputs($_POST);
if(in_array(false, $contact) === true) 
echo process_errors($contact);
exit;

else 
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!

Your contact form has been submitted by:

First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>

Comments:
<?php echo $contact['comments']; ?>

End of message
<?php
$message    =   ob_get_contents();
ob_end_clean();

// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) 
header('Location: thanks.html');
exit();

else
die("An error occurred while sending. Please contact the administrator.");


我还没有测试过第二个代码;但会这样做,让你知道我发现了什么;同时对上面的修改/更新代码有什么建议吗?再次感谢您的帮助...

【问题讨论】:

【参考方案1】:

尝试将您的一些逻辑拆分为小函数,这样更容易跟踪任务。同样对于表单,尝试通过 jQuery 使用表单验证:

表单页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/css/default.css" rel="stylesheet">
<title>Contact Us Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() 
    $("#qForm").validate(
            rules: 
                firstname: "required",
                lastname: "required",
                email: 
                    required: true,
                    email: true
                ,
                comments: "required"
            ,
            messages: 
                firstname: "First Name Required",
                lastname: "Last Name Required",
                email: 
                    required: "Email Required",
                    email: "Invalid Email Address"
                ,
                comments: "You must write a message"
            
        );
);
</script>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <div id="logo">
            <h1 id="sitename"><img src="Images/logo.jpg"    /></span></h1>
            <h2 class="description">The home for pampered pets.</h2>
        </div>
        <div id="headercontent">
            <h2>Happy Pets-timonials</h2>
            <p>My owner took me to Sandy's for a bath and I got the 'spaw' treatment. - Rover</p>
        </div>
        <div id="sitecaption"> Satisfaction <span class="bigger">Guaranteed</span> </div>
    </div>
    <div id="ripmain">
        <div id="menuet">
            <nav>
                <ul id="menubar">
                  <li><a href="PSTP.html">Home</a></li>
                  <li><a href="AboutUs.html">About</a></li>
                  <li><a href="Location.html">Location</a></li>
                  <li><a href="GroomingServices.html">Grooming</a></li>
                  <li><a href="ContactUs.html">Contact Us</a></li>
                </ul>
            </nav>
        </div>

<form method="POST" action="contact.php" id="qForm">
    <fieldset>
        <legend>Contact Us Form</legend>
        <p>First Name: <input type="text" size="32" name="firstname" /></p>
        <p>Last Name: <input type="text" size="32" name="lastname" /></p>
        <p>Email: <input type="text" size="32" id="email" name="email" /></p>
        <div id="rsp_email"><!-- --></div>
        <td>Comments: </td>
        <td>
            <textarea name="comments" cols="40" rows="3" wrap="virtual"></textarea>
        </td>
            <input type="hidden" name="subject" value="online_submission" />
        <p><input type="submit" value="Submit"></p>
    </fieldset>
</form>
<div id="footer"> &copy; Copyright 2015 Time Live, Inc. All rights reserved. <br>
Hours: Mon-Fri: 6 am to 11 pm; Sat & Sun: 8 am to 10pm <br>
Links to other local services:   <li><a href="http://www.hillsidevetclinic.org">Hillside Vet Clinic</a></li> <li><a href="http://www.petsmart.com">PetSmart Stores</a></li> <li><a href="http://www.poochhotel.com">Pooch Hotel</a> </div>
</body>
</html>

联系表格所需的功能:

// This will return error messages (you could expand it to be database driven)
function error_codes($code = false)
    
        $valid['firstname'] =   "Enter your name";
        $valid['lastname']  =  "Enter your name";
        $valid['subject']   =  "Write a subject";
        $valid['email']     =  "Invalid email";
        $valid['comments']  =   "Write your comments";

        return (isset($valid[$code]))? $valid[$code] : false;
    
// Run the validation and return populated array
function validate_inputs($REQUEST)
    
        /* Check all form inputs using check_input function */
        $valid['firstname'] =   check_input($REQUEST['firstname']);
        $valid['lastname']  =   check_input($REQUEST['lastname']);
        $valid['subject']   =   check_input($REQUEST['subject']);
        $valid['email']     =   check_input($REQUEST['email'],"email");
        $valid['comments']  =   check_input($REQUEST['comments']);

        return $valid;
    
// Modify your validate function a bit to do only validation, no returning of errors
function check_input($data = false, $type = false)
    
        if($type == 'email')
            return (filter_var($data,FILTER_VALIDATE_EMAIL))? $data : false;

        $data   =   trim($data);
        $data   =   stripslashes($data);
        $data   =   htmlspecialchars($data);

        return (!empty($data))? $data : false;
    
// This will loop through returned values and populate errors based on empty
function process_errors($array = false)
    
        if(!is_array($array))
            return $array;

        foreach($array as $key => $value) 
                if(empty($value))
                    $errors[]   =   error_codes($key);
            

        return (!empty($errors))? show_error($errors) : false;
    
// display errors via buffer output
function show_error($myError)
    
        ob_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<b>Please correct the following error:</b><br />
<?php echo implode("<br />".PHP_EOL,$myError); ?>
</body>
</html>
<?php
        $data   =   ob_get_contents();
        ob_end_clean();

        return $data;
    

function send_email($settings = false)
    
        $to         =   (!empty($settings['to']))? $settings['to']:false;
        $from       =   (!empty($settings['from']))? "From:".$settings['from'].PHP_EOL:false;
        $subject    =   (!empty($settings['subject']))? $settings['subject']:false;
        $message    =   (!empty($settings['message']))? $settings['message']:false;

        if(in_array(false, $settings) === true)
            return false;

        return (mail($to,$subject,$message));
    

contact.php:

// Include above functions
if(isset($_POST['firstname'])) 
        $contact    =   validate_inputs($_POST);
        if(in_array(false, $contact) === true) 
                echo process_errors($contact);
                exit;
            
        else 
                /* Let's prepare the message for the e-mail */
                ob_start();
?>Hello!

Your contact form has been submitted by:

First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>

Comments:
<?php echo $contact['comments']; ?>

End of message
<?php
                $message    =   ob_get_contents();
                ob_end_clean();

                // Send the message here
             if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) 
                        header('Location: thanks.html');
                        exit();
                    
                else
                    die("An error occurred while sending. Please contact the administrator.");
            
    

【讨论】:

谢谢!感谢您的帮助。 好的;查看了contact.php 文件,看不到那里可能有什么问题;正文中确实有一条消息说请更正以下错误......但这并不能解释为什么它仍然没有发送。是否需要连接。 ... 抱歉,您的问题是什么?我发布的是contact.php 页面的更新/修订。如果缺少信息,它将退出并打印以分页丢失的信息。我在主机上测试了代码,它工作正常。您是否尝试过或到目前为止您尝试过什么? 如果你想看到它的工作,我可能会发布一个演示。 你完全回答了我的问题!!!!所以,我用复选标记标记了它,但还没有足够的分数来投票给你。但只要我这样做,我会的。再次感谢拉斯克拉特!!!!

以上是关于更新特定于电子邮件提交按钮上的此代码的主要内容,如果未能解决你的问题,请参考以下文章

使用PayPal IPN发送特定于交易的电子邮件

一个表单,一个提交按钮,但有两个操作

如果电子邮件已使用 jquery 在 php 中注册,则禁用提交按钮 [重复]

求C# winform中点击按钮执行网页JS提交表单代码实现~~!

使用提交按钮更新数据库数据

许多 UIButtons 的共享操作,获取特定于按钮的字符串