Welcome, Guest
Username: Password: Remember me

TOPIC: Form custom code

Form custom code 14 Feb 2013 10:15 #6745

  • organicwebs
  • organicwebs's Avatar
  • Offline
  • Premium Member
  • Chris
  • Posts: 133
  • Thank you received: 21
  • Karma: 4
Hi Guys,

I have read through about 20+ forum posts - plus the tutorials - but something is just not clicking for me. So I thought I'd ask directly for help...

I'm using
  • Jcook V2.0
  • JS Framework:Jquery
  • Embed Framework:No
  • Form Style: Condensed
  • Features: JDom Complete (so I don't get those todo graphics)
  • Todo:No
  • DB Auto: Model
I have a membership form. Its has these fields (amongst others)...
  • State (ie: country state) (3 characters)
  • Company Code (4 characters)
  • Membership Type (2 characters)
  • Membership Number
The Membership number is a string containing the State, Company Code, Membership Type and a sequential number. ie: NSW-JCOOK-ST-001

How do I auto-populate the Membership number in a Front end form? I'd like the form field to auto populate when the State, Company Code and Membership Type has been entered - but also allow the user to be able to override it with their own text.

Javascript would be the way - but I don't see where to add it in components/com_mycomponent/views/member/tmpl/member_form.php ?

Instead of seeing JDom calls, I see this...
// Iterate through the fields and display them.
	foreach($fieldSet as $field):
		//Check ACL
	    if ((method_exists($field, 'canView')) && !$field->canView())
	    	continue;
	    // If the field is hidden, only use the input.
	    if ($field->hidden):
	        echo $field->input;
	    else:
	    ?>
	    <dt>
	        <?php echo $field->label; ?>
	    </dt>
	    <dd<?php echo ($field->type == 'Editor' || $field->type == 'Textarea') ? ' style="clear: both; margin: 0;"' : ''?>>
	        <?php echo $field->input ?>
	    </dd>
	    <?php
	    endif;
	endforeach;
I've given each field a class.

Also - can somebody send me a link to the available parameters with the form xml's (components/com_mycomponent/models/forms/member.xml )

Thanks in advance :)
Just call me Chris :)
Last Edit: 14 Feb 2013 10:16 by organicwebs.
The administrator has disabled public write access.

Re: Form custom code 14 Feb 2013 10:46 #6747

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi,

This post here covers using jQuery to interact with your form: www.j-cook.pro/forum/35-jquery/4481-show...ds-in-front-end-form

This isn't exactly what you want but demonstrates how to add the jQuery and select your form elements and check their values etc.

You will need something like:
// set some vars for the vale you need to concatenation into your membership no
var stateVal = jQuery("jform_{id-of-your-state-field}").val();
var companyCodeVal = jQuery("jform_{id-of-your-company-code-field}").val();
var membershipTypeVal = jQuery("jform_{id-of-your-membership-type-field}").val();

// concatenate them - change the "-" delimiter to whatever you want
var membershipNoVal = stateVal + "-" + companyCodeVal + "-" + membershipTypeVal;

//set the membership number fld to your calculated value
 jQuery("jform_{id-of-your-membership-number-field}").val(membershipNoVal);
I'd create a function for this to run any time any of the 3 fields your basing your membership number on change using jQuery's .change() method.

Hope it helps!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
The following user(s) said Thank You: organicwebs

Re: Form custom code 14 Feb 2013 12:50 #6750

  • organicwebs
  • organicwebs's Avatar
  • Offline
  • Premium Member
  • Chris
  • Posts: 133
  • Thank you received: 21
  • Karma: 4
Thanks Gez, appreciate that.

I started to add Mootools javascript at the top of components/com_mycomponent/views/member/tmpl/member_form.php . I've just check back at the forum - didn't expect you would answer so fast :lol: Glad to know I'm on the right track.

You think JQuery is the way to go? Maybe I'll switch...

My "State" and "Membership Type" fields are radio boxes. Off the top of your head, do you know how return the labeled value of a radio box in JQuery and JCook? That's not their id value (1,2,3,4) - but the label value (QLD, NSW, VIC)?

Cheers
Just call me Chris :)
The administrator has disabled public write access.

Re: Form custom code 14 Feb 2013 13:00 #6751

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Well, jQuery in my opinion would definitely be the way to go!

I think you can access the label with .text() instead of .val() but do double check that on the jQuery website to be sure!

Glad it helped!

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
The following user(s) said Thank You: organicwebs

Re: Form custom code 15 Feb 2013 04:54 #6759

  • organicwebs
  • organicwebs's Avatar
  • Offline
  • Premium Member
  • Chris
  • Posts: 133
  • Thank you received: 21
  • Karma: 4
Getting the labels for the Radios wasn't that easy unfortunately.

The finished code for those in the same shoes...

(jQuery JavaScript placed at the top of components/com_mycomponent/views/member/tmpl/member_form.php)
<script type="text/javascript">
  // Script to Auto Generate the Membership Number
  jQuery(document).ready(function($) { 
	// Set input field change events to trigger the update membership number
	$("#jform_company_code").on("change",updateMemNum);
	$("input[name='jform[membership_type_id]']").on("change",updateMemNum);
	$("input[name='jform[state_id]']").on("change",updateMemNum);
	
	function updateMemNum() {	
	  // Get the State Radio Value
	  var stateId = $('input:radio[name="jform[state_id]"]:checked').prop("id");
	  var stateLabel = $('label[for='+stateId+']').text();
				
	  //Get the Company Code
	  var companyCodeVal = $("#jform_company_code").val();
				
	  // Get the Membership Type Radio Value
	  var membershipTypeId = $('input:radio[name="jform[membership_type_id]"]:checked').prop("id");
	  var membershipTypeLabel = $('label[for='+membershipTypeId+']').text();
				
	  // concatenate to make the membership No
	  var membershipNoVal = stateLabel + "-" + companyCodeVal + "-" + membershipTypeLabel;
				
	 //set the membership No
	 $("#jform_membership_number").val(membershipNoVal);	   		   
	} //end function
});//end document.ready
</script>
Just call me Chris :)
Last Edit: 15 Feb 2013 04:57 by organicwebs.
The administrator has disabled public write access.
The following user(s) said Thank You: robertocook, BTB300, JoomGuy

Re: Form custom code 15 Feb 2013 15:24 #6768

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Thanks for sharing!!! K+1

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: Form custom code 18 Feb 2013 09:58 #6804

Hi,

How you did for the sequential number??
The administrator has disabled public write access.

Re: Form custom code 18 Feb 2013 10:35 #6805

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
@robertocook,

Do you mean you want to create a sequential number?

If so, you need to create a var (int), initially set it to whatever value you want to start from then increment it....

Something like this would do the trick:
// JS
var seqNo = 0; // or whatever you want to start from

//Then, increment it
seqNo ++;
Naturally, if you want to do this for dynamically added inputs you'll need to do this in a loop (jQuery.each() for example) and target the class of the dynamically added field. Therefore, when you create these dynamic inputs, you will need to ensure they are assigned the same class that will allow you to target them in this way.

Hope it helps,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: Form custom code 19 Feb 2013 11:19 #6815

  • organicwebs
  • organicwebs's Avatar
  • Offline
  • Premium Member
  • Chris
  • Posts: 133
  • Thank you received: 21
  • Karma: 4
Hi Robertocook,

Sorry - my original scope changed and I didn't need that sequential number in my membership number anymore! :whistle:

Off the top of my head, I'd do a database call during the prepareQuery first to get all membership numbers and pass these as an array into the JavaScript. Then within the JavaScript, use a loop to increment the membership number by 1 until you find a unique value.

But after all this, I've ended up doing this in PHP on form saving now.

By the way, you can add php script prior to the form loading and after submitting the form in the components/com_mycomponent/models/member.php file

For running php before form loading, look for
protected function prepareQuery(&$query, $pk)

For running php after form submit, you need to create a save function in the same file...
public function save($data){	
	//Do what you want before. Work with $data
	
	// The original call to the Parent Save Script
	$result = parent::save($data);
	
	//Do what you want after. Read and work on $result
	
	return $result;
}

See
www.j-cook.pro/forum/9-coding-inside-you...-data-upon-save#5083

I'm just learning all this - so take anything I say with a pinch of salt. ;)
Just call me Chris :)
The administrator has disabled public write access.

Re: Form custom code 21 Feb 2013 14:50 #6827

Hi, i used it on the frontend controller,
public function save($data){	
	//Do what you want before. Work with $data
	
	// The original call to the Parent Save Script
	$result = parent::save($data);
	
	//Do what you want after. Read and work on $result
	
	return $result;
}

but if i do a print_r($data), i obtain nothing
if i do a print_r or echo $result after the store i receive just a 1

it is true that i can obtain the data via $_POST
The administrator has disabled public write access.

Re: Form custom code 22 Feb 2013 05:44 #6828

  • organicwebs
  • organicwebs's Avatar
  • Offline
  • Premium Member
  • Chris
  • Posts: 133
  • Thank you received: 21
  • Karma: 4
Hi Robertocook

I've used it in the model (not controller) - and print_r($data) works.

components/com_mycomponent/models/mypage.php
echo "<pre>data:". print_r($data,true)."</pre>";		//debug
Just call me Chris :)
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: Form custom code 22 Feb 2013 15:03 #6830

Hi !!

My mistake !!
I placed the code on the controller -> ERROR !!

It should be placed in the MODEL !
On the model it works fantastic !!!!
The administrator has disabled public write access.
Time to create page: 0.078 seconds

Get Started