title: How can I put PHP code to HTML using JavaScript?
body: <p>I making ajax request on my form in wordpress/woocommerce. I'm trying to show error message when user haven't been found. I tried to do it using the function <code>username_exist()</code>, but it doesn't work. The only way i find to do that is use <code>error:</code> in ajax-request and then past any message i want. But the problem is that i can't translate this message. I tried to put to the message <code>&lt;?php wc_print_notices() ?&gt;</code> but it shows as comment. Please help me solve this problem.
  I have this code:</p>
  <p>html form:</p>
  <pre><code>&lt;form class=&quot;login&quot; id=&quot;login&quot; method=&quot;post&quot;&gt;
    &lt;p class=&quot;status_login&quot;&gt;
    &lt;/p&gt;
    &lt;div class=&quot;form-field&quot;&gt;
      &lt;label for=&quot;user_login&quot; class=&quot;label&quot;&gt;&lt;?php esc_html_e('Username or email address', 'woocommerce'); ?&gt;&amp;nbsp;&lt;span
          class=&quot;required&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input type=&quot;text&quot; class=&quot; h-full-width c-h&quot; name=&quot;user_login&quot; id=&quot;user_login&quot; autocomplete=&quot;user_login&quot;
        value=&quot;&lt;?php echo (!empty($_POST['username'])) ? esc_attr(wp_unslash($_POST['username'])) : ''; ?&gt;&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;form-field&quot;&gt;&lt;label for=&quot;user_pass&quot;&gt;&lt;?php esc_html_e('Password', 'woocommerce'); ?&gt;&amp;nbsp;&lt;span
          class=&quot;required&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
      &lt;input class=&quot; h-full-width c-h&quot; type=&quot;password&quot; name=&quot;user_pass&quot; id=&quot;user_pass&quot; autocomplete=&quot;current-password&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;group&quot;&gt;
      &lt;label class=&quot;woocommerce-form__label woocommerce-form__label-for-checkbox woocommerce-form-login__rememberme&quot;&gt;
        &lt;input class=&quot;woocommerce-form__input woocommerce-form__input-checkbox&quot; name=&quot;rememberme&quot; type=&quot;checkbox&quot;
          id=&quot;rememberme&quot; value=&quot;forever&quot; /&gt; &lt;span&gt;&lt;?php esc_html_e('Remember me', 'woocommerce'); ?&gt;&lt;/span&gt;
      &lt;/label&gt;
    &lt;/div&gt;
    &lt;div class=&quot;group&quot;&gt;
      &lt;?php wp_nonce_field('ajax-login-nonce', 'security'); ?&gt;
      &lt;button type=&quot;submit&quot; class=&quot;btn btn--primary h-full-width&quot; name=&quot;login&quot;
        value=&quot;&lt;?php esc_attr_e('Log in', 'woocommerce'); ?&gt;&quot;&gt;&lt;?php esc_html_e('Log in', 'woocommerce'); ?&gt;&lt;/button&gt;
    &lt;/div&gt;
    &lt;div class=&quot;hr&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;foot&quot;&gt;
      &lt;a
        href=&quot;&lt;?php echo esc_url(wp_lostpassword_url()); ?&gt;&quot;&gt;&lt;?php esc_html_e('Lost your password?', 'woocommerce'); ?&gt;&lt;/a&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Javascript</p>
  <pre><code>  $j.ajax({
      type: 'POST',
      dataType: 'json',
      url: ajax_login_object.ajaxurl,
      data: {
        'action': 'ajaxlogin', //calls wp_ajax_nopriv_ajaxlogin
        'username': $j('#user_login').val(),
        'password': $j('#user_pass').val(),
        'security': $j('#login #security').val()
  
      },
      success: function (data) {
        console.log(data)
        $j('.status_login').html(data.message);
        if (data.error == false) {
          $j('.status_login').html(data.message);
          document.location.href = ajax_login_object.redirecturl;
        }
      },
      error: function (msg) {
        $text = '&lt;?php (Hello, world) ?&gt;'
        $j('.status_login').html('&lt;?php wc_print_notices() ?&gt;);
      },
    });
    e.preventDefault();
  });
  </code></pre>
  <p>php code:</p>
  <pre><code>function ajax_login_init()
          {
  
  
              wp_enqueue_script('ajax-login-script', get_template_directory_uri() . '/assets/js/ajax_form.js', array('jquery'), null, true);
  
              wp_localize_script('ajax-login-script', 'ajax_login_object', array(
                  'ajaxurl' =&gt; admin_url('admin-ajax.php'),
                  'redirecturl' =&gt; &quot;//&quot; . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
                  'loadingmessage' =&gt; '&lt;div class=&quot;alert alert-success&quot;&gt;' . __('checking', 'malik') . '
    &lt;/div&gt;'
              ));
              add_action('wp_ajax_nopriv_ajaxlogin', 'ajax_login');
          }
          if (!is_user_logged_in()) {
              add_action('init', 'ajax_login_init');
          }
          require_once ABSPATH . WPINC . '/user.php';
          function ajax_login()
          {
              if (!check_ajax_referer('ajax-login-nonce', 'security')) {
  
                  echo json_encode(array('error' =&gt; true, 'message' =&gt; '&lt;div class=&quot;alert alert-danger&quot;&gt;' . __('Session token has
      expired, please reload the page and try again', 'malik') . '&lt;/div&gt;'));
  
                  die();
              }
  
              // Nonce is checked, get the POST data and sign user on
              $info = array();
              $info['user_login'] = !empty($_POST['username']) ? sanitize_user(trim($_POST['username'])) : NULL;
              $info['user_password'] = !empty($_POST['password']) ? sanitize_text_field(trim($_POST['password'])) : NULL;
              $info['remember'] = true;
              $user_signon = wp_signon($info, false);
              $user_login = sanitize_user($_POST['username']);
              $user_pass = sanitize_text_field(trim($_POST['password']));
              $user_exists = false;
  
              if (username_exists($user_login)) {
                  $user_exists = true;
                  $user = get_user_by('login', $user_login);
              } elseif (email_exists($user_login)) {
                  $user_exists = true;
                  $user = get_user_by('email', $user_login);
              } elseif (!empty($user_login) &amp;&amp; !empty($user_pass)) {
                  $error = new WP_Error('no_user_found', 'Username or Email was not found, please try again', 'Page Data');
              } else {
                  echo json_encode(array('error' =&gt; true, 'message' =&gt; '&lt;div class=&quot;alert alert-danger&quot;&gt;' .
                      $user_signon-&gt;get_error_message() . '&lt;/div&gt;'));
                  die();
              }
  
  
              if ($user_exists === true) {
                  $user_id = $user-&gt;ID;
                  $user_data = get_userdata($user_id);
                  $username = $user_data-&gt;user_login;
                  $userpass = $user_data-&gt;user_pass;
              } elseif (is_wp_error($user_signon) || $user_exists === false) {
                  echo json_encode(array('error' =&gt; true, 'message' =&gt; '&lt;div class=&quot;alert alert-danger&quot;&gt;' .
                      $user_signon-&gt;get_error_message() . '&lt;/div&gt;'));
                  die();
              } else {
                  wp_clear_auth_cookie();
                  wp_set_current_user($user_id, $username);
                  wp_set_auth_cookie($user_id);
                  echo json_encode(array('error' =&gt; false, 'message' =&gt; '&lt;div class=&quot;alert alert-success&quot;&gt;' . __('Login successful,
      reloading page...', 'malik') . '&lt;/div&gt;'));
  
                  die();
              }
              die();
          }
  </code></pre>
  <p>I and when login=faled i have this:</p>
  <pre><code>&lt;p class=&quot;status_login&quot;&gt;
  &lt;!--?php  wc_print_notices() ?--&gt;
        &lt;/p&gt;
  </code></pre>