0

PHP into JavaScript Var. Well, I tried the div method and for this I used the code below:

    <div class="service-container" data-service="
<?php echo bp_loggedin_user_domain() . BP_XPROFILE_SLUG . '/change-avatar/'; ?>"
 style="display:none;"><\div>

<?php 
$avatarcount = 0;
if ( is_user_logged_in() ) {
global $bp;
global $current_user;
get_currentuserinfo();
$local = BP_AVATAR_UPLOAD_PATH . '/avatars/' . $bp->loggedin_user->id;
$useremail = $current_user->user_email;

if (!file_exists($local) ) {
echo '<script type="text/javascript">
$(document).ready(function() {
    $('.service-container').each(function() {
        var container = $(this);
        var service = container.data('service');
        if(document.URL.search("change-avatar") != -1) { } else  {
               window.location = service;}
    });
});
      </script>';
}}?>

Received this syntax error:

    Parse error: syntax error, unexpected '').each(function()
{' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'

What I couldn't figure out?

Ray
  • 53
  • 7

2 Answers2

1

You can do it like, so you don't have to work with escape etc....

<?php
if (!file_exists($local) ) {
?>
<script type="text/javascript">
$(document).ready(function() {
    $('.service-container').each(function() {
        var container = $(this);
        var service = container.data('service');
        if (document.URL.search("change-avatar") != -1) {} else {
            window.location = service;
        }
    });
});
</script>;
<?php
}
?>
tanaydin
  • 4,692
  • 23
  • 42
0

You should change inside echo the '' to "" like this ..

 $('.service-container'). ==========  $(".service-container").
  • That fixed the syntax, however it seems like service value is zero, cause there's no redirect occur – Ray Feb 05 '19 at 13:00