Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon PHP post link with JavaScript confirmation dialog

I am developing a CRUD system in Phalcon PHP (version 1.3.4).

My goal is to create a link (delete row), that asks for confirmation on click (JavaScript confirmation box) and then goes (request type POST) to the link.

So lets say a user clicks on the "delete row" button.

  1. JavaScript confirmation "Are you sure you want to delete this row?"
  2. User clicks "yes"
  3. Webpage does a POST to "/users/delete/1"

I know CakePHP has a function (FormHelper::postLink()) that does exactly that.

I was wondering if Phalcon PHP also had a function like this.

like image 930
Tijme Avatar asked Jan 18 '15 13:01

Tijme


1 Answers

I see three possibilities to achieve what you want. One is to create a macro in Volt template, second is to add a function to your View. Third and closest to - what I understand is your wish - is to extend Phalcons tag helper and this is part I will describe here.

Phalcon has its own Tag helper to allow you to easily create some elements. postLink is not a part that is implemented there, but you can easily achieve it. In my example I have namespace of Application with class of Tag that extends from \Phalcon\Tag. This is my base for this tutorial.

// Tag.php

namespace Application;

class Tag extends \Phalcon\Tag
{

    static public function postLink() {
        return '<strong>TEST TAG</strong>';
    }
}

To force Phalcon DI to use this class, it is necessary to override it's standard declaration from engine by declaring it by hand as a new DI service:

// services.php
$di['tag'] = function() {
    return new \Application\Tag();
};

You can test if it is working properly by typing {{ tag.postLink() }} in Volt template or with $this->tag->postLink() if using phtml template.

Now you can fill your Tag::postLink() method with HTML and parameters you wish it will produce:

namespace Application;

class Tag extends \Phalcon\Tag
{

    static $forms = [];

    static public function postLink($title, $url, $options = array()) {

        // random & unique form ID
        while ($randId = 'f_' . mt_rand(9000, 999999)) {
            if (!isset(self::$forms[$randId])) {
                self::$forms[$randId] = true;
                break;
            }
        }

        // dialog message
        $dialogMessage = isset($options['message']) && $options['message'] ?  $options['message'] : 'Are you sure you want to go on?';


        $html = <<<HTML

            <form action="{$url}" method="post" id="{$randId}">
                <!-- maybe not necessary part -->
                <input type="hidden" name="confirmed" value="1" />
            </form>
            <a href="#" onclick="javascript: confirm('{$dialogMessage}') ? document.forms['{$randId}'].submit() : false;">{$title}</a>

HTML;

        return $html;
    }
}

Now you can run it like this:

{{ tag.postLink('delete', '/users/delete/1') }}

{% set formOptions = ['message' : 'Are you sure you want to delete user Kialia Kuliambro?'] %}
{{ tag.postLink('delete', '/users/delete/1', formOptions) }}

{{ tag.postLink('delete', '/users/delete/1', ['message' : 'Are you sure you want to delete user Kialia Kuliambro?']) }}

Have fun extending :)

like image 199
yergo Avatar answered Sep 24 '22 11:09

yergo