• Hey, I have a client looking to add a couple specific forms to pages on her site, I tried to puzzle through this myself to no avail. Basically all I’m doing is allowing her to create the page with help text and terms etc. and then using the “the_content” hook to append a form to the page, this works like a charm, my problem is that when the “submit” button is pressed, the page reloads, the form seems to submit, but I get no post data in my plugin. I’ve added a snippet into the “init” hook to capture all post data and record it in the db to ensure it getting there, but it never even makes it that far. I then opened firebugs NET console and watched the post, there is a 301 Moved Permanently notice, and that is where I’m stumped, I tried pointing the action of the form directly to a php file that should accept it, but even that didn’t work. I’m dyin here! Does wordpress use a double post system? does it clear the post before it calls the init hook? any help would be much appreciated!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Anonymous User

    (@anonymized-3085)

    try adding
    global $_POST;
    to your processing function.

    Thread Starter zaphod-42

    (@zaphod-42)

    I’ve tried using global $_POST, also tried global $_REQUEST, they are both empty, wordpress is still relatively new to me, I’ve built a couple plugins and several widgets, I’m well versed in php and mysql, and have been developing on drupal for a couple years, I’ve also created several stand alone web apps. I worked a little more on it today with no progress, the only thing I can think is that it must have something to do with validation, does wordpress validate form data using some sort of form id system?

    Anonymous User

    (@anonymized-3085)

    Have tried simply echo’ing the $_POST to the screen to see if anything is there? I do this sometimes when I can’t figure things out, sometimes followed by an exit to ensure it gets seen.

    perhaps use the pasteebin and let me see your code?

    Thread Starter zaphod-42

    (@zaphod-42)

    um, like I said I’m dumping it into a db table…
    the relevant code is here:

    add_filter('the_content','pt_edit_content');
    add_action('init','pt_process_post');
    function pt_edit_content($content){
      if(!is_page('1176')){
        return $content;
      }
      $form='<form method="post" action=""><input type="hidden" name="form_id" value="affil_signup">';
      $fields=array('Name','Email','Link');
      foreach($fields as $field){
        $safe=strtolower($field);
        $form.='<div style="padding: 15px;"><label for="'.$safe.'-edit">'.$field.': </label><input id="'.$safe.'-edit" name="'.$safe.'" /></div>';
      }
      $form.='<input type="submit" value="Submit" /></form>';
      return $content.$form;
    }
    function pt_process_post(){
      global $_POST,$_REQUEST,$wpdb;
      $data=array(
        'post'=>$_POST,
        'request'=>$_REQUEST,
      );
      $wpdb->insert('pt_dump_table', array('data'=>serialize($data));
    }

    Like I said I’ve played with the action and had no luck, I’ve also used my own database management functions to insert rows from a php file I’ve pointed the action to with the same lack of results.

    Anonymous User

    (@anonymized-3085)

    No you’re missing the point, by echoing you see what is actually there – if there was an issue you may miss it by just adding them to the database.
    (well IMO).

    Thread Starter zaphod-42

    (@zaphod-42)

    I can’t echo, the redirect seems to be causing the whole thing to skip something. but serializing the post and inserting into the db has the same effect as echoing, except I can see if something was passed even if the echo doesn’t get to the screen, like I said I’m pretty well versed in php and sql, and this has been a very reliable way to troubleshoot forms in every system I’ve used…. well until now, I may have to just talk this client into drupal, I’ve never run into this kind of weirdness there

    Anonymous User

    (@anonymized-3085)

    Well yeah if you don’t exit, then you’ll never see it.

    In fact maybe echo anything to the screen and exiting might be a good idea to see if the code is actually being run, which like you say looks doubtful atm.

    Thread Starter zaphod-42

    (@zaphod-42)

    That’s nonsense, the variable can’t change from something outside of the scope before the scope exits anyway unless I call the function and send it as a reference or overwrite it, when I dump that variable to the database it has the exact same value as echoing it. The row is inserted before the function completes so the value of $_POST doesn’t change whether or not I echo and exit (which btw isn’t something I’m inclined to do in a customers production site).

    Anonymous User

    (@anonymized-3085)

    currently you are assuming that there is nothing in the $_POST as nothing gets entered into your database. What you still don’t know is if your function is even being run. hence my suggestion .

    But you could always add in a small debug

    if(isset($_GET['debug']))
       echo 'yes I'm running';

    into your function, then add ?debug to the form action(via firebug). then what ever you print to the screen will only be seen by you.

    You could try this as well $wpdb->print_error(); just in case the error lies in your query.

    Thread Starter zaphod-42

    (@zaphod-42)

    ok, I think I wasn’t clear in my earlier post….in the function I posted called “pt_process_post” I create an array to insert into the table, the row is inserted but the values are blank… the function is being called and the query is working. From that I can determine that when the submit button is pressed the init hook gets called 1 time before the page loads again, but when that hook is called the $_POST and $_REQUEST variables are empty. If I go to an admin page that I’ve created and submit a form, the values are inserted into the database as expected.

    Moderator cubecolour

    (@numeeja)

    This sounds like something which could be achieved very easily with gravity forms rather than a custom solution

    Anonymous User

    (@anonymized-3085)

    lol sorry zaphod – I had assumed nothing got inserted.

    Thread Starter zaphod-42

    (@zaphod-42)

    hey cubecolour,
    I appreciate that, and I have thought about it, and may do it if this doesn’t get worked out, more importantly than how I fix the problem, I’d like to know why it’s occurring so I can avoid it in the future on this, and other jobs.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Using a plugin to add forms to a page’ is closed to new replies.