douyuan1752 2018-06-30 18:33
浏览 52
已采纳

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

Building a contact form. I have required input fields Your Name, E-mail and phone. Person has to enter information in these fields or else they will get error message. My fourth field is a message text box. In that field using placeholder= I have a default message I am interested in: "get_the_title (of the post)". Working as I hoped. My problem is I do not want to force person filling form to have to input text in this field and if they don't add any additional text in the message text box it will send the contact form email with the default text I put there using the "placeholder" text I default generated as mentioned above. Right now my code is forcing text to be input in the message box to send.

enter image description here

Here is the code I am using:

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>" . "
";

                                    // 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>';
                                    }
                                }

</div>
  • 写回答

1条回答 默认 最新

  • dqiz20794 2018-06-30 18:48
    关注

    Rather than using placeholder="I am interested in: '.get_the_title().' " you could use this:

    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>';
    

    That way the value will be sent by default if the user makes no changes to the text area

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵