0

I am trying to pass values from option panel to use them on jQuery setting value everything working except true or false SO How can I convert it or use it right way.

What I am trying where $offers_animation_type = true its return true but not working as like if direct write true or false in the option value.

$("#Ticker").breakingNews({
    effect      :"slide-v",
    autoplay    :<?php echo $offers_animation_type;?>,
    timer       :<?php echo $offers_animation_change;?>,
    color       :"turquoise",
    border      :true
});

But it should be like

$("#Ticker").breakingNews({
    effect      :"slide-v",
    autoplay    :true,
    timer       :3000,
    color       :"turquoise",
    border      :true
});
1
  • 1
    you should never have PHP inside JS (and vice versa). It's a bad practice that can lead to unexpected behaviour Commented Jul 17, 2019 at 11:07

3 Answers 3

2

In PHP printing true will result 1, but printing false will not print anything. So when the value was false, you broke the JS code. (autoplay:,)

$("#Ticker").breakingNews({
    effect      :"slide-v",
    autoplay    :<?php echo $offers_animation_type ? "true" : "false";?>,
    timer       :<?php echo $offers_animation_change;?>,
    color       :"turquoise",
    border      :true
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have added fixes on how I convert it using json encode.
1

you can use ternary operator in php like this

    $("#Ticker").breakingNews({
    effect      :"slide-v",
    autoplay    :<?php echo ($offers_animation_type): 'true'? 'false'; ?>,
    timer       :<?php echo ($offers_animation_change): 'true'? 'false'; ?>,
    color       :"turquoise",
    border      :true
});

Comments

0
 $("#Ticker").breakingNews({
   effect      :"slide-v",
   autoplay    :<?=(isset($offers_animation_type) && !empty($offers_animation_type))? 'true' : 'false'; ?>,
   timer       :<?=(isset($offers_animation_change) && !empty($offers_animation_change))? $offers_animation_change : 3000; ?>,
   color       :"turquoise",
   border      :true
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.