Can't load current field value into PHP


#1

Hi, is there no longer a way to get the current value of a field in PHP? I have tried both via javascript callback:

$form->on('focus', 'input', function($js, $data) {
    $c=$data['cnt']+1;
    $js->val($c)->data('cnt', $c);
});

and field reference:

$form['field'];

Just like some of the examples say should work, with no success. How is this supposed to work? I want to implement some PHP logic based on the value of a field after it is changed (javascript blur event). Thank you,


#2

Ok, few things here:

  1. $form['field'] used to work in ATK4.3, which is a legacy version (https://github.com/atk4/atk4). It was a short syntax for $form->model['field'] which you can use inside submission handler. (Form::onSubmit)

  2. You can reference field values inside JavaScript, for example take value from one field and do something with it. Example is here: https://github.com/atk4/ui/blob/develop/demos/reloading.php#L64

  3. If you want to bring value of arbitrary JS expression into your PHP callback, it’s documented here: http://agile-ui.readthedocs.io/en/latest/callbacks.html#javascript-arguments.

In it’s simple form it looks like this:

$label->on('click', function($j, $arg1) {
    return 'width is '.$arg1;
}, [new \atk4\ui\jsExpression( '$(window).width()' )]);

From your code it looks like you want to increment values in your fields when you click inside them, so here are some ideas:

Use argument with expression $(this).val(), this way when PHP code is called you pass the current value into the callback.


#3

Okay I got a variety of this working, thank you. But I can’t get the ‘blur’ event to work, it only seems to work with ‘click’. And what is $j ??


#4

$j same as $js, whatever you want to call it. First argument passed into your callback is js chain.

I am not sure about the JS events, try debugging them in the browser. I’m pretty bad at JavaScript :smiley:


#5

I later got it to work by making (in this case) ‘input’ be the second argument, and a callback function the third, e.g.: $someVar->on(‘blur’, ‘input’, function() use ($anotherVar) { … code … }. However neither method would work with the ‘change’ event though ‘blur’ is better than nothing :slight_smile:


#6

And function requires an event to occur. What I’m also looking for is something that can just return a value to PHP, for example a string. It should look something like this:

echo 'the value of my obj is: ’ . (new \atk4\ui\jQuery($obj)->val()) /* <-- But I want this to display the value/result of the jQuery, NOT the jQuery function!! */

Maybe there some simple obvious way to do it but so far no success.


#7

PHP can’t display value of JS property, they live on different side of the internet. I might be miss-understanding what you try to achieve here, but try to change your thinking and solve your problem differently.


#8

Being new to agiletoolkit and Jquery, the way they seem to operate through functions made me think it was possible. I will look into another way, thank you,