首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用php处理Wordpress Ajax表单

用php处理Wordpress Ajax表单
EN

Stack Overflow用户
提问于 2013-03-20 12:02:14
回答 1查看 449关注 0票数 0

我有一个表格在我的网站上,这是目前张贴到一个php文件。现在,当表单提交时,它会将用户带到空白的php文件。我想让它通过ajax发布或重定向后,它已经处理了邮件功能。谢谢!

HTML:

代码语言:javascript
复制
<form method="post" id="formApply"  action="<?php bloginfo('template_url');?>/inc/sendContact.php" name="applyForm">
                <div id="col1">
                <fieldset class="col-1">
                <div class="white info-line">Want to know more? Drop us a line and we'll get right back to you.</div>
                <label class="white text-label name" for="fname">Name:</label><input type="text" name="fname" id="fname" tabindex="1" />
                <div class="clear"></div>
                <label class="white text-label email" for="email">Email:</label><input type="email" class="lower" name="email" id="email"  tabindex="2"/>
                <div class="clear"></div>
                <label class="white text-label phone" for="phone">Phone:</label><input type="tel" name="phone" id="phone" tabindex="3" maxlength="10"/>
                <div class="white text-label checker-title" id="checkererror">I am interested in:</div>
                <input type="checkbox" id="checkbox-2-1" class="regular-checkbox big-checkbox" name="checker[]" value="Office Space" /><label for="checkbox-2-1"></label><div class="white text-label checker">Office Space</div>
                <input type="checkbox" id="checkbox-2-2" class="regular-checkbox big-checkbox" name="checker[]" value="Designated Workspace" /><label for="checkbox-2-2"></label><div class="white text-label checker">Designated Workspace</div>
                <input type="checkbox" id="checkbox-2-3" class="regular-checkbox big-checkbox" name="checker[]" value="Shared Space" /><label for="checkbox-2-3"></label><div class="white text-label checker">Shared Space</div>
                </fieldset>
                </div>
                <div id="col2">
                <fieldset class="col-2">
                <label class="white text-label note-label" for="notes">Notes:</label><br/>
                <textarea name="notes" id="notes" rows="10" cols="30" class="requiredField" tabindex="4"></textarea>
                <input type="hidden" name="submitted" id="submitted" class="submitted" value="true" /><button id="submit" type="submit">Submit Application</button>
                </fieldset>
                </div>
                </form>

PHP文件:

代码语言:javascript
复制
    require_once 'lib/swift_required.php';


$transport = Swift_MailTransport::newInstance();

$mailer = Swift_Mailer::newInstance($transport);

$name = Trim(stripslashes($_POST['fname']));
$email = Trim(stripslashes($_POST['email']));
$phone = Trim(stripslashes($_POST['phone']));
$notes  = Trim(stripslashes($_POST['notes']));
$checkbox = $_POST['checker'];


$message = Swift_Message::newInstance()
  ->setFrom(array('From_Email' => 'My Website'))
  ->setSubject('[My_Subject from '.$name.']')
  ->setTo(array('My_Email'))
  ->setBody(
'<html>'.
'<head></head>'.
'<body>'.
'<strong>Name:</strong><br />'.
$title.' '.$name.'<br /><br />'.
'<strong>Email Address:</strong><br />'.
$email.'<br /><br />'.
'<strong>Phone Number:</strong><br />'.
$phone.'<br /><br />'.
'<strong>Interested in:</strong><br />'.
implode('<br />', $checkbox).'<br /><br />'.
'<strong>Message:</strong><br />'.
$notes.'<br /><br />'.
'</body>'.
'</html>',
'text/html' );
$result = $mailer->send($message); 

有人建议我使用这个jquery,但我也不能让它工作。

代码语言:javascript
复制
jQuery(document).ready(function() {  
jQuery("#submit").click(function(){ 
    jQuery.ajax({  
        type: 'POST',  
        url: '<?php echo admin_url('admin-ajax.php');?>',  
        data: {  
            action: 'MyAjaxCallBack',
            MyParam: 'MyParamValue'
        },  
        success: function(data, textStatus, XMLHttpRequest){  
            alert(data);  
        },  
        error: function(MLHttpRequest, textStatus, errorThrown){  
            alert(errorThrown);  
        }  
    });  
});  

});

在functions.php中

代码语言:javascript
复制
 function MyAjaxCallBack()
{
    foreach($_POST as $key=>$value)
        $$key = $value;

    die('Email script will go here!');
}

// creating Ajax call for WordPress  
add_action('wp_ajax_nopriv_MyAjaxCallBack', 'MyAjaxCallBack');
add_action('wp_ajax_MyAjaxCallBack', 'MyAjaxCallBack');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-21 03:38:31

我通过添加以下更新的代码解决了这个问题: HTML:

代码语言:javascript
复制
<form method="post" id="formApply"  action="<?php bloginfo('template_url');?>/inc/sendContact.php" name="applyForm">
                <div id="col1">
                <fieldset class="col-1">
                <div class="white info-line">Want to know more? Drop us a line and we'll get right back to you.</div>
                <label class="white text-label name" for="fname">Name:</label><input type="text" name="fname" id="fname" tabindex="1" />
                <div class="clear"></div>
                <label class="white text-label email" for="email">Email:</label><input type="email" class="lower" name="email" id="email"  tabindex="2"/>
                <div class="clear"></div>
                <label class="white text-label phone" for="phone">Phone:</label><input type="tel" name="phone" id="phone" tabindex="3" maxlength="10"/>
                <div class="white text-label checker-title" id="checkererror">I am interested in:</div>
                <input type="checkbox" id="checkbox-2-1" class="regular-checkbox big-checkbox" name="checker[]" value="Office Space" /><label for="checkbox-2-1"></label><div class="white text-label checker">Office Space</div>
                <input type="checkbox" id="checkbox-2-2" class="regular-checkbox big-checkbox" name="checker[]" value="Designated Workspace" /><label for="checkbox-2-2"></label><div class="white text-label checker">Designated Workspace</div>
                <input type="checkbox" id="checkbox-2-3" class="regular-checkbox big-checkbox" name="checker[]" value="Shared Space" /><label for="checkbox-2-3"></label><div class="white text-label checker">Shared Space</div>
                </fieldset>
                </div>
                <div id="col2">
                <fieldset class="col-2">
                <label class="white text-label note-label" for="notes">Notes:</label><br/>
                <textarea name="notes" id="notes" rows="10" cols="30" class="requiredField" tabindex="4"></textarea>
                <input type="hidden" name="submitted" id="submitted" class="submitted" value="true" /><button id="submit" type="submit">Submit Application</button>
                </fieldset>
                </div>
                </form>

PHP文件:

代码语言:javascript
复制
require( '../../../../wp-blog-header.php' );        
require_once 'lib/swift_required.php';


    $transport = Swift_MailTransport::newInstance();

    $mailer = Swift_Mailer::newInstance($transport);

    $name = Trim(stripslashes($_POST['fname']));
    $email = Trim(stripslashes($_POST['email']));
    $phone = Trim(stripslashes($_POST['phone']));
    $notes  = Trim(stripslashes($_POST['notes']));
    $checkbox = $_POST['checker'];
    $adminEmail = get_bloginfo('admin_email');

    $message = Swift_Message::newInstance()
      ->setFrom(array('From_Email' => 'My Website'))
      ->setSubject('[My_Subject from '.$name.']')
      ->setTo(($adminEmail))
      ->setBody(
    '<html>'.
    '<head></head>'.
    '<body>'.
    '<strong>Name:</strong><br />'.
    $title.' '.$name.'<br /><br />'.
    '<strong>Email Address:</strong><br />'.
    $email.'<br /><br />'.
    '<strong>Phone Number:</strong><br />'.
    $phone.'<br /><br />'.
    '<strong>Interested in:</strong><br />'.
    implode('<br />', $checkbox).'<br /><br />'.
    '<strong>Message:</strong><br />'.
    $notes.'<br /><br />'.
    '</body>'.
    '</html>',
    'text/html' );
    $result = $mailer->send($message);
$url = home_url('/#apply');
die(header("Location: $url", true));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15515055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档