How to set value on...
 
Share:
Notifications
Clear all

Question How to set value on field dynamically with JS

7 Posts
2 Users
0 Likes
552 Views
Posts: 4
Topic starter
(@danihml)
Active Member
Joined: 1 year ago

i'm facing a trouble setting dynamically in name and email fields the value of my logged clients. I'm not using the wordpress login, there are some APIs on my website and i get the information from there. 

 

There are some ways to set the value on the fields with JS? I'm tryed to set on value of the input field, but it don't worked.

6 Replies
Asti
Posts: 7056
 Asti
Support
(@asti)
Illustrious Member
Joined: 6 years ago

@danihml,

You can use the js code below. Just change the red-marked part in the code: 

add_filter('wpdiscuz_js_options', function($jsOption) {
    if (true) {//condition if user logged in other API and user exist
        $jsOption['other_api_user_login'] = 'user_login';
        $jsOption['other_api_user_email'] = 'user@email.com';
    }
    return $jsOption;
});
add_action('wpdiscuz_front_scripts', function() {
    $wpdz = wpDiscuz();
    $handle = 'wpdiscuz-ajax-js';
    if ($wpdz->options->general['loadComboVersion']) {
        $handle = 'wpdiscuz-combo-js';
    }
    wp_add_inline_script($handle, 'jQuery(document).ready(function ($) {
                                     if(wpdiscuzAjaxObj.other_api_user_login){
                                        $("#wpdcom .wc_name").val(wpdiscuzAjaxObj.other_api_user_login);
                                        $("#wpdcom .wc_email").val(wpdiscuzAjaxObj.other_api_user_email);
                                        Cookies.set("comment_author_email_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.other_api_user_email);
                                        Cookies.set("comment_author_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.other_api_user_login);
                                     }
                                   });');
});

Pay attention to the commented part next to the red-marked value. 

Reply
Posts: 4
Topic starter
(@danihml)
Active Member
Joined: 1 year ago

thanks for the answer @Asti, but my console is getting an error on that part:

if ($wpdz -> options -> general['loadComboVersion'])

Uncaught SyntaxError: Unexpected token '>'. How that works exactly? 

Here is my code modified for get the value from API:

const userInfo = getLastUserInfo();

add_filter('wpdiscuz_js_options', function ($jsOption) {
	if (userInfo) {
		$jsOption['username'] = userInfo.fullName;
		$jsOption['uid'] = userInfo.uId;
		for (let i = 0; i < userInfo.contactList.length; i++) {
			const mail = userInfo.contactList[i];
			if (documentCPF.type === 'email') {
				$jsOption['usermail'] = mail;
			}
		}
	}
	return $jsOption;
});

add_action('wpdiscuz_front_scripts', function () {
	$wpdz = wpDiscuz();
	$handle = 'wpdiscuz-ajax-js';
	if ($wpdz -> options -> general['loadComboVersion']) { //error part
		$handle = 'wpdiscuz-combo-js';
	}
	wp_add_inline_script($handle,
		`jQuery(document).ready(function ($) {
			if (wpdiscuzAjaxObj.username) {
				$("#wpdcom .wc_name").val(wpdiscuzAjaxObj.username);
				$("#wpdcom .wc_email").val(wpdiscuzAjaxObj.usermail);
				$("#wpdcom .wc_uid").val(wpdiscuzAjaxObj.uid);
				Cookies.set("comment_author_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.username);
				Cookies.set("comment_author_email_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.usermail);
				Cookies.set("comment_author_uid_" + wpdiscuzAjaxObj.cookiehash, wpdiscuzAjaxObj.uid);
			}
		});`
	);
});
Reply
1 Reply
Asti
 Asti
Support
(@asti)
Joined: 6 years ago

Illustrious Member
Posts: 7056

@danihml,

Please let us know where you have added the code. Is it added to the active theme functions.php file? 

Reply
Posts: 4
Topic starter
(@danihml)
Active Member
Joined: 1 year ago

I inserted the code directly into a .js file that is loaded into the blog pages

Reply
2 Replies
Asti
 Asti
Support
(@asti)
Joined: 6 years ago

Illustrious Member
Posts: 7056

@danihml,

It's a PHP code and should be added in the active theme functions.php file. 

Reply
(@danihml)
Joined: 1 year ago

Active Member
Posts: 4

@asti there's no way to do that only on javascript? All my sources and data are on localStorage and JS apis, i dont know how to get all this data to PHP.

Reply
Share: