在文本框中提交联系表单默认文本

时间:2018-06-30 18:33:14

标签: php wordpress

建立联系表格。我必须输入字段您的姓名,电子邮件和电话。人员必须在这些字段中输入信息,否则他们将收到错误消息。我的第四个字段是消息文本框。在该字段中,使用placeholder =我有一条我感兴趣的默认消息:“(帖子的)get_the_title”。如我所愿。我的问题是我不想强迫人员填写表格必须在此字段中输入文本,如果他们不在消息文本框中添加任何其他文本,它将发送带有我在其中使用的默认文本的联系表单电子邮件如上所述,我默认生成的“占位符”文本。现在,我的代码是强制在消息框中输入文本进行发送。

enter image description here

这是我正在使用的代码:

echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
                                echo '<h5>';
                                echo 'Ask a question <br/>';
                                echo '</h5>';
                                echo '<p>';
                                echo '<input style="margin-left: 15px;" type="text" placeholder="Your Name (required)" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="29" />';
                                echo '</p>';
                                echo '<p>';
                                echo '<input style="margin-left: 15px;" type="email" placeholder="E-mail (required)" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="29" />';
                                echo '</p>';
                                echo '<p>';
                                echo '<input style="margin-left: 15px;" type="text" placeholder="Phone (required)" name="cf-subject" pattern="[0-9-() ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="29" />';
                                echo '</p>';
                                echo '<p>';
                                echo '<textarea style="margin-left: 15px; width: 246px;" rows="3" cols="28" placeholder="I am interested in: '.get_the_title().' " name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
                                echo '</p>';
                                echo '<p><input type="submit" name="cf-submitted" value="Contact Agent" style="border-radius: 5px; color: #fff; margin-left: 15px; width: 247px; height: 38px; background-color: #27ae60;"></p>';
                                echo '</form>';
                                echo '<p style="font-size: x-small; font-weight: bold; margin-left: 22px;">';
                                echo 'By sending a request you agree to our Privacy Policy';
                                echo '</p>';


                                // if the submit button is clicked, send the email
                                if ( isset( $_POST['cf-submitted'] ) ) {

                                    // sanitize form values
                                    $name    = sanitize_text_field( $_POST["cf-name"] );
                                    $email   = sanitize_email( $_POST["cf-email"] );
                                    $subject = sanitize_text_field( $_POST["cf-subject"] );
                                    $message = esc_textarea( $_POST["cf-message"] );

                                    // 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, $subject, $message, $headers ) ) {
                                    echo '<div>';
                                    echo '<h5>Thanks for contacting us, expect a response soon.</h5>';
                                    echo '</div>';
                                    } 
                                    else {
                                    echo '<h5>An unexpected error occurred.</h5>';
                                    }
                                }

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法来代替使用placeholder="I am interested in: '.get_the_title().' "

echo '<textarea style="margin-left: 15px; width: 246px;" rows="3" cols="28" name="cf-message">' . 
( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : 'I am interested in: '.get_the_title() ) . 
'</textarea>';

这样,如果用户不更改文本区域,默认情况下将发送值

相关问题