我的联系表单在wordpress中的未知问题

时间:2012-11-27 22:26:10

标签: php wordpress contact-form

我开始研究wordpress模板,我非常依赖这个联系表单,我似乎没有发现问题。我必须提一下,我对php有点新手,但我能理解得很快。这是我用于我的联系页面的内容(还有其他元素,但它们不会干扰我遇到问题的代码):

    <?php get_header();
$two2_option = two2_get_global_options();
global $post; setup_postdata($post); ?>

<?php if($two2_option['two2_contact_email']){ ?>
        <form method="post" id="ajax_form" action="#">
        <fieldset>
          <ul class="form-item" id="field-name">
            <li>
              <label><?php _e("Name","jbirdie"); ?>:<span class="field-required" title="<?php _e("Required Field","jbirdie"); ?>">*</span></label>
              <input type="text" maxlength="90" name="name" id="name" placeholder="<?php _e("name","jbirdie"); ?>" size="30" class="form-field required text_input clear" />
            </li>
            <li>
              <label><?php _e("E-mail","jbirdie"); ?>:<span class="field-required" title="<?php _e("Required Field","jbirdie"); ?>">*</span></label>
              <input type="email" maxlength="90" name="email" id="email" placeholder="<?php _e("e-mail","jbirdie"); ?>" size="30" class="form-field required email text_input clear" />
            </li>
            <li>
              <label><?php _e("Phone","jbirdie"); ?></label>
              <input type="number" maxlength="90" name="phone" id="phone" placeholder="<?php _e("phone number","jbirdie"); ?>" size="30" class="form-field required email text_input clear" />
            </li>
            <li class="message">
              <label><?php _e("Comments","jbirdie"); ?> <span>Available for quotation</span></label>
              <textarea cols="55" rows="" name="message" id="message" placeholder="<?php _e("Comments","jbirdie"); ?>"  class="form-textarea text_area clear" ></textarea>
            </li>
          </ul>
          <input type="submit" name="submit" id="submit" value="send" class="submit form-submit" />
          <div id="result"></div>
        </fieldset>
     </form>
         </div>
        <?php } ?>
    </div>
    <?php } ?>
</div>

</div>

<?php get_footer(); ?>

以及functions.php中的以下内容:

add_action('wp_ajax_nopriv_two2_send_contact_form', 'two2_send_contact_form');
add_action('wp_ajax_two2_send_contact_form', 'two2_send_contact_form');

function two2_send_contact_form(){
    $two2_option = two2_get_global_options();
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    $subject = "Message from your portfolio";
    $body = "From $name, \n\n$email, \n\n$phone, \n\n$message";

    $to = $two2_option['two2_contact_email'];

    $result = mail($to, $subject, $body);

    if($result){ 
        print "<h3>".__("Success","two2")."</h3><p>".__("Your email has been sent!","two2")."</p>"; 
    } else {  
        print "<h3>".__("Error","two2")."</h3><p>".__("Try it again later, please.","two2")."</p>"; 
    }

    die();
}

......而它根本就不起作用......有人可以给我一个提示或某事发现错误吗?

1 个答案:

答案 0 :(得分:0)

您是否有脚本将表单提交给ajax处理程序? e.g。

<script type="text/javascript" >
  jQuery(document).ready(function($) {
     $('#ajax_form').submit(function(evt){
       var form_data = {
        action: 'two2_send_contact_form',
        name: jQuery('#name').val(),
        email: jQuery('#email').val(),
        phone: jQuery('#phone').val(),
        message: jQuery('#message').val()
      };
      $.post('<?php echo admin_url('admin-ajax.php'); ?>', form_data, function(response) {
        console.log(response);
      });
      return false;
    });
  });
</script>

http://codex.wordpress.org/AJAX_in_Plugins

您的联系表格中还有一个额外的PHP结束括号<?php } ?>

修改 修正了jQuery选择器&amp; ajax网址。