【问题标题】:Form not sending all the field data表单未发送所有字段数据
【发布时间】:2015-04-26 18:36:02
【问题描述】:

所以我借用并修改了一个电子邮件表单插件,我添加了模态成功并更改了一些字段名称,回显了一些 html 等。我是 php 的新手,但基本上它并没有发送完整的表单,它只发送输入的电子邮件,不发送任何其他信息,例如姓名、电话等。这可能很愚蠢,但我把头发拉出来试图把它弄出来。

查看下面的 php:

<?php
/*
Plugin Name: Example Contact Form Plugin
Plugin URI: http://example.com
Description: Simple non-bloated WordPress Contact Form
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
*/
function html_form_code() {
	echo '<div class="frame">';
	echo '<div class="box-1">';
	echo '<div class="box">';
	echo '<h2 class="appointment-h2">My Rates</h2>';
	echo '<p class="news-section p">&pound;50 per 50 minute session.</br>I offer either a free initial 30 minute phone consultation or charge &pound;25 for a face to face initial 30 minute consultation.</br>I also offer concession rates for those on a low income.</p>';
	echo '<h2 class="appointment-h2">Request an appointment. I will contact you to discuss a convienient time for us to meet.</h2>';
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post" class="appointment-form">';
    echo '<label>First Name (required) <br/></label>';
    echo '<input type="text" class="pill required" name="cf-firstname" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-firstname"] ) ? esc_attr( $_POST["cf-firstname"] ) : '' ) . '" size="40" placeholder="First name" />';
    echo '<label>Last Name (required) <br/></label>';
    echo '<input type="text" class="pill required" name="cf-lastname" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-lastname"] ) ? esc_attr( $_POST["cf-lastname"] ) : '' ) . '" size="40" placeholder="Last name" />';
    echo '</br></br>';
    echo '<label>Your Email (required) <br/></label>';
    echo '<input type="email" class="pill required" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" placeholder="Email" />';
    echo '<label>Telephone (required) <br/></label>';
    echo '<input type="text" class="pill" name="cf-telephone" value="' . ( isset( $_POST["cf-telephone"] ) ? esc_attr( $_POST["cf-telephone"] ) : '' ) . '" size="40" placeholder="Telephone" />';
    echo '</br></br>';
    echo '<p><input type="submit" class="pill appointment-btn submit" name="cf-submitted" value="Send"></p>';
    echo '</form>';
	echo '</div>';
	echo '</div>';
	echo '</div>';
}
 
function deliver_mail() {
 
    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {
 
        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
		$lastname  = sanitize_text_field( $_POST["cf-lastname"] );
        $email   = sanitize_email( $_POST["cf-email"] );
        $telephone = sanitize_text_field( $_POST["cf-telephone"] );
 
        // get the blog administrator's email address
        $to = get_option( 'admin_email' );
 
        $headers = "From: $name <$email>" . "\r\n";
 
        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $name, $lastname, $email, $telephone ) ) {
        	echo '<div class="modal-overlay"></div>';
            echo '<div class="modal" id="modal-one" aria-hidden="true">';
			echo '<div class="modal-dialog">';
			echo '<div class="modal-header">';
			echo '<h2>Appointment request sent succsefully</h2>';
			echo '</div>';
			echo '<div class="modal-body">';
			echo '<p>I will be in touch in due course, thank you, Jo.</p>';
			echo '<div class="modal-footer">';
			echo '<a href="#/" class="btn close-modal">Close</a>';
            echo '</div></div></div>';
            echo '</div>';
    
    
        } else {
            echo '<div>';
            echo '<p>There were errors in the form, please try again</p>';
            echo '</div>';
        }
    }
}
 
function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();
 
    return ob_get_clean();
}
 
add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );
 
?>

【问题讨论】:

  • 对不起,上面的代码发送的是姓氏字段,而不是电子邮件...
  • 你能复制并把真正的 HTML 放到你的问题中吗?这更容易看。如果存在错误,它也会显示错误。您不需要回显每一行:回显适用于多行。
  • 当您提交表单时,表单字段中的值是否仍在?表单本身看起来会拉入提交的表单字段(如果存在)。您还应该尝试直接在处理电子邮件的位中输出帖子变量。看起来它应该可以快速浏览。
  • 尝试打开错误报告,然后检查 error_log 文件以及您在屏幕上看到的任何异常情况。有时错误只是打印在 error_log 中。
  • 是的,前端表单数据还在,提交后还在。

标签: php wordpress forms send


【解决方案1】:

你使用 wp_mail 错误。

您需要将电话和其他变量放在一个变量中,如下所示:

$message = " Name: $name <br>
             Lastname:  $lastname<br>
             Telephone: $telephone <br>
             Email: $email   ";

$subject = "Contact from my site";

然后使用这个:

wp_mail( $to, $subject, $message)

【讨论】:

  • 太棒了!请点击我的回答关闭问题。
猜你喜欢
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多