views:

281

answers:

2

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.

A: 

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.

Tesserex
Yes, looks like I typed that too hastily. I lost the comma from erasing additional parameters to make this more readable. I have tried it with quotes, when I put quotes around the "$F('mpc_category')" it shows up in the params as: [category] => $F()and in the url as: /category:$F(/ [quot;mpc_category/] => [quot;)]So it is still not returning the value of the element, but just passing the text as a string to the controller.
TwoThumbs
Similarly, if i replace $F('category') with document.getElementByID('category').value; it still is not evaluated correctly, although it is not exactly just passed as a string.I have a result of [category] => document.getElementByID() in the params and category:document.getElementByID() in the url. So, it seems that something is happening other than just passing the parameter as a string, but I don't know what that is.
TwoThumbs
A: 

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.

TwoThumbs
Just to clarify the above, I found that I could pass any element id this way, but if you want to pass more than one element, they both/all need to be wrapped in another element or form. I can't find any way to pass 2 separate elements.
TwoThumbs