Skip Navigation

[Resolved] Retrive data from custom field to jquery variable

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 13 replies, has 2 voices.

Last updated by salimA 2 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2249489

Dears,

I have a custom field (renew-date) that stores number of days before expiration date. Ex, 3
I would like to add the number of days to the expiration date.

How to retrieve the value of renew-date field to add it and recalculate the date.

var dif = jQuery("wpcf-renew-date").val(); ----> this returned NaN/NaN/NaN

I have use the below code

jQuery(function($){
var dif = jQuery("wpcf-renew-date").val(); ----> here is the issue
var day2 = new Date();
day2.setDate(day2.getDate()+dif);
day2.setFullYear(day2.getFullYear()+1);
var dd = day2.getDate();
var mm = day2.getMonth()+1;
var yyyy = day2.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
day2 = mm+'/'+dd+'/'+yyyy;

// 2nd part: updating all the date fields inside all the user forms of the page
$('form.cred-user-form').each(function(){
var form = jQuery(this)
form.find('input[name="wpcf-expiration-date"]').val(day2);
})
})

#2249733

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that you created a post form using Toolset form and when you submit the form you want to add the number of days you added to the field renew-date should be added to the post expiration date?

Can you please share all required details with explanation what you want to achieve and problem URL where you added the form and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2250611

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

But why you want to use jQuery here as we can save the expiration date when you submit the form and we can use the cred_save_data hook to make amendments to the expiration date. Will that help?
- If yes:

Can you please share where on what page you added your form?

#2250627

Because i already wrote the code in JS editor and it is working fine, it saves the expiration date. only i want to add the number of days from the renew-date field to that expiration date.

the form is on hidden link in renewed users

#2250641

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm on this URL which you shared: hidden link

But I do not see any form. Can you please share where exactly you want to make changes and what steps I should follow to see the form?

#2251257

Sorry, I forgot to inform you, it is inside the view in renewed users tab.
view name:accounts-renewal
form name: accounts-renewal

I have added a user with name raiya for test.

#2251315

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm still not clear about the flow you are following.

I see you are using Edit user form and user form does not offer the post expiration.

I see you have created a view that list subscriber, do you mean that when I click on activate, you want to add the number of days (3 in this case currently) to the subscriber's post expiration date? If yes:
I'm not sure why you are using jQuery as the best solution could be to use the "cred_save_data" hook in this case where we can grab the post ID of the subscriber post and also we can get the number of days (3 in this case) and update the post expiration date for that specific subscriber.

Please correct me if I'm wrong and share detailed workflow of your requirement.

#2251325

No please, it is a user form, the expiration date is there it was hidden, i just show it.

yes , when we click activate

the form should add number of days ex,3 to the current expiration date and update it to store the new one in the user profile.

#2251419

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see the form now with the field "expiration date".

The "expiration date" field displays the current date (today's's date) i.e. 12/23/2022 , what you want is you want to update the "expiration date" with the value of "wpcf-renew-date" field that holds the value 3.

So, your goal is to add value of "wpcf-renew-date" to the current that that is 12/23/2022 + 3 i.e. 12/26/2022 - correct?

#2252847

yes, exactly.. I have used the below code , dif variable is not retrieving the value of days.

jQuery(function($){
var dif = jQuery("wpcf-renew-date").val(); ----> here is the issue
var day2 = new Date();
day2.setDate(day2.getDate()+dif);
day2.setFullYear(day2.getFullYear()+1);
var dd = day2.getDate();
var mm = day2.getMonth()+1;
var yyyy = day2.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
day2 = mm+'/'+dd+'/'+yyyy;

// 2nd part: updating all the date fields inside all the user forms of the page
$('form.cred-user-form').each(function(){
var form = jQuery(this)
form.find('input[name="wpcf-expiration-date"]').val(day2);
})
})

#2253361

any update

#2253385

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To get the number of days from the custom field "renew-date", you will have to use the following jQuery line of code:

var dif = jQuery("input[name='wpcf-renew-date']").val();

I can see it adds 3 days to the current date it and it shows the date as 30.

#2253881

there is still an issue on the date

when I add var dif = jQuery("input[name='wpcf-renew-date']").val();
the date shows 09/09/2023

if I add 3 as constant value to the variable var dif = 3;
the date shows
12/31/2022 --> this is the correct date

So, the new date should add 1 year and 3 days to the current date.

#2253899

My issue is resolved now.

I have convert the date to int using parseInt

day2.setDate(day2.getDate() + parseInt(dif));

Now the date is correct

Thank you Minesh for your support !