WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sending a form and getting POST data from a custom plugin (8 posts)

  1. albpara
    Member
    Posted 6 months ago #

    Hi all,

    I am new to WP world and I am starting developing a custom plugin that will show a form and after sending it, the data will be stores on DB but I don't know how to do that...

    I'm using the shortcode API so the forms are inserted into posts using something like [form name="my_form"]

    What will be the action attribute on the form? I'm using the_permalink() but if the form is sent and with the fields filled, wordpress shows an 404 error...

    My code is something like this:

    <br />
    if ( isset($_POST['send_button_name'] )<br />
    {<br />
        print_r( $_POST );<br />
    }<br />
    else<br />
    {<br />
        show_form();<br />
    }<br />

    If no field is filled, the code works and the post array is displayed but if an input is filled it shows me a 404 error.

    Can anyone help me on this?

    If this is not the correct way to do this (A plugin that shows a form a stores data on DB) I will be glad to hear other solutions.

    Thanks in advance

  2. Ashok
    WPMU DEV Support Staff
    Posted 6 months ago #

    Did you try the following as form action?

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>">
    
    </form>

    It will send the data to same page where the form is.

  3. albpara
    Member
    Posted 6 months ago #

    Thanks for your help Ashok,

    I know what $_SERVER['PHP_SELF'] was, I have some experience on PHP dev :).

    Of course I've tried that, that was my first thought, and the result was the same: and 404 error page if some input was filled.

    On both cases seems to work if the submit button was clicked without filling any input, that is weird.

    Thanks again

  4. bcworkz
    Member
    Posted 6 months ago #

    The action attribute as an empty string should suffice, it is VERY strange that form content causes a 404! While it's fine for plugins to store data in the DB, let's backup and think about the best way to submit a form. Submitting directly to a plugin's PHP file will not work well unless that file has no need for any WP resources.

    The plugin cannot know how to load the WP environment for any random installation because where the wp-content folder is located and named is variable. The only way a form can submit to its own page is as a page template. Page templates belong in the current theme's folder, which is another problem. It is possible for a template to reside elsewhere, but they will not show up in the page edit form's dropdown, which can be a good thing in some cases.

    Another option is use AJAX. There are some quirks of AJAX with WP, so don't dive into this solution until you review the WP way of doing AJAX. There's also admin-post.php, which is similar to AJAX but does not require javascript or jQuery.

    AFAIK these are the only 3 proper ways of directly accessing a custom code page with the WP environment. I don't know if using these will eliminate the 404 issue, but it might, and you need to start off on the right foot anyway.

  5. nickohrn
    Member
    Posted 6 months ago #

    When I've run into this in the past I've always had a form field that conflicted with a WordPress query var. Check you field names and make sure they're not something that would do the same.

  6. Ashok
    WPMU DEV Support Staff
    Posted 6 months ago #

    Is the form in front end or in back end? Do you have any link to share?

  7. albpara
    Member
    Posted 6 months ago #

    @bcworkz Thanks for the info, if everything fails I will use the AJAX solution, I've donde that before.

    @nickohrn I will check the post field names but I am almost sure that this is not the problem, when the form was submitted without filling the form, the $_POST array was shown and only the submit button field was present; in a few hours I will check that, thanks for pointing ;)

    @ashok The form is on the front end but it is on a local network. I can share the complete code I you want :)

    Thanks for the help guys!!

  8. albpara
    Member
    Posted 6 months ago #

    As @nickohrn suggested... the problem was a form field called name so be sure to use some unique form filds names for tying to avoid this problem,

    Thanks for the help!!!

Reply

You must log in to post.

About this Topic