How do I bind to onclick events for buttons in Mojolicious? - perl

I'm trying to make a small webapp to control the command-line music player I use (mocp), but I'm having trouble hitting routes when the user presses a button on the page. I expect the problem is related to my lack of HTML.
Code:
#!/usr/bin/perl
use Mocp;
use Mojolicious::Lite;
my $mocp = Mocp->new;
for my $action ( qw( play pause stop next previous unpause toggle-pause server ) ) {
get $action => sub {
my $self = shift;
$mocp->$action;
$self->render( 'controls' );
};
}
get '/' => sub { shift->render( 'controls' ) };
app->start;
__DATA__
## controls.html.ep
%= input_tag 'play', type => 'button', value => 'Play'
%= input_tag 'pause', type => 'button', value => 'Pause'
%= input_tag 'volup', type => 'button', value => 'Volume Up'
%= input_tag 'voldown', type => 'button', value => 'Volume Down'
%= input_tag 'toggle', type => 'button', value => 'Toggle'
%= input_tag 'next', type => 'button', value => '>>'
%= input_tag 'previous', type => 'button', value => '<<'
If I navigate to the routes directly (e.g. '/play'), everything works as expected. The goal is to simply click the 'Play' button to execute the associated method, rather than having to type the route out by hand. My questions at this stage are:
Is mapping these methods to routes the best way to go about this?
If so, how do I trigger a route with an 'onclick' button event as in JavaScript?
Any advice is much appreciated.

Using jQuery on client side you can trigger route with $.get() function
%= input_tag 'play', type=>'button', value=>'Play', onclick=>"$.get('/play')"
or
<input type="button" value="Play" onclick="$.get('/play');">
To use jQuery you have to add the following line into your template
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

Related

Additional Form submit button with redirection

I have added additional submit button on node create page via form_alter:
$form['check_data'] = array(
'#type' => 'submit',
'#access' => TRUE,
'#value' => 'Check',
'#validate' => array('node_form_validate'),
'#submit' => array('node_form_submit', 'custom_redirect'),
);
So the button now validates the form and saves the node, but it doesn't redirect it afterwards:
function custom_redirect($form, &$form_state) {
$form_state['redirect'] = 'node/%nid/check';
}
Any ideas how to redirect it after submission?
in _form_alter add the following:
$form['actions']['check_data']['#submit'][] = 'custom_redirect';
and/or check does form_state have nid and use it from there, like
$form_state['nid'], $form_state['redirect'] = 'node/' . $form_state['nid'] . '/check';

How to set up query arguments to `link_to` mojolicious helper?

I want to pass arguments for link generated by link_to helper:
%= link_to 'Change password' => 'auth_remind', { login => $login }
but this does not work.
How to set up query arguments?
It is not documented yet, but in default welcome.html.ep we can found:
%= link_to 'click here' => url_for
So we can do next:
%= link_to 'Change password' => url_for( 'auth_remind' )->query( login => $login )
If we need to set up some arguments for route we may:
%= link_to 'Change password' => url_for( 'auth_remind', { format => 'txt' } )->query( login => $login )

Drupal 7: Show fields on a form based on another field and a database lookup

I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.
Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.
EDIT:
I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.
Here is what I tried and it didn't work.
/**
* Implements hook_form().
*/
function _vista_form($form, &$form_state) {
$form = array();
$form_state['rebuild'] = TRUE;
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
);
$form['render_area'] = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div id="job-agent-form">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('class' => array('submit')),
'#ajax' => array(
'callback' => '_display_form',
'wrapper' => 'job-agent-form',
'method' => 'replace',
'effect' => 'fade',
),
return $form;
}
function _display_form($form, &$form_state) {
// there are other form elements that would go here also, I just added two for example
$type_options = array(
'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
'International' => 'International Locum Tenens',
'Permanent' => 'Permanent Physician',
'US-Locum-Tenens' => 'US Locum Tenens',
);
$form['job_type'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => 'Type of Job',
'#options' => $type_options,
'#empty_option' => 'Choose a placement type',
'#empty_value' => 'all',
//'#default_value' => $type_selected,
);
$form['active'] = array(
'#type' => 'checkbox',
'#title' => 'Subscribe/Unsubscribe',
'#default_value' => 1,
);
return $form;
}
I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.

How to apply click event for option button using Zend form

I am new to zend, Below is my piece of code,
$TEST = new Zend_Form_Element_Radio('test', array(
'label' => 'Send:*',
'value' => $data,
'multiOptions' => array(0 => 'TEST1', 1 => 'TEST2',2 => 'TEST3 '),
));
$this->addElement($TEST);
Here i Want to apply onclick event for above 3 option button respectively.Is it possible ?.Kindly help me on this
You would probably be best off using jQuery for this? E.g.:
$(function() {
$("#test").click(function(){
// whatever you need to do
});
});

drupal questions - how to print a form from another module?

I'm trying to include a module form in a panel and I've tried using drupal_get_form(), but not sure I'm using it correctly.
In the organic groups module, there's a function to render an og_broadcast_form. It's called within a page_callback in og.module:
// Broadcast tab on group node.
$items['node/%node/broadcast'] = array(
'title' => 'Broadcast',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_broadcast_form', 1),
'access callback' => 'og_broadcast_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'file' => 'og.pages.inc',
'weight' => 7
);
And in og.pages.inc, the function is:
function og_broadcast_form($form_state, $node) {
drupal_set_title(t('Send message to %group', array('%group' => $node->title)));
if (!empty($form_state['post'])) {
drupal_set_message(t('Your message will be sent to all members of this group.'));
}
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 70,
'#maxlength' => 250,
'#description' => t('Enter a subject for your message.'),
'#required' => TRUE,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#rows' => 5,
'#cols' => 90,
'#description' => t('Enter a body for your message.'),
'#required' => TRUE
);
$form['send'] = array('#type' => 'submit', '#value' => t('Send message'));
$form['gid'] = array('#type' => 'value', '#value' => $node->nid);
return $form;
}
I've tried a number of variations of drupal_get_form:
print drupal_get_form('og_broadcast_form', NULL, arg(1)); //where arg 1 is the node id from the url
print drupal_get_form('og_broadcast_form');
print drupal_get_form('og_broadcast_form', &$form_state, arg(1));
print drupal_get_form('og_broadcast_form', $n); //where $n is node_load(arg(1));
print drupal_get_form('og_broadcast_form', &$form_state, $n);
etc., etc... Is there a way to accomplish what I'm trying to do here?
FYI.. your problem is that you're attempting to load a form located in another modules include file.
The function is located in og.pages.inc and you'll need to make a call to:
module_load_include('inc', 'og', 'og.pages');
This must be done before you can grab the form.
If drupal_get_form is given the name of a function as it's first argument, that will be both the form_id and a function to be called to generate the $form array.
On line 3 of the function code, we have $args = func_get_args();, this is used by drupal_get_form to collect any or all additional arguments you may want to pass to your form-building function.
You should be using drupal_get_form('og_broadcast_form', node_load(arg(1)));.
Are you sure you should be using print and not return? I have recently learned they do very different things in the theming system. I have used drupal_get_form in this way to populate the contents of a block successfully, but at no point did I print to the screen myself.
EDIT: The full node object and not the nid because %node in a menu path uses a wildcard loader to pass the node_load(arg(1)) on to whatever function is being called.
drupal_get_form gets the form for you. have you tried print drupal_render(drupal_get_form('whatever'))) ?
drupal_get_form() only accepts one argument, the $form_id.
http://api.drupal.org/api/function/drupal_get_form/6
Run a hook_form_alter() and var_dump($form_id). That will give you the $form_id, and when you pass that to drupal_get_form(), it should return the rendered form.