• Resolved krisxinx

    (@krisxinx)


    Hello there WP community,

    I have previously developed in PHP, but in my new job I need to learn to mess with lots of CMS core code, and now its time for wordpress.

    I dont wanna lose it so I need to be as quick as I can, but right now my head is a bit of a mess of ideas.

    I just need to make a custom form to insert data into some custom tables.

    After researching a bit, I concluded I need an ajax call to an external php file that makes the insert. But I’m struggling a bit on how to do that.

    Like, where am I suposed to put the file (wp own folders or form plugin folders?), and then how do I reference it in the ajax call? I dont know how WP handles urls/routes.

    Any help would be appreciated. Thanks!

    • This topic was modified 5 years, 9 months ago by krisxinx.
    • This topic was modified 5 years, 9 months ago by krisxinx.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Ajax in WP has a few quirks, please review the related section in the Plugin Handbook. If you can avoid using WP resources to handle your form data, your form will perform much better by not needing to load all of WP for every Ajax call. In such a case, it’s straight PHP. You can place such a file outside of the WP hierarchy, or in a theme or plugin folder if you created the related module. A custom folder in wp-content ought to be safe as well.

    To be clear, your form can be part of WP content, it’s the Ajax calls handling its data where avoidance has benefits.

    Nabil

    (@nabil_kadimi)

    So you want two things:
    – Show a form
    – Save form submission in SQL tables

    I don’t see any need for AJAX here unless required by your client.

    What you need is the code that does the 2 things (show and save), this requires knowledge of WordPress plugin development.

    Thread Starter krisxinx

    (@krisxinx)

    What I require is fairly simple:
    Show a form, click next, insert data, then show a user registration form.

    You are suposed to go back/next from one form to the other.

    Then the user should be able to modify some data of the first form.

    For what I need I could use code outside WP, but I need WP user management for security. And it’s not like I need to do lost of ajax calls.

    I really wonder what’s the easiest way to do this : /

    Moderator bcworkz

    (@bcworkz)

    Whether you use Ajax or not largely depends on the sort of user experience (UX) you are after. If you need WP functionality to handle the form submit, there’s something to be said about avoiding Ajax and handling the entire submit at once. Then the WP environment is only loaded once on submit. But UX is more important than how often WP is loaded as long as it’s not too time consuming.

    If you are not going the Ajax route (not the “easy” option anyway), and you need WP resources, you could handle the entire thing with a single custom page template that contains everything necessary to serve various forms and handle the submitted data.

    Template code can decide what to serve based on the request type. If GET, serve page one. If POST and page one fields are passed, handle the data and serve page two. If POST and page two fields are passed, handle the data and serve a thank you page and/or redirect elsewhere. I would consider this one of the easiest options.

    For security purposes, your form should include a hidden nonce field that is verified upon submit. Be sure to validate and sanitize all submitted data.

    Thread Starter krisxinx

    (@krisxinx)

    Thank you guys, I got it finally using code in a custom template.
    Have a nice day.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom forms in WordPress’ is closed to new replies.