Yii Framework Forum: Passing JS created values to hidden form elements - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Passing JS created values to hidden form elements Rate Topic: -----

#1 User is offline   diggity 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 05-March 10

Posted 05 March 2010 - 09:02 PM

greetings.

i searched but was unable to find a direct solution.

i have a need to embed values into hidden form elements, after they are generated within a javascript call on the form's page.
the javascript is google maps returning lat/long and i need to pass that to hidden form elements on the same page.

right now, the javascript is in the form partial. is it necessary to wrap that within a yii convention in order to access the generated hidden elements?

i've tried directly accessing the hidden form elements via the DOM/js and setting them that way, but it doesn't seem to be working.

any pointers to the function or examples are greatly appreciated.


thanks.
0

#2 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 05 March 2010 - 11:14 PM

View Postdiggity, on 05 March 2010 - 09:02 PM, said:

greetings.

i searched but was unable to find a direct solution.

i have a need to embed values into hidden form elements, after they are generated within a javascript call on the form's page.
the javascript is google maps returning lat/long and i need to pass that to hidden form elements on the same page.

right now, the javascript is in the form partial. is it necessary to wrap that within a yii convention in order to access the generated hidden elements?

i've tried directly accessing the hidden form elements via the DOM/js and setting them that way, but it doesn't seem to be working.

any pointers to the function or examples are greatly appreciated.


thanks.


If the hidden form fields are generated from a model with CHtml::activeHiddenField, make sure you use the correct ID of the element, which is going to be the Model name + underscore + attribute name. You can see the exact ID of the element by viewing source on your page. Here's an example using jQuery:
<!-- ....active form based on model named Model.... -->
<?php echo CHtml::activeHiddenField($model,'attribute_name'); ?>
<!-- ....end form.... -->

<script type="text/javascript">
$(document).ready(function() {
    var myVar = 'something';
    $("#Model_attribute_name").val(myVar);
});
</script>

0

#3 User is offline   diggity 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 05-March 10

Posted 06 March 2010 - 02:41 AM

perfect, that did the trick. (it also helps to view generated source)

thanks for your quick reply.


View Postluoshiben, on 05 March 2010 - 11:14 PM, said:

If the hidden form fields are generated from a model with CHtml::activeHiddenField, make sure you use the correct ID of the element, which is going to be the Model name + underscore + attribute name. You can see the exact ID of the element by viewing source on your page. Here's an example using jQuery:
<!-- ....active form based on model named Model.... -->
<?php echo CHtml::activeHiddenField($model,'attribute_name'); ?>
<!-- ....end form.... -->

<script type="text/javascript">
$(document).ready(function() {
    var myVar = 'something';
    $("#Model_attribute_name").val(myVar);
});
</script>


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users