Skip Navigation

[Resolved] Change select value based on a field value

This support ticket is created 3 years, 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 voices.

Last updated by salimA 3 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2132345

I am trying to change the select item based on the value of another text box,

for example, if the value of the input (st-test) is true, the value of the select field (payment ) = Paid.
Else, the value of the select remains = Waiting for Payment.

what i did is,

jQuery(document).ready(function() {
jQuery('input[name="wpcf-st-test"]').keyup(function() {
if (jQuery(this).val() == 'true') {

jQuery("select[name^='wpcf-payment'] option[value='Paid']").attr("selected","selected").trigger('change');
});

} else {
jQuery("select[name^='wpcf-payment'] option[value='Waiting for Payment']").attr("selected","selected").trigger('change');
}
});
});

What is wrong?? the select remains Waiting for Payment even if the value of the input is true.

#2133715

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2021-08-04 at 13.15.53.png

I've spent quite a bit of time puzzling over this and am also having trouble getting it working.

Here's the version of the code I currently have:

document.addEventListener("DOMContentLoaded", function (event) {
    (function ($) {
        $("select[name^='wpcf-payment'] option[value='Waiting for Payment']").attr("selected", "selected");
        $("select[name^='wpcf-payment'] option:first").removeAttr("selected");
        $('input[name="wpcf-st-test"]').keyup(function (e) {
            if ( $(this).val() == 'true') {
                $("select[name^='wpcf-payment'] option[value='Waiting for Payment']").removeAttr("selected");
                $("select[name^='wpcf-payment'] option[value='Paid']").attr("selected", "selected");
            } else {
                $("select[name^='wpcf-payment'] option[value='Paid']").removeAttr("selected");
                $("select[name^='wpcf-payment'] option[value='Waiting for Payment']").attr("selected", "selected");
            }
        });

    })(jQuery);
});

If you open the browser dev tools and inspect the select input markup, when you type "true" into the text field being listened to, the select dropdown correctly changes to the Paid option.

The problem is that if you keep typing, breaking the comparison, it reverts to the --Not Set-- empty option.

But the dev tools show the code is working correctly. The selected option in the browser doesn't match the option with the selected attribute (screenshot).

That doesn't make sense and I don't know why it's happening.

I also tried modifying the code to delete the --Not Set-- option rather than simply removing the selected attribute, and that also didn't work, inasmuch as the option selected in the dropdown did not correspond to the option with the selected attribute.

As I already spent quite a bit of time on this custom code, let me pass it back to you to see if you can use that to get closer to a solution.

#2134605

We have changed the idea. We added a new text field (Pay). We want to auto-submit the form once the user fills the text field.

what we did is:
what is wrong with the code??

[creduserform]
<div class="form-group" hidden>
<label>[cred_i18n name='civil-id-label']Civil-Id[/cred_i18n]</label>
[cred_field field='civil-id' force_type='field' class='form-control' output='bootstrap' readonly='true']
</div>
<div class="form-group">
<label>[cred_i18n name='pay-label']Pay[/cred_i18n]</label>
[cred_field field='pay' force_type='field' class='form-control' output='bootstrap']
</div>
[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/creduserform]

---------------------------------------------------
jQuery(document).on('cred_form_ready', function(){
jQuery("input[name='wpcf-pay']").on('keyup',function(){

jQuery('#check-payment-form').submit();
});
});

#2134609

what we are trying to do from the previous scenario is.

we have a form with the payment. we have a field called Payment status in our form. we want the status changed to be Paid once the payment is completed.

first, Once the user completes the payment. the payment page sends through a session a value 'true'.
then, we want the status in our form changed to 'Paid' in the database if the session returned true value only.

#2134647

Minesh
Supporter

Languages: English (English )

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

From where you are generating the session value and where exactly at what point you want to save the Payment Status.

If you can share problem URL and access details first I would like to review how you setup the things and after that I'll be able to guide you in the right direction.

*** 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.

#2137287

Minesh
Supporter

Languages: English (English )

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

I do not see any login page when I try to load the login page URL you shared: hidden link

Can you please send me working login URL.

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

#2137443

Minesh
Supporter

Languages: English (English )

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

I've added the following shortcode to "Custom Code" section with code snippet: pass_value

function func_get_status( $atts ){

  if($_SESSION['status'] == 'true'){
    	return 2;
  }else if($_SESSION['status'] == 'false'){
        return 1;
  }
 
}
add_shortcode( 'get_status', 'func_get_status' );

There was a error as well with the code with the following line:

$user_id1 = get_current_user_id();
$user_info1 = get_userdata( $user_id1 );
$user_phone =  $user_info1->phone-number;

There is no "phone-number" field available with the user object $user_info1 . You should correct it that as well.

Now on profile file I can see the payment status option "Paid" is selcted:
- hidden link

#2139929

Dear Minesh,

Thank you for your support,

the status is changed in the front end only. But in the backend, the status still Waiting for payment. it should be changed from the database.

#2139931

Minesh
Supporter

Languages: English (English )

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

To change the status in database you will require to submit the form.

As I do not know what payment API you are using. Are you using Toolset Forms commerce for payment?

#2140143

I don't want the customer to change his payment status. the payment status should be changed directly after the payment is completed.

I am using Thawani payment. the payment is completed on a separate web page.

#2140145

Minesh
Supporter

Languages: English (English )

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

So, you will require to know if there is any payment callback filter/hook available with Thawani payment that you can use to update the custom field.

#2140481

Do you think this will help ??
hidden link

#2140939

Minesh
Supporter

Languages: English (English )

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

Actually it will not help much as you will have to check with "WooCommerce Thawani Checkout Payment gateway" plugin author if they offer any hook that you can use to update custom field on successful/unsuccessful payment.

Its not on us.

#2144347

My issue is resolved now. Thank you!