Jump to content
nickngqs

Posting to a php folder without creating a page

Recommended Posts

I'm trying to create a form that does a post to a php file, getting the values before posting to a database.


For example a form with an action /controller/add_client.php.

My current workaround is create a page with the add_client template. But is it possible to do that without creating a page? I.E i do not want it to show on the CMS side.

Share this post


Link to post
Share on other sites

Yes, you can use the database class: https://processwire.com/api/ref/database/

You can create a module (recommended) or do it directly on your template. See this example:

public function saveViews(Page $page)
    {
        $db = $this->wire('database');
        $table = "table-name";

        $sql = "SELECT page_id, COUNT(page_id) as total_views
                FROM $table
                WHERE page_id=:page_id";

        $query = $db->prepare($sql);
        $query->bindValue(':page_id', $page->id, \PDO::PARAM_INT);

        try {
            $query->execute();

            while ($set = $query->fetch(\PDO::FETCH_ASSOC)) {
                $new_views = $set['total_views'];
                $current_views = $page->page_views;
                $page->setOutputFormatting(false);
                $page->page_views = $current_views + $new_views;
                $page->save('page_views');
                $this->wire('log')->save("messages", $new_views.' views imported for page '.$page->title);
            }

        } catch(\Exception $e) {
            // intentionally blank
        }

        

    }

 

Share this post


Link to post
Share on other sites
3 minutes ago, Sergio said:

Yes, you can use the database class: https://processwire.com/api/ref/database/

You can create a module (recommended) or do it directly on your template. See this example:


public function saveViews(Page $page)
    {
        $db = $this->wire('database');
        $table = "table-name";

        $sql = "SELECT page_id, COUNT(page_id) as total_views
                FROM $table
                WHERE page_id=:page_id";

        $query = $db->prepare($sql);
        $query->bindValue(':page_id', $page->id, \PDO::PARAM_INT);

        try {
            $query->execute();

            while ($set = $query->fetch(\PDO::FETCH_ASSOC)) {
                $new_views = $set['total_views'];
                $current_views = $page->page_views;
                $page->setOutputFormatting(false);
                $page->page_views = $current_views + $new_views;
                $page->save('page_views');
                $this->wire('log')->save("messages", $new_views.' views imported for page '.$page->title);
            }

        } catch(\Exception $e) {
            // intentionally blank
        }

        

    }

 

Hi, thanks for the insight. But not, what I'm looking for. (But it does help with my later part of the stage)

But for now, currently, when doing a <form action='add_client.php> </form>. Processwire search for pages instead. So I'll need to create a page named add_client with a add_client template to make it work. How to I ask the form to post straight to add_client.php without adding a page.
 

10 minutes ago, Fokke said:

Hi!

You can use URL Segments to archieve this.

Thanks, I'll looking into this.

Share this post


Link to post
Share on other sites

You cannot post to nothing :). You have to post your client-request to something server-side to handle that request. That something can be a module page or a 'normal' (hidden, in admin, etc) page.

A URL Segment alone won't work. The name gives it away. Segment of a URL. A URL points somewhere. In ProcessWire, it points to a page. So, yes, you will need a page somewhere in the system.

  • Like 1

Share this post


Link to post
Share on other sites

Why not post to the page that contains the form? Then just check if the user posted the form, and send the data to the database...

Share this post


Link to post
Share on other sites
9 minutes ago, kongondo said:

You cannot post to nothing :). You have to post your client-request to something server-side to handle that request. That something can be a module page or a 'normal' (hidden, in admin, etc) page.

A URL Segment alone won't work. The name gives it away. Segment of a URL. A URL points somewhere. In ProcessWire, it points to a page. So, yes, you will need a page somewhere in the system.

Yes, I'm awared of that. In normal setup, in a non Processwire website. I'll just put a add_client.php in a folder, and form post to that add_client.php which will provide that server-side handling for that request.

But the issue I'm getting is that since Processwire routes everything according to the pages you've created. How can I route that url to add_client.php file without creating a page.

Currently, my work around is creating a page called add_client.php

I'm just curious if there's a better way to do this.

Share this post


Link to post
Share on other sites

Merely curious, but I was wondering why you don't handle the request with the same page (action="./") ?

 

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, nickngqs said:

Currently, my work around is creating a page called add_client.php

I'm just curious if there's a better way to do this.

Are you able to post your workaround so that someone can review and may be able to improve upon your methods?

Share this post


Link to post
Share on other sites
1 hour ago, nickngqs said:

I'll just put a add_client.php in a folder, and form post to that add_client.php which will provide that server-side handling for that request.

From a security standpoint, ProcessWire will not allow you to directly access PHP files in a number of its protected folders (including the site/templates folder) unless that file is a template file. You can read more about it here. In that thread you'll also find a number of workarounds.

1 hour ago, nickngqs said:

How can I route that url to add_client.php file without creating a page.

Only if it's outside PW directories. See option #1 in the post I linked to above. This means you won't have access to PW's API, unless you bootstrap it, which might not be worth the effort, if all you want to avoid is creating a page .:).

Many people often use option #3. Post to self (same page) and include your add_client.php in that page's template file (e.g., require_once('/path/to/add_client.php/')).

Edited by kongondo
  • Like 5

Share this post


Link to post
Share on other sites

@kongondo Ahh, I see! Alright, I guess I'll just created that page.
 

12 hours ago, cstevensjr said:

Are you able to post your workaround so that someone can review and may be able to improve upon your methods?

My workaround was to create that page with the template as I've written on top.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By Marvin
      Hello, i want to ask, i maintain a website that using a processwire and php, and i want to make an archive at my website using a subfolder system, but when i try,
      the sebfolder is show but when i click the files in that subfolder not show, and my browser just show me an error Invalid argument supplied for foreach(), i don't know why it error
      Here i attach my code and my screenshoot website :
      This is my code

      This is result of my website

      This is my error

       
       
    • By dimitrios
      Hello,
      this module can publish content of a Processwire page on a Facebook page, triggered by saving the Processwire page.
      To set it up, configure the module with a Facebook app ID, secret and a Page ID. Following is additional configuration on Facebook for developers:
      Minimum Required Facebook App configuration:
      on Settings -> Basics, provide the App Domains, provide the Site URL, on Settings -> Advanced, set the API version (has been tested up to v3.3), add Product: Facebook Login, on Facebook Login -> Settings, set Client OAuth Login: Yes, set Web OAuth Login: Yes, set Enforce HTTPS: Yes, add "https://www.example.com/processwire/page/" to field Valid OAuth Redirect URIs. This module is configurable as follows:
      Templates: posts can take place only for pages with the defined templates. On/Off switch: specify a checkbox field that will not allow the post if checked. Specify a message and/or an image for the post.
      Usage
      edit the desired PW page and save; it will post right after the initial Facebook log in and permission granting. After that, an access token is kept.
       
      Download
      PW module directory: http://modules.processwire.com/modules/auto-fb-post/ Github: https://github.com/kastrind/AutoFbPost   Note: Facebook SDK for PHP is utilized.


    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By CareerTeam GmbH
      Hi there,
      We are an executive search agency based in Germany looking for a freelancer (2-5 days per week) supporting us with the development and design of our websites. The position will be located in Hamburg, Germany and it would be great if you are on short call. German language knowledge is mandatory. 
      You can reach me via email jobs@careerteam.de.
      Thank you!
      Regards
      Annemie
×
×
  • Create New...