Harshit Laddha
Harshit Laddha

Reputation: 2124

How to submit a form to itself in wordpress

I have a wordpress theme i designed, i have a contact form at the address -

www.[website_name].com/contact

and i have a form definition in it which says -

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

and the way i am determining the post method using

if ($_SERVER["REQUEST_METHOD"] == "POST"){ // followed by my code for the form 

But, when i submit the form with this syntax i am redirected to

www.[website_name].com/index.php

By googling it, i found at several places to try out

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

I get redirected to http://[website_name].com/contact/ but the form is not submitted in that case too. I also tried absolute url -

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

but all in vain, can anyone help me with this one? Sorry i used get_permalink before, but even the permalink is not working for me here is the full code i am using -

<?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   = "[email protected]";  // GMAIL username
            $mail->Password   = "[password]";            // GMAIL password  
            $mail->SetFrom("[email protected]", 'Harshit Laddha');
            $mail->AddReplyTo("[email protected]","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 = "[email protected]";
            $mail->AddAddress($address, "John Doe");
            if(!$mail->Send()) {
                $errormsg="mail sent"; 
            } else {
                $errormsg="some error occurred, please contact webmaster at [email protected].";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(); ?>

Upvotes: 4

Views: 8899

Answers (2)

Debakant Mohanty
Debakant Mohanty

Reputation: 825

You were having the name and id for : <input class="form-textbox validate[required]" type="text" size="30" name="your_name" id="your_name" /> as name="name" and id="name", I just changed the name and id values. Because the name attribute is a keyword itself, so that was generating the error.

<?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   = "[email protected]";  // GMAIL username
            $mail->Password   = "[password]";            // GMAIL password  
            $mail->SetFrom("[email protected]", 'Harshit Laddha');
            $mail->AddReplyTo("[email protected]","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 = "[email protected]";
            $mail->AddAddress($address, "John Doe");
            if(!$mail->Send()) {
                $errormsg="mail sent"; 
            } else {
                $errormsg="some error occurred, please contact webmaster at [email protected].";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(); ?>

Upvotes: 4

Rajesh Paul
Rajesh Paul

Reputation: 7009

replace the following:

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

With the following:

<form method="post">

Actually action when not specified then the form is submitted to the current page.

Upvotes: 2

Related Questions