Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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"

share|improve this question

2 Answers 2

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).

share|improve this answer
    
Thank you for your advice. I change my name fields to name ="thename" instead of name="name" and nothing change. In my example the isset($_POST['send']) doesn't work to ... –  Gilles Vauvarin Apr 9 '11 at 9:36
    
Updated answer, please add more of your code to the original question(no need to respond in comments, i'll check back periodically) –  t31os Apr 9 '11 at 11:25
    
If you try the test code i posted above does it work? > No :-( and no plugin is activated. –  Gilles Vauvarin Apr 11 '11 at 7:38
    
Again, works just fine for me, of course noting i changed the name input to myname instead, fine in the sense the $_POST vars are being set correctly.. var_dump( $_POST ) or print_r( $_POST ) shows me the correct post vars when i submit the form. –  t31os Apr 11 '11 at 9:18
    
The problem come from the "action" form. If i assign get_permalink() to "action", i retrieve my $_POST form data. –  Gilles Vauvarin Apr 11 '11 at 9:30

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
              }
                ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.