0

I have creating a template page in which i have a form, upon testing it gave a 404 error. should i putting something else in action?

`<form action="results.php" method="post">
    <!--my code-->
</form>`
  • It would help to know what context this form is in. – s_ha_dum Aug 24 '13 at 21:59
  • @s_ha_dum couple of Questions, in form on MCQ's and when clicked submit, i will pull results from categories based on selected radio buttons – Nofel Aug 24 '13 at 22:02
  • What is "MCQ"? Does the form appear on only one page or on multiple pages? – s_ha_dum Aug 24 '13 at 22:04
  • @s_ha_dum MCQ are multiple choice questions, like a question says, what is the day of week today? and you have couple of radio button which says the days and you choose one. that is MCQ :), yes the pages i have are two, one is form which has mcq and other will have the results. (pulled from category based on choices made) – Nofel Aug 24 '13 at 22:10
3

i putting something else in action?

Yes. The complete path to the file. If your site is http://mysite.com and you are on the home page then you are requesting a file at http://mysite.com/result.php. If you are on a date archive you'd be requesting a file at http://mysite.com/2013/03/03/result.php. The file you want is actually at http://mysite.com/wp-content/themes/theme-name/result.php. See the problem?

Relative URLs do not work well in a WordPress context.

But I would say that even if you were to construct a complete URL to the template file that you are still doing it wrong. If you load a file like that then none of the WordPress core loads and you tend to get undefined function errors.

The right way to do this, in my opinion, is ...

  1. to send the request to the same template file as contains the form.

    <form action="<?php the_permalink(); ?>" method="post"> 
    

    That particular code only works in some circumstances so you may need to construct the URL differently, using site_url(), for example. You can usually just omit the action attribute (as prompted by Milo).

  2. You could also hook a function to template_redirect or wp_head and process the form on that hook.
  3. Or, use the AJAX API to process the form submission. Of course, that means some Javascript as well.

If you form appears on more than one page you will need one of option 2 or 3. Otherwise, use the first option.

If you need the form to appear on one page and the results on another, then you will need a custom page template for each component, and you will need to create a page for the form and another for the results via the "Pages" section of the WordPress backend.

| improve this answer | |
  • 1
    You can omit the action attribute altogether and the form will always submit to the current URI. – Milo Aug 24 '13 at 21:39
  • @s_ha_dum i don't know option 2 or 3 as not familiar with it. however if i use <form action="<?php the_permalink(); ?>" method="post"> how will it know which one to get as i haven't told it which page has the result. – Nofel Aug 24 '13 at 21:40
  • @Milo : yeah, you can, but I don't like to... personality quirk maybe. – s_ha_dum Aug 24 '13 at 21:52
  • @AmmarKhan : If you go with option 1 your form processing code must be in the same file as the form. – s_ha_dum Aug 24 '13 at 21:57
  • @s_ha_dum the page not found. here is my code – Nofel Aug 25 '13 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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