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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Joachim
      Long time user and huge fan of PW, but this time I can't find an answer to my question this time:
      For my social media buttons, I have a Repeater field called var_link_web with two fields: one is for the URL, and the other is an Images field containing two images that are used as a background-image for a <div>, of which the second is the ':hover' version (although activated through JavaScript here). 
      There are four instances of this Repeater, of which two are 'turned off'.
      I use the following PHP in my _main.php to call them, wrapped in <p><?php == ?></p>:
      $s_m_button = $variables->var_link_web; foreach($s_m_button as $button){ $button_image = $button->var_link_image->first->height(80); $button_image2 = $button->var_link_image->eq(1)->height(80); echo " <a href='$button->var_link_url'> <div class='image_link' style='background-image:url({$button_image->url})' onMouseOver='this.style.backgroundImage=url({$button_image2->url})' onMouseOut='this.style.backgroundImage=url({$button_image->url})'> </div> </a>"; }; ($variables leads to an unpublished page with several fields I want to have easy access to, and is defined in _init.php.)
      However, this gives me the following result:

      I have no idea where the extra <p>'s come from. The URL field has the 'HTML Entity Encoder' turned on. What's even weirder is that the HTML source file seemingly renders correctly:
      <p> <a href='https://www.facebook.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1045/icons-facebook-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square.0x80.png)"'> </div> </a> <a href='https://www.instagram.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1046/icons-instagram-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square.0x80.png)"'> </div> </a> </p> Removing the JavaScript has no effect. I'm probably missing something obvious, but am at a loss here.
       
      Thank you in advance!
    • By Juergen
      Hello @ all
      I want to share a new module with you, which makes the creation and validation of forms easy.  Take a look at the following example of a simple contact form:
      // A very simple example of a contactform for demonstration purposes $form = new Form('contactform'); $gender = new Select('gender'); $gender->setLabel('Gender'); $gender->addOption('Mister', '0'); $gender->addOption('Miss', '1'); $form->add($gender); $surname = new InputText('surname'); $surname->setLabel('Surname'); $surname->setRule('required'); $form->add($surname); $name = new InputText('name'); $name->setLabel('Name'); $name->setRule('required'); $form->add($name); $email = new InputText('email'); $email->setLabel('E-Mail'); $email->setRule('required'); $form->add($email); $subject = new InputText('subject'); $subject->setLabel('Subject'); $subject->setRule('required'); $form->add($subject); $message = new Textarea('message'); $message->setLabel('Message'); $message->setRule('required'); $form->add($message); $privacy = new InputCheckbox('privacy'); $privacy->setLabel('I accept the privacy policy'); $privacy->setRule('required')->setCustomMessage('You have to accept our privacy policy'); $form->add($privacy); $button = new Button('submit'); $button->setAttribute('value', 'Send'); $form->add($button); if($form->isValid()){ print_r($form->getValues()); // do what you want } // render the form echo $form->render(); This piece of code creates a simple contact form and validates it according to the validation rules set. Inside the isValid() method you can run your code (fe sending an email)
      Highlights:
      30+ validation types Support for UiKit 3 and Bootstrap 5 CSS framework SPAM protection Highly customizable Hookable methods for further customization Multi-language You can download and find really extensive information on how to use at https://github.com/juergenweb/FrontendForms.
      Please report errors or suggestions directly in GitHub.
      Best regards and happy testing 🙂
    • By Marvin
      Hello, excuse me
      I'm new at processwire, i want to ask about a master detail system (Inheritance system) using a processwire. Can we create a Inheritance table using a processwire, and how we create it? Thank you for the help
    • By Marvin
      Hi,
      I'm still new at processwire, i want to ask, i was create a website with sign in system, but i want to add a access roles/permission for each user at my website. For now, i just can create a login user without any permission and the user data became as a pages in my processwire.
      Here i attach my code for login
      <?php $note = $note2 = $hidden =""; if($input->get->id == "logout") { $session->remove('login_id'); } if($input->post->submit) { $email = $sanitizer->email($input->post->email); $pass = $sanitizer->text($input->post->pass); $result = $pages->find("email=$email, text_1=$pass"); if(!$email || !$pass) { $note = "Data belum lengkap"; } else { if($result->count > 0) { $session->login_id = "$result"; $hidden = "style = 'diplay:none'"; $url=$pages->get("/files/")->httpUrl; header("Location:$url"); die(); } else { $note = "Akun tidak ditemukan"; } } } ?> And this code for login form
      <div class="frow-container"> <div class="frow centered mt-50"> <div class="col-md-1-3"> <div class="box p-40 shadow-light"> <h2 class="mb-20 text-center" <?=$hidden?>>Database Partitur<br>GII HIT</h2> <form method="post" <?=$hidden?>> <label>Username <input type="text" name="email"></label> <label>Password <input type="password" name="pass"></label> <input type="submit" name="submit" value="Masuk"> </form> <p class="notif"><?=$note;?></p> <div class="text-center"><?=$note2;?></div> </div> </div> </div> </div> Just for remember, my user data now is a pages, and i cannot give any permission to user data.
      Thank you very much for help.
    • By Marvin
      Hello,
      I'm new at process wire and i want to make an web using upload file and showing it at the table as a link to open it at the new tab. I wa succed while upload a file, but how i showing it as a link at the table to open it at the new tab of my browser? Any suggestion may helpfull
      Here i attach my code below :
      This code is for upload it to back-end (process wire)
      <?php $note = $note2 = $hidden =""; if($input->post->submit){ $upload_path = $config->paths->assets.'files/upload/'; if(!is_dir($upload_path)){ if(!wireMkdir($upload_path)) throw new WireException("No upload path"); } $original = $sanitizer->text($input->post->original); $indonesia = $sanitizer->text($input->post->indonesia); $other = $sanitizer->text($input->post->other); $composer = $sanitizer->text($input->post->composer); if(!$original || !$indonesia || !$other || !$composer){ $note = "Data tidak lengkap"; } else { $newFile = new WireUpload("song_files"); $newFile->setMaxFiles(1); $newFile->setOverwrite(false); $newFile->setDestinationPath($upload_path); $newFile->setValidExtensions(array('pdf','docx','doc')); $files = $newFile->execute(); if(!count($files)) { $newFile->error("No files received, so not creating page."); return false; } $newImg = new WireUpload("img_files"); $newImg->setMaxFiles(1); $newImg->setOverwrite(false); $newImg->setDestinationPath($upload_path); $newImg->setValidExtensions(array('jpeg','jpg','png','gif')); $files = $newImg->execute(); if(!count($files)) { $newImg->error("No files received, so not creating page."); return false; } $newPage = new Page(); $newPage->template = "files"; $newPage->parent = $pages->get("/files/"); $newPage->title = $original; $newPage->text_1 = $indonesia; $newPage->text_2 = $other; $newPage->text_3 = $composer; $newPage->of(false); $newPage->save(); foreach($files as $filename) { $filepath = $upload_path . $filename; $newPage->files->add($filepath); $newPage->message("Add file : $filename"); unlink($filepath); } $newPage->save(); } } ?> and this code to showing it as a link at the table
      <table class="border"> <tr> <th>No.</th> <th>Original Song Title</th> <th>Indonesia Song Title</th> <th>Other Song Title</th> <th>Composer</th> <th>File (pdf)</th> </tr> <?php $num = 1; $song; foreach($pages->get("/files/")->children as $child) { //showing every child at files parent directory $page == $child; $song = $pages->get("/files/".$child->id."/")->files; //showing uploaded files at child directory echo $child->id; echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$song->httpUrl."'</a>".$song->name."</td></tr>"; } ?> </table> Thank you for any suggestion
×
×
  • Create New...