How to send a php var to jQuery Ajax call with two different file

I need to send a private e-mail address from a php file (page.php) to an Ajax Call in an other jquery file (sender.js). This e-mail address must be invisible to user/or website until the user get an automatic response from wp_mail, It must allowed it to answer to the organization.

This e-mail address is different according to the organization that the client want to contact (several hundred). I'm obliged to get the e-mail address from organization before form because it is generated by javascript. (generated_mail.js)

Sender.js is well called in header for page.php

I tried this code inside (page.php) - one organization is called with ajax inside page.php:

?php $adressMailEntity = '[email protected] *'; ? 
script type=text/plainvar adressMailEntity = ?php echo $adressMailEntity ; ? ;/script
  • This e-mail address is for example , but must come from database. jquery code :
    (function($){
    var adressMailEntity;
        
            $(document).on('submit','.w-sender',function(e){
                e.preventDefault();
                var error = false,
                    form = $(this).closest('form'),
                    busy = null;
                
                
                var formSerialize = form.serialize();
                var entityName = $('.s_ref_mail').text();
                var entityLogo = $('.atv_bg_WinEnterprise .logo').attr('src');
                //How to get php var $adressMailEntity , or jquery var adressMailEntity ?
                console.log(adressMailEntity);  
                if(!error){
                    if (busy)
                        busy.abort();
                    
                    busy = $.ajax({
                        url : ajaxSender.ajax_url,
                        data : {
                            action : 'mail_form',
                            nonce: ajaxSender.nonce,
                            formFields : formSerialize,
                            entityName : entityName,
                            entityLogo : entityLogo,
                            adressMailEntity : adressMailEntity
                        },
                        type : 'POST',
                        success : function(response){
                        
                        },
                        error : function(response){  
                    
                        }
                      })
                }
                return false;
                 
            });
        
    })(jQuery);

Topic functions variables php jquery Wordpress

Category Web


I use the data attribute to pass variables from PHP to JS.

<div data-your-php-variable="<?php echo $yourCoolVar; ?>"></div> 

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.