Pass Element value to $ajax->link in cakephp - php

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.

Related

PHP SOAP - Passing parameters with same name

I'm calling an API using php and SOAP.
some parts of the request are like:
<fin:Customer>
<fin1:Address>
<fin1:City>City</fin1:City>
<fin1:Country>CA</fin1:Country>
</fin1:Address>
<fin:Customer>
Which is easy to do with arrays:
'Customer' => array(
'Address' => array (
'City' => $City,
'Country' => $Country,
),
But my challenge is for this part:
<fin:Criterions>
<fin:Criterion name="VALX">17</fin:Criterion>
<fin:Criterion name="VALOP">1</fin:Criterion>
<fin:Criterion name="VALLP">10</fin:Criterion>
<fin:Criterion name="TMS">3</fin:Criterion>
Because all tags have the same name, and they also have a name parameter and a value.
How can I pass that to SOAP API?
Is it possible to use arrays?
Thanks in advance!
The easiest way to construct a request, send it and handle the response is to use a WSDL to PHP generator as it'll normally generate each class for every parameter you need to send (in addition to the response objects and the operations classes)
Knowing this, you'll be able to easily see how to pass these "duplicated" tags as it is certainly an array of object that you must pass,
Try the PackageGenerator project which should work fine,
Solution:
'Criterions' => array(
'Criterion' => array(
array('name'=> 'VALX' , '_'=> '17' ),
array('name'=> 'VALOP' , '_'=> '1' ),
array('name'=> 'VALLP' , '_'=> '10' ),
array('name'=> 'TMS' , '_'=> '3' )
));
Be sure to use underscore ('_'), if other values (e.g. val, value, etc) didn't work.
Might save someone's time and effort.

How to attach a file using SubmitForm() in Codeception

I'm testing a form through the SubmitForm() function because the form uses javascript to cycle through each individual item.
example:
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');
This works fine but I'm having trouble with a file upload input.
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
'file' => ???
), 'submit');
I tried sending an array to mimic the $_FILES array but that obviously isn't the right way to do it. Is this possible?
I've encountered this issue as well and the only way I can see around it is to manually fill the fields and the click the submit button.
For example
$I->fillField(['name' => 'name'], 'Test');
$I->attachFile('input[name=photo]', 'test.jpg');
$I->click('#formId button[type=submit]');
$I->seeCurrentRouteIs('route.index');
$I->see('Model has been updated.');
You can store any test files in the Codeception tests/_data folder.
This does work, but sadly doesn't help me in my current situation as I have a form which dynamically populates various select elements so I need to submitForm as I can't manually selectOption as the options are populated depending on other form completions.
I realise this is old and already marked as answer but it doesn't answer the specific question, which is still a problem for #alexleonard, who posted the accepted answer.
You can user attachFile in conjunction with submitForm. You just have to call is first. For example:
$I->attachFile('#form input[type=file]', <pathtofile> );
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');

Pass line id as argument in x-editable or TbEditableColumn to update a particular record

I am trying to use TbEditableColumn / X-Editable to make inline edits in the CGridView. I'm trying to use the usual $data variable to get row id by the intuitive "$data->id" to pass the line id in the dynamically generated url so that in the update action of the controller, I'd know which record to update.
However $data->id is just not working while creating URL. Interestingly the same $data variable is accessible while applying "Editable" property on the item. See below.
I don't want to go for JS:function way to fetch id of an element and pass it as argument in ajax call. This just doesn't seem elegant.
array(
'class' => 'editable.EditableColumn',
'name' => 'mrp',
'headerHtmlOptions' => array('style' => 'width:200px'),
'editable' => array(
'type' => 'text',
'apply' => '$data->id>10605',
'url' => '/product/changeMRP/id/$data->id',
),
),
In above code, this line works
'apply' => '$data->id>10605'
But this line doesn't
'url' => '/product/changeMRP/id/$data->id',
Why is it that? And how can i solve my problem?
I followed this question :
How can update db values using x-editable EditableColumn? but it didnt help
You can use createUrl() for creating URL and pass the id in array. Like this
'url' => "Yii::app()->createUrl('product/changeMRP', array('id'=>'$data->id'))",
Hope it will solve your problem.

CodeIgniter, form helper - Adding classes to the elements

So i created a form with the CodeIgniter Form helper. I'm trying to give some form elements a class, but something strange happens. I do it like this:
echo form_input('title', $this->input->post('title'), 'class="titleInput"');
When i inspect the element it actually shows the class is there:
but it doesn't take any of the property's given in the CSS file. Also when i do something ugly like:
echo form_input('title', $this->input->post('title'), style="height=30px");
When i inspect the element i get this:
But it doesn't actually do anything with the styling given.
Hopefully someone can give me a hand here!
$data = array(
'name' => 'title',
'value' => $this->input->post('title'),
'class' => 'titleInput',
'style' => 'height:30px'
);
echo form_input($data);
reference

Using multiple PregReplace filters on a Zend Form element

I want to be able to add multiple PregReplace filters on a single Zend Form element.
I can add one PregReplace filter using the code below:
$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
'match' => '/bob/',
'replace' => 'john'
));
$this->addElement($word);
I've tried
$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
'match' => '/bob/',
'replace' => 'john'
));
$word->addFilter('PregReplace', array(
'match' => '/sam/',
'replace' => 'dave'
));
$this->addElement($word);
but this just meant only the second filter worked.
How do I add multiple PregReplace filters?
The problem you're facing is that the second filter will override the first one in the filters stack ($this->_filters) defined in Zend_Form_Element.
As David mentioned in the question comments, the filters stack use filter names as index ($this->_filters[$name] = $filter;) this is the reason why the second filter override the first one.
In order to resolve this problem, you can use a custom filter as follows:
$element->addFilter('callback', function($v) { return preg_replace(array('/bob/', '/sam/'),array('john', 'dave'), $v); });
This is done using an inline function(), in case you're not using PHP version 5.3 or higher, you can set your callback as follows to make it work:
$element->addFilter('callback', array('callback' => array($this, 'funcName')));
And add under your init() method in your form:
function funcName($v) {
return preg_replace(array('/bob/', '/sam/'), array('john', 'dave'), $v);
}
At last, if you want to use only the PregReplace filter, unlike Marcin's answer (the syntax is incorrect), you can still do it this way:
$element->addFilter('pregReplace', array(
array('match' => array('/bob/', '/sam/'),
'replace' => array('john', 'dave')
)));
That should do the trick ;)
Since PregReplace uses php's preg_replace function, I guess something like this would be possible (preg_replace can accepts arrays of patterns and array of corresponding replacement strings):
$word = new Zend_Form_Element_Text('word');
$word->addFilter('PregReplace', array(
'match' => array('/bob/', '/sam/'),
'replace' => array('john' , dave)
));
$this->addElement($word);
I haven't tested it though. Hope it will work.
I was unable to get the previous example to work with 'PregReplace'. I switched instead to calling it with new Zend_Filter_PregReplace(). It now works for me.
$word->addFilter(new Zend_Filter_PregReplace(array(
'match' => array('/bob/', '/sam/'),
'replace'=> array('john', 'dave'))
));
I was looking for same-response does not have a usable version
$word->addFilter(new Zend_Filter_PregReplace(new Zend_Config(array(
'match'=>array('/bob/', '/sam/'),
'replace'=>array('john', 'dave')
))));

Categories