如何在wordpress中向自己提交表单

时间:2014-01-03 13:55:11

标签: php wordpress forms

我有一个我设计的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(); ?>

2 个答案:

答案 0 :(得分:2)

您的名称和ID为<input class="form-textbox validate[required]" type="text" size="30" name="your_name" id="your_name" /> 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(); ?>

答案 1 :(得分:0)

替换以下内容:

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

以下内容:

<form method="post">

实际上未指定时执行操作,然后将表单提交到当前页面。