A form in a custom widget

I’m trying to build a contact form custom widget. So i would like to display a contact form with a WordPress widget and send a message.

No problem to display the form with my widget but when i click on the send button i can’t retrieve my data. The isset($_POST[‘my_data’]) doesn’t work.

Read More

When I put my form code out of the widget in a simple php file, everything is ok, the isset($_POST[‘my_data’]) work well.

I do the test with this simple snippet:

<?php

//$form_action = '';
$form_action = get_bloginfo('url').'#contactBox';

if (isset($_POST['send'])) {
echo "The isset 'send' work well 
";
$name = (isset($_POST['name'])) ? $_POST['name'] : '';
echo "name = ".$name." ";
} else {
echo "There is something WRONG !!!";
}

// Display the form
echo '<form method="post" action="'.$form_action.'">'."n";
echo '  <p>'."n";
echo '  <label for="name">Name</label>'."n";
echo '  <input type="text" name="name" value="'.$name.'" />'."n";
echo '  </p>'."n";
echo '  <div style="text-align:center;"><input class="submit" type="submit" name="send" value="Send" /></div>'."n";
echo '</form>'."n";

?>

Is it possible to implement a form with a WordPress custom widget ?

This is the complete code of my custom widget with the contact form: www.pixenjoy.com/documents/widget.zip

I add two echo messages in this code to test the retrieve of my data form (if(isset…) > line 145 “ok” line 190 “ko”

Related posts

Leave a Reply

2 comments

  1. I remember a support topic i read with regard to a problem when you use the name name for one of your post fields, try prefixing your input names.

    <input type="text" name="my-name"  ...
    

    And see if that helps..

    Follow-up #1

    I performed a simple test inside a widget..

    if( isset( $_POST ) ) {
        print'<pre>';print_r( $_POST );print'</pre>';
    }
    echo '<form action="'.home_url().'" method="post"><input type="text" name="thename" /><input type="submit" name="submit" /></form>';
    

    I see the post variables that have been set by the form just fine, post up more of your applicable code please.

    Follow-up #2

    If you try the test code i posted above does it work? If it does, then i’d guess at an issue with either form action of the submit input name, try hard-coding the action or changing the submit input to match my test one, and see if either change fixes the issue.

    If neither help, next i’d suggest disabling plugins to rule out interferance(a plugin could be unsetting $_POST variables at some point(just a wild guess, but a possibility nonetheless).

  2. It seems the problem is with the “form action”. I have previously implemented a form inside the widget. (I am using WP_Widget Class)

    I left the action blank.. which means the $_POST data is submitted to the widget function. One can write form processing logic inside the widget function.

          <?php
           function widget($args, $instance){
            Form Processing Logic here  
             if isset ($_POST)  {  do   }
              //Display complete form here like this
    
              ?>
             <form method="post" id="form_id" action = "" >
    
         Email:<input name="email" id="email" class="a" value=" email"/>
              <br /><br />
                 <input type="submit" value="subscribe"  class="b"/>  
          </form>
             </div>
    
          <?php
                  }
                    ?>