您必须为此创建单独的模块。我还在这里创建和共享代码。表单提交是通过ajax。可能是我错过了要删除或重命名的内容。尝试纠正它。
app\etc\modules\namespace_modulename.xml
<Namespace_Modulename>
<active>true</active>
<codePool>local</codePool>
</Namespace_Modulename>
app\code\local\Namespace\Modulename\Helper\Data.php
<?php class Namespace_Modulename_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>
app\code\local\Namespace\Modulename\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Modulename>
<version>0.0.01</version>
</Namespace_Modulename>
</modules>
<frontend>
<routers>
<contacts>
<args>
<modules>
<Namespace_Modulename before="Mage_Contacts">Namespace_Modulename</Namespace_Modulename>
</modules>
</args>
</contacts>
</routers>
</frontend>
<global>
<helpers>
<Modulename>
<class>Namespace_Modulename_Helper</class>
</Modulename>
</helpers>
<template>
<email>
<havequestion_email_email_template translate="label" module="contacts">
<label>Have a Question Form</label>
<file>havequestion_form.html</file>
<type>text</type>
</havequestion_email_email_template>
</email>
</template>
</global>
</config>
app\code\local\Namespace\Modulename\controller\IndexController.php
public function havequestionpostAction()
{
$template_path = 'havequestion_email_email_template';
//$post = $this->getRequest()->getPost();
$post = array (
'name' => $this->getRequest()->getParam('name'),
'email' => $this->getRequest()->getParam('email'),
'comment' => $this->getRequest()->getParam('comment')
);
if ( $post ) {
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);
$error = false;
if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
$error = true;
}
if ($error) {
throw new Exception();
}
$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$senderDetail = Mage::getStoreConfig('trans_email/ident_'.Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER));
$senderDetail['name'] = $post['name'];
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
->sendTransactional(
$template_path,
$senderDetail,
//Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array('data' => $postObject)
);
if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}
$translate->setTranslateInline(true);
//Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
//$this->_redirectUrl($post['currentpage']);
echo 'SUCCESS';
return;
} catch (Exception $e) {
$translate->setTranslateInline(true);
//Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
//$this->_redirectUrl($post['currentpage']);
echo '<div class="error-msg">Unable to submit your request. Please, try again later.</div>';
return;
}
} else {
echo '<div class="error-msg">Unable to submit your request. Please, try again later.</div>';
//$this->_redirectUrl($post['currentpage']);
return;
}
}
}
app\design\frontend\default\YOUR_TEMPLATE_PATH\template\contacts\havequestionform.phtml
<div class="form-add">
<form action="<?php echo $this->getUrl('') ?>contacts/index/havequestionpost/" id="havequestionForm" method="post">
<div class="question-ajax-msg"></div>
<ul class="form-list">
<li class="fields">
<div class="field">
<label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
<div class="input-box">
<input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
</div>
</div>
<div class="field">
<label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
<div class="input-box">
<input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
</div>
</div>
</li>
<li class="wide">
<label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
<div class="input-box">
<textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
</div>
</li>
</ul>
<div class="">
<p class="required" style="text-align:left"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
<button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
<span class="question-ajax-loading"> </span>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
var havequestionForm = new VarienForm('havequestionForm', true);
// submit have a question by ajax
jQuery('#havequestionForm').submit(function (e){
e.preventDefault();
jQuery(".question-ajax-loading").css('display','block');
var name = jQuery("#name").val();
var email = jQuery("#email").val();
var comment = jQuery("#comment").val();
var hideit = jQuery("#hideit").val();
var prosku = jQuery("#prosku").val();
var prostyle = jQuery("#prostyle").val();
var procolor = jQuery("#procolor").val();
var currentpage = jQuery("#currentpage").val();
var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment + '&hideit=' + hideit + '&prosku=' + prosku + '&prostyle=' + prostyle + '&procolor=' + procolor + '¤tpage=' + currentpage;
url = jQuery('#havequestionForm').attr('action');
jQuery.ajax( {
url : url,
Type : "POST",
data: dataString,
success: function(data) {
jQuery(".question-ajax-msg").css('display','block');
if(data == 'SUCCESS'){
var successMsg = '<div class="success-msg">Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.</div>';
jQuery(".question-ajax-msg").html(successMsg);
jQuery("#comment").val('');
}
else {
jQuery(".question-ajax-msg").html(data);
jQuery("#comment").val('');
}
jQuery(".question-ajax-loading").css('display','none');
jQuery('.question-ajax-msg').delay(5000).fadeOut('slow');
}
});
return false;
});
//]]>
</script>
</div>