Pass Element value to $ajax->link in cakephp
I need to pass the value of an element to an $ajax->link without using a form/submit structure. (because I have a dynamically set number of clickable links through which I am triggering the action)
I used to do this in Ruby using the Prototype javascript function $F like this:
<%= link_to_remote "#{item.to_s}",
:url => { :action => :add_mpc },
:with => "'category=' + $F('mpc_category')" -%>
But this does not seem to work in Cakephp:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => '$F("mpc_category")'),
array('update' => 'results', 'position' => 'top')); ?>
PHP sees $F as a variable instead of a call to javascript. I'm not too familiar with Javascript, but is there another way to pass the开发者_运维技巧 value of the 'mpc_category' input element to the controller through this link? I have been looking for a couple days and can't find anyone dealing with this specific issue. Thanks for any assistance.
Edit: fixed syntax in php statement.
haven't really used cake, but I have used rails. The js part should be entirely a string. Probably something like this:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => "$F('mpc_category')"),
array('update' => 'results', 'position' => 'top')); ?>
I'm assuming that it tacks "$key=$value" onto the params in the ajax link. Also note you were missing a comma at the end of that second line.
After working on this for a couple days now, the best I have come up with is this:
<?php echo $ajax->link($year,
array( 'action' => 'add_row',
'category' => $category,
'product' => $product.$newpart,
array( 'update' => $summary['summary']['id']." ".$vehicle['vehicles'],
'with' => "$('app_select').serialize()")); ?>
the 'with' => "$('app_select').serialize()"
being the part that grabs the values out of the form without having to submit the form.
I only needed one of the form elements, however, so this is not ideal as it passes the entire serialized form to the controller.
I would still like to be able to do this with any element, regardless of it is in a form or not, but this method doesn't seem to do that. Perhaps someone more familiar with prototype could shed some light on that.
精彩评论