Page 1 of 1

Set value based upon data in other field

Posted: 2015-05-30 22:24
by shasta59
Using 5.31 (have not upgraded yet)

If you wish to set the value of a form field based upon the value put in another field here is one way to do it.

I recommend you use a field with set values to pick from to make it easier. My example uses a field where the values are Yes and No. The file you modify is the tablename_templateDV.html. I have done this using jquery.

At the very bottom you would add text similar to the following using your values.

Code: Select all

<script>
	
	$j(document).ready(function(){
	$j('#position').change(function(){
	
	var positionvalue = ($j('#position').val());
	if(!positionvalue || positionvalue == 'Yes'){
	$j('#points').val("2");
	}else{
	$j('#points').val("1");
	}
	});
	});
	</script>
Explanation:

<script> this line announces what follows is a script
$j(document).ready(function(){ - this is the jquery document ready function
$j('#position').change(function(){ - this line watches for the field to change
var positionvalue = ($j('#position').val()); - this line put the value of the field called postion into the variable positionvalue - gets content of field
if(!positionvalue || positionvalue == 'Yes'){ - this line is the start of the if function. Tests to see if the variable postionvalue equals Yes
$j('#points').val("2"); - if yes it puts in a 2 into the field called points.

Then next line then puts in a 1 if it is not equal to yes using the if then else construct.

</script> - closes the script section.

You may wish to turn off the ability to edit the field so the user cannot change the input but you can put a value in. I have another topic which covers how to do that. (Not sure which one right now).

There is a tighter way to do it but this method is fairly easy to follow and allow everyone to make easy changes to suit their needs.

Enjoy

Alan

Re: Set value based upon data in other field

Posted: 2016-05-12 22:10
by jstrick
ok Alan

question in the above statement it says:

At the very bottom you would add text similar to the following using your values.

the question is at the bottom of what?
I have tried building a magic file and no luck.
Tried to add it to the bottom of a hook file no luck.

So were the heck do i add this or something like it.

Thanks in advance
using version 5.5

Re: Set value based upon data in other field

Posted: 2016-05-12 22:42
by AhmedBR
jstrick,

The file to be modified is not the hook file, it is tablename_templateDV.htm

Re: Set value based upon data in other field

Posted: 2016-07-08 01:16
by jstrick
Well maybe so but this worked in side the hooks folder

function Your table name_before_update(&$data, $memberInfo, &$args){
if($data['field with data name you want looked at'] == 'Complete'){ $data['field to change staus of'] = "null" and $data['another field you want changed to a specific date'] = date('Y-m-d'); }
return TRUE;
}


Think I explained this correctly seems easier in a hook took a bit to figure out found this on the forum some place.

Seems like you start making changes outside of the hook folder things get complicated.