Jump to content

Posting to a php folder without creating a page


nickngqs
 Share

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.

Link to comment
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
        }

        

    }

 

Link to comment
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.

Link to comment
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
Link to comment
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.

Link to comment
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?

Link to comment
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
Link to comment
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.

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

×
×
  • Create New...