mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-05 19:12:42 +00:00
Created the method FormHelper::postLink() to create a link with form to send data via POST. This feature require javascript.
This commit is contained in:
parent
4c106490ef
commit
373fa780f7
2 changed files with 79 additions and 0 deletions
|
@ -1289,6 +1289,51 @@ class FormHelper extends AppHelper {
|
|||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an HTML link, but access the url using method POST. Requires javascript enabled in browser.
|
||||
*
|
||||
* ### Options:
|
||||
*
|
||||
* - `data` - Array with key/value to pass in input hidden
|
||||
* - Other options is the same of HtmlHelper::link() method.
|
||||
* - The option `onclick` will be replaced.
|
||||
*
|
||||
* @param string $title The content to be wrapped by <a> tags.
|
||||
* @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
|
||||
* @param array $options Array of HTML attributes.
|
||||
* @param string $confirmMessage JavaScript confirmation message.
|
||||
* @return string An `<a />` element.
|
||||
*/
|
||||
public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
|
||||
if (!empty($options['confirm'])) {
|
||||
$confirmMessage = $options['confirm'];
|
||||
unset($options['confirm']);
|
||||
}
|
||||
|
||||
$formName = uniqid('post_');
|
||||
$out = $this->create(false, array('url' => $url, 'name' => $formName, 'id' => $formName));
|
||||
if (isset($options['data']) && is_array($options['data'])) {
|
||||
foreach ($options['data'] as $key => $value) {
|
||||
$out .= $this->hidden($key, array('value' => $value, 'id' => false));
|
||||
}
|
||||
unset($options['data']);
|
||||
}
|
||||
$out .= $this->end();
|
||||
|
||||
$url = '#';
|
||||
$onClick = 'document.' . $formName . '.submit();';
|
||||
if ($confirmMessage) {
|
||||
$confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage);
|
||||
$options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }";
|
||||
} else {
|
||||
$options['onclick'] = $onClick;
|
||||
}
|
||||
$options['onclick'] .= ' event.returnValue = false; return false;';
|
||||
|
||||
$out .= $this->Html->link($title, $url, $options);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a submit button element. This method will generate `<input />` elements that
|
||||
* can be used to submit, and reset forms by using $options. image submits can be created by supplying an
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue