A
A
Alex1352017-04-07 09:14:46
JavaScript
Alex135, 2017-04-07 09:14:46

How to pass a variable from JavaScript to ActiveForm in YII2?

The code ends with a function like this and code

function savecoordinats (){	
    var new_coords = [coords[0].toFixed(4), coords[1].toFixed(4)];	
    myPlacemark.getOverlaySync().getData().geometry.setCoordinates(new_coords);
    document.getElementById("latlongmet").value = new_coords;
    document.getElementById("mapzoom").value = myMap.getZoom();
    var center = myMap.getCenter();
    var new_center = [center[0].toFixed(4), center[1].toFixed(4)];	
    document.getElementById("latlongcenter").value = new_center;	
  }

<div id="map"></div>  
<div id="coord_form">
<p><label>Координаты метки: </label><input id="latlongmet" class="form-control" name="icon_text" /><br/>
<label>Масштаб: </label><input id="mapzoom" class="form-control" name="icon_text" /></p>
<p><label>Центр карты: </label><input id="latlongcenter" class="form-control" name="icon_text" /></p>
</div>

id="latlongmet" looks like 56.3224,44.0061
How to pass this variable to ActiveForm
$form->field($model, 'map')->textInput(['maxlength' => true])

?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Fedorov, 2017-04-07
@qonand

What do you think it means to pass a variable from JavaScript to ActiveForm? JavaScript works on the client side, php (which forms the ActiveForm) on the server side. Do you want to write the result of your js script execution in the form input field? then implement it in js, for example:
$("#latlongmet").val(value);

M
Maxim Timofeev, 2017-04-07
@webinar

How to pass this variable to ActiveForm

You don’t seem to understand the life cycle of the web, but it’s like this: there
is a request from the browser to the server , the
server launches yii, including processing the ActiveForm widget, generating some html,
this html is given back to the browser
, the browser based on it renders, what you see on screen.
So your question is "how to return to the past?" - no way
But you can solve your question, you can use js to manipulate the html that resulted from the work of ActiveForm. That is, it is necessary to influence not ActiveForm, but the result of its (ActiveForm widget) work. And the result of his work is some html and some js that does validation. And you can change them. And how Maxim Fedorov
correctly wrote to youthis is done with 1 line of jquery. You should spend 2-3 hours and start reading the jquery tutorial. I assure you that all questions will disappear at 3 hours of reading.

A
Alex135, 2017-04-07
@Alex135

Thank you, I read the tutorial and figured it out.
In line
replaced "latlongmet" with the desired value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question