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
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By iipa
      Hello forum!
      I have a site, where I want to remember two settings defined by user:
      1) Theme (light or dark)
      2) Language (Finnish or English)
      Because user makes changes to these settings on client side, I am a bit lost with how can I save them in ProcessWire $session variable? I would like to use $session for more reliable saving, and since it is only two variables I will use, I doubt it will become too resource-needy.
      I have tried using jQuery's post() -method referring to a file in Templates folder (theme.php), but I get 403 Forbidden Error. I don't like the idea of trying to post to same file user currently is in, which is why I thought having a separate file would be good in this.
      Contents of theme.php:
      <?php namespace ProcessWire; header('Access-Control-Allow-Origin: https://domain.com'); $theme = $input->post['theme']; if(!empty($theme)) $session->theme = $theme; ?> Long story short: Does anybody have any pro tips I could use with setting and getting $session variables?
    • By louisstephens
      So I have been hard at work creating url segments for a template (api) and everything is working swimmingly in creating a simple end point for svelte.js. I have however, run into a few questions that I can wrap my head around.
      In my api template I have:
      if($input->urlSegment1 === 'clients') { header('Content-Type: application/json'); $clients = $pages->find("template=clients"); $client_array = array(); foreach ($clients as $client) { $id = $client->id; $title = $client->title; $url = $client->url; $clientName = $client->client_name; $clientColor = $client->client_color->value; $assigned = $client->assigned_to->user_full_name; $client_array[] = array( 'id' => $id, 'code' => $title, 'name' => $clientName, 'associated_users' => $assigned, 'url' => $url ); } $client_json = json_encode($client_array, true); echo $client_json; } The output json from this is:
      [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was curious is it possible to add in "clients" before this output json so it would appear as 
      clients: [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was not really sure of how to tackle this in my php code, and have spent more time than I care to admit trying to figure it out. Another question I have is that "associated_users" is returning null, which in this instance is correct. It is a multi page field that is set to pull a custom name field from the users template, ie "Louis Stephens" would be associated with the first page. I understand that I need to use a foreach to get the correct data, but I was really unsure of how to place this inside an array, or update the array with the new data. Any help with any of this would greatly be appreciated.
    • By Mithlesh
      Changing it to null
×
×
  • Create New...