Jump to content

where to put my scripts?


froot
 Share

Recommended Posts

I have a php-script that should run after a form submission and in that php-script I would put a redirect to a success or error page respectively.

so I add <action='/_myscript.php'> to the form.

The script doesn't load though. What's the best practice for this? Where to put the script? root folder? site folder? template folder? and what would the path be because either way I try gives an 404 error or lack of access error.

My guess is it absolutely needs to be/have a page? 

Link to comment
Share on other sites

I think here are some different possibilities:

  • You create a template + page
  • You use the same template/page where your form is (And check it with the $input API)
  • Create a template / page for different functions (for multiple pages) - Here you have to use the $input API to decide what to do

Depending on how complex everything is, you have to decide how you want to use it ? 

  • Like 1
Link to comment
Share on other sites

3 minutes ago, fruid said:

My guess is it absolutely needs to be/have a page? 

It doesn't have to be, but it's certainly best practice. You can point your form directly to a PHP script, but you will have to bootstrap ProcessWire manually if you want to use the API. The PHP script also can't be in the template directory, as direct access to that is blocked by ProcessWire for security reasons – see my answer here.

How best to add custom logic to your forms depends on how your form is built:

  • FormBuilder? Use a hook to adjust how it processes the form,
  • Custom form? You can put your logic in the same page template where you build your form. As zoeck mentioned, use the $input API to check if the form has been submitted and handle the inputs there.
  • Like 1
Link to comment
Share on other sites

If you don't need the PW API to process the form, you can place your script in root.

action="<?=$config->paths->root . 'myscript.php'?>"

Although, as already mentioned, I'd suggest to create a PW page + dedicated template for such stuff. Together with URL segments, you can place all your form processing logic in one single file (or even include() each sep. form processing logic, in case there's many of them). Well, actually URL segments aren't really needed, you can just as well include a hidden field in your form, or add a ?form=support to your action URL. Many ways to skin a cat ?

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...