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);
})
})
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.
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?
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.
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.
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?
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);
})
})