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 picarica
      so i am trying to fetch dimensions of image using getimagesize() but it returns nothing, at least i think so, i want to feed it to data attribute, bit its emtpy, i tried feeding it the image directly or just image->url
      here is my source code

       
      foreach($page->repeat_body as $r_body) { /* other code here*/ if ($r_body->gallery_check == 1) { echo "<div class='my-gallery' id='grid-gallery' itemscope itemtype='http://schema.org/ImageGallery'>"; foreach($r_body->image as $image) { $options = array('quality' => 80, 'upscaling' => true, 'cropping' => 'north', 'sharpening'=>'medium'); $thumb = $image->size(400, 400, $options); $large = $image->size(1280, 0, $options); list($width, $height) = getimagesize($image->url); echo " <figure itemprop='associatedMedia' itemscope itemtype='http://schema.org/ImageObject'> <a href='$large->url' itemprop='contentUrl' data-size='$widthx$height' data-index='0'> <img src='$thumb->url' height='$height' width='$width' itemprop='thumbnail' alt='Beach'> </a> </figure>"; } echo "</div>"; } /* other code here*/ } now, the images are outputted correctly, i can open then and browse them
    • By 997 998 999
      Hello,
      I'm trying to edit the field of a page with a value from a javascript fetch post request but I can't get the
      file_get_contents("php://input") or
      $_POST value in my PHP file.
      I created a `update` template and a `update` page (to allow access from fetch request and avoid 404/403/... errors) containing this code :
      $data = file_get_contents('php://input'); echo $data; # test edit the page field with random value #$home = $pages->get("/"); #$home->of(false); #$home->testfield = "test value"; #$home->save(); # test fetch response with random value #echo "test fetch response"; That I fetch request with my client javascript like this :
      fetch('/update', { method: 'POST', body: 'test' }) .then(function(response) { return response.text(); }) .then(function(data) { console.log(data); }) .catch(error => console.log('error')) } The field edit works fine and I get the test response from the `update` file but I can't get the posted value, I only get `<empty string>`. It is working fine outside of ProcessWire so I guess this is related to how PW handle $_POST ?
      I found this post where it seems to be working :
       
      Is there any way to get post value from javascript fetch request? What am I doing wrong?
       
      Thank you
       
       
       
    • By franciccio-ITALIANO
      Hello to all. I would like to create an app. So I need to learn at least one programming language. I got informed online, and discovered that javascript with node.js, is the revolution of recent years, because it's faster than php. I wonder: if I develop an app with javascript and with a javascript framework (e.g. Meteor), is there a way to integrate processwire work? I know that processwire supports the transformation of the site into an application, but would it be as simple as Meteor? With the Meteor framework I have my app online in 10 minutes, and without even knowing javascript! (Knowing javascript would serve to personalize it). I should then install the app in a SUB-DOMAIN. If I study php, instead, and if I use a php framework (e.g. Laravel), how long does it take to have my first working app? Is it easy to process Laravel's components? Is writing forms for processwire apps with php a very complex job? Is it better to use Meteor and start with javascript? What would you recommend?
    • 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

       
       
×
×
  • Create New...