【问题标题】:How to submit a form to itself in wordpress如何在wordpress中向自己提交表单
【发布时间】:2014-01-21 05:11:36
【问题描述】:

我有一个我设计的 wordpress 主题,我在地址上有一个联系表 -

www.[website_name].com/contact

我有一个表单定义,上面写着 -

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

以及我使用

确定 post 方法的方式
if ($_SERVER["REQUEST_METHOD"] == "POST"){ // followed by my code for the form 

但是,当我使用这种语法提交表单时,我会被重定向到

www.[website_name].com/index.php

通过谷歌搜索,我在几个地方找到了尝试

<form method="post" action="<?php echo get_permalink(); ?>">

我被重定向到http://[website_name].com/contact/ 但在这种情况下也没有提交表格。 我也试过绝对网址 -

www.[website_name].com/wp-content/themes/[theme-name]/contact.php
www.[website_name].com/contact.php
www.[website_name].com/contact

但一切都是徒劳的,有人可以帮我解决这个问题吗? 抱歉,我以前使用过 get_permalink,但即使是永久链接也不适合我,这里是我正在使用的完整代码 -

<?php
    /*
        Template Name: Contact
    */
    ?>
    <?php
        $errormsg="";
        if ($_SERVER["REQUEST_METHOD"] == "POST"){
            if (!empty($_POST["name"]))
                $name=$_POST["name"];
            if (!empty($_POST["number"]))
                $number=$_POST["number"];
            if (!empty($_POST["address"]))
                $address=$_POST["address"];
            if (!empty($_POST["purpose"]))
                $purpose=$_POST["purpose"]; 
            require_once('PHPMailer_5.2.4/class.phpmailer.php');    
            $mail = new PHPMailer();    
            $body="test body2";
            $mail->IsSMTP();
            $mail->SMTPAuth   = true;                  // enable SMTP authentication
            $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
            $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
            $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
            $mail->Username   = "harshitladdha93@gmail.com";  // GMAIL username
            $mail->Password   = "[password]";            // GMAIL password  
            $mail->SetFrom("harshitladdha93@gmail.com", 'Harshit Laddha');
            $mail->AddReplyTo("harshitladdha93@gmail.com","First Last");
            $mail->Subject    = "subject";
            $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test   
            $mail->MsgHTML($body);
            $address = "maniyogi764.com@gmail.com";
            $mail->AddAddress($address, "John Doe");
            if(!$mail->Send()) {
                $errormsg="mail sent"; 
            } else {
                $errormsg="some error occurred, please contact webmaster at contact@exoticalstudio.com.";echo $mail->ErrorInfo;
            }                           
        }
    ?>
    <?php get_header(); ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url');?>/css/contact/css/style.css" />
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url');?>/css/contact/css/animate-custom.css" />
    <link type="text/css" rel="stylesheet" href="<?php bloginfo('template_url');?>/css/contact/css/styles/form.css?v3.1.1082"/>
    <style type="text/css">
            .form-label{
                width:150px !important;
            }
            .form-label-left{
                width:150px !important;
            }
            .form-line{
                padding-top:12px;
                padding-bottom:12px;
            }
            .form-label-right{
                width:150px !important;
            }
            .form-all{
                width:650px;
                color:#FFFFFF !important;
                font-family:'Verdana';
                font-size:12px;
            }
            .form-radio-item label, .form-checkbox-item label, .form-grading-label, .form-header{
                color:#FFFFFF;
            }

    </style>
    <div class="content" style="top:0px" >
        <div class="designs" style="color:#646464;">
            <div id="wrapper" style="width:60%;margin-left:25%;text-align:left;">
                <div id="login" class="animate form">
                    <form method="post" action="<?php the_permalink(); ?>">
                          <h1><?php if (!empty($_POST["name"])) echo $errormsg; else echo "Connect with us!"?></h1>
                          <div class="form-all">
                            <ul class="form-section">
                              <li class="form-line">
                                <label class="form-label-left" for="name">
                                  Full Name<span class="form-required">*</span>
                                </label>
                                <div class="form-input"><span class="form-sub-label-container"><input class="form-textbox validate[required]" type="text" size="30" name="name" id="name" /></span>
                                </div>
                              </li>
                              <li class="form-line">
                                <label class="form-label-left"  for="number"> Phone Number </label>
                                <div class="form-input"><span class="form-sub-label-container"><input class="form-textbox" type="tel" name="number" id="number" size="10">
                                 </span>
                                </div>
                              </li>
                              <li class="form-line">
                                <label class="form-label-left" id="label_7" for="address">
                                  Address
                                </label>
                                <div class="form-input">
                                  <input id="address" class="form-textarea " name="address"/>
                                </div>
                              </li>
                              <li class="form-line">
                                    <label class="form-label-left"  for="textarea">
                                      Purpose<span class="form-required">*</span>
                                    </label>
                                    <div class="form-input">
                                      <textarea id="purpose" class="form-textarea validate[required]" name="purpose" cols="35" rows="6"></textarea>
                                    </div>
                                  </li>
                              <li class="form-line">
                                <div class="form-input-wide">
                                  <div style="margin-left:156px" class="form-buttons-wrapper">
                                    <button id="submit" type="submit" class="form-submit-button">
                                      Submit
                                    </button>
                                  </div>
                                </div>
                              </li>

                              <li style="display:none">
                                Should be Empty:
                                <input type="text" name="website" value="" />
                              </li>
                            </ul>
                          </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <script src="<?php bloginfo('template_url');?>/js/cbpTooltipMenu.min.js"></script>
    <script>
        var menu = new cbpTooltipMenu( document.getElementById( 'cbp-tm-menu' ) );
    </script>
    <?php get_footer(); ?>

【问题讨论】:

    标签: php wordpress forms


    【解决方案1】:

    您的名称和 ID 为:&lt;input class="form-textbox validate[required]" type="text" size="30" name="your_name" id="your_name" /&gt;name="name"id="name",我只是更改了名称和 ID 值。因为name属性本身就是一个关键字所以产生了错误。

    <?php
        /*
            Template Name: Contact
        */
        ?>
        <?php
            $errormsg="";
            if ($_SERVER["REQUEST_METHOD"] == "POST"){
                if (!empty($_POST["your_name"]))
                    $your_name=$_POST["your_name"];
                if (!empty($_POST["number"]))
                    $number=$_POST["number"];
                if (!empty($_POST["address"]))
                    $address=$_POST["address"];
                if (!empty($_POST["purpose"]))
                    $purpose=$_POST["purpose"]; 
                require_once('PHPMailer_5.2.4/class.phpmailer.php');    
                $mail = new PHPMailer();    
                $body="test body2";
                $mail->IsSMTP();
                $mail->SMTPAuth   = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
                $mail->Username   = "harshitladdha93@gmail.com";  // GMAIL username
                $mail->Password   = "[password]";            // GMAIL password  
                $mail->SetFrom("harshitladdha93@gmail.com", 'Harshit Laddha');
                $mail->AddReplyTo("harshitladdha93@gmail.com","First Last");
                $mail->Subject    = "subject";
                $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test   
                $mail->MsgHTML($body);
                $address = "maniyogi764.com@gmail.com";
                $mail->AddAddress($address, "John Doe");
                if(!$mail->Send()) {
                    $errormsg="mail sent"; 
                } else {
                    $errormsg="some error occurred, please contact webmaster at contact@exoticalstudio.com.";echo $mail->ErrorInfo;
                }                           
            }
        ?>
        <?php get_header(); ?>
        <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url');?>/css/contact/css/style.css" />
        <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url');?>/css/contact/css/animate-custom.css" />
        <link type="text/css" rel="stylesheet" href="<?php bloginfo('template_url');?>/css/contact/css/styles/form.css?v3.1.1082"/>
        <style type="text/css">
                .form-label{
                    width:150px !important;
                }
                .form-label-left{
                    width:150px !important;
                }
                .form-line{
                    padding-top:12px;
                    padding-bottom:12px;
                }
                .form-label-right{
                    width:150px !important;
                }
                .form-all{
                    width:650px;
                    color:#FFFFFF !important;
                    font-family:'Verdana';
                    font-size:12px;
                }
                .form-radio-item label, .form-checkbox-item label, .form-grading-label, .form-header{
                    color:#FFFFFF;
                }
    
        </style>
        <div class="content" style="top:0px" >
            <div class="designs" style="color:#646464;">
                <div id="wrapper" style="width:60%;margin-left:25%;text-align:left;">
                    <div id="login" class="animate form">
                        <form method="post" action="<?php the_permalink(); ?>">
                              <h1><?php if (!empty($_POST["name"])) echo $errormsg; else echo "Connect with us!"?></h1>
                              <div class="form-all">
                                <ul class="form-section">
                                  <li class="form-line">
                                    <label class="form-label-left" for="name">
                                      Full Name<span class="form-required">*</span>
                                    </label>
                                    <div class="form-input"><span class="form-sub-label-container"><input class="form-textbox validate[required]" type="text" size="30" name="your_name" id="your_name" /></span>
                                    </div>
                                  </li>
                                  <li class="form-line">
                                    <label class="form-label-left"  for="number"> Phone Number </label>
                                    <div class="form-input"><span class="form-sub-label-container"><input class="form-textbox" type="tel" name="number" id="number" size="10">
                                     </span>
                                    </div>
                                  </li>
                                  <li class="form-line">
                                    <label class="form-label-left" id="label_7" for="address">
                                      Address
                                    </label>
                                    <div class="form-input">
                                      <input id="address" class="form-textarea " name="address"/>
                                    </div>
                                  </li>
                                  <li class="form-line">
                                        <label class="form-label-left"  for="textarea">
                                          Purpose<span class="form-required">*</span>
                                        </label>
                                        <div class="form-input">
                                          <textarea id="purpose" class="form-textarea validate[required]" name="purpose" cols="35" rows="6"></textarea>
                                        </div>
                                      </li>
                                  <li class="form-line">
                                    <div class="form-input-wide">
                                      <div style="margin-left:156px" class="form-buttons-wrapper">
                                        <button id="submit" type="submit" class="form-submit-button">
                                          Submit
                                        </button>
                                      </div>
                                    </div>
                                  </li>
    
                                  <li style="display:none">
                                    Should be Empty:
                                    <input type="text" name="website" value="" />
                                  </li>
                                </ul>
                              </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <script src="<?php bloginfo('template_url');?>/js/cbpTooltipMenu.min.js"></script>
        <script>
            var menu = new cbpTooltipMenu( document.getElementById( 'cbp-tm-menu' ) );
        </script>
        <?php get_footer(); ?>
    

    【讨论】:

    • 我试过 the_permalink();太抱歉我用了 get_permalink();之前没有 id,但即使此代码也无法正常工作 - 您可以在 www.exoticalstudio.com/contact 上查看该页面
    • 我认为此操作不起作用,因为联系人是我的自定义 wordpress 主题中带有自定义模板的页面
    • 我用我正在使用的代码编辑了我的帖子,请查看并帮助。
    • 我已经添加了我的编码,就用它吧。这应该对你有用!
    • 您拥有以下名称和 ID: 作为 name="name" 和 id="name",我只是更改了 name 和 id 值。因为name属性本身就是一个关键词。所以产生了错误。
    【解决方案2】:

    替换以下内容:

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    

    以下内容:

    <form method="post">
    

    实际动作,当没有指定时,表单被提交到当前页面。

    【讨论】:

    • 我试过了,但它不起作用,请注意使用表单的邮件脚本工作正常,就像我删除 if ($_SERVER["REQUEST_METHOD"] == "POST")并尝试直接执行它。
    • if ($_SERVER["REQUEST_METHOD"] == "POST") 可以保留,直到您保留 method="post"。那么($_SERVER["REQUEST_METHOD"] == "POST") 肯定是true。所以它应该可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多