-3

I want to use a variable in my JS code but I failed. I tried a few things that I found here but not worked.

I should use this into Javascript; option-<?php echo $option['product_option_id']; ?>

Here is my Javascript:

<script type="text/javascript" >
    $("#series").chained("#mark");

</script>

I need to use my variable instead of #mark

Edit: I want to use chained jQuery with this variable. I have to use this variable. Simply, I need to remove #mark and place my variable.

Thank you!

birlikbilisim
  • 163
  • 2
  • 5
  • 14
  • 4
    "I tried a few things that I found here but not worked" — What things? What do you mean "not worked"? What did you expect to happen? What actually happened? – Quentin Sep 07 '13 at 07:38
  • what is option-?Let me know your code? – VIVEK-MDU Sep 07 '13 at 07:38
  • 1
    I think this question is similar to your problem. http://stackoverflow.com/questions/1808108/how-to-access-php-variables-in-javascript-or-jquery-rather-than-php-echo-vari – mic4ael Sep 07 '13 at 07:38
  • Please provide few more details, so that we could help you. – Eugine Joseph Sep 07 '13 at 07:42

4 Answers4

0

Maybe you want

$("#series").chained('# + "<?php echo $option['product_option_id'];?>"');
Rikard
  • 7,485
  • 11
  • 55
  • 92
  • Yes I need something like this but not this one, because this one is not working with chained jQuery (http://www.appelsiini.net/projects/chained) – birlikbilisim Sep 07 '13 at 07:59
0

You should pass whole objects so they can be passed via an ajax call, if required.

For example, on the PHP side, create an array (or standard object). Then use json_encode.

In PHP:

$my_js_container = array(
   'variable_1' => 'value 1',
   'variable_2' => 'value 2'
);

On the template:

<script>
    var php_vars = <?php echo json_encode( $my_js_container ); ?>;

    alert( php_vars.variable_1 );

    //or

    $( "#series" ).chained( "#" + php_vars.variable_2 );
</script>

In this example, you're printing the json_encode string to the page. However, you could just as easily have the json_encode returned from a specific url that you load from an AJAX call. I see that you're using jQuery, so using jQuery, here is an example of how to load a variable from the client side (making a request to the server side):

http://api.jquery.com/jQuery.getJSON/

Try to pass your variable container around (in a uniform format), instead of single variables. It can come in handy later when you need to move to a different context (such as database, or template, or AJAX, or client-side store, or form submission).

Homer6
  • 15,034
  • 11
  • 61
  • 81
0
<?php $option['product_option_id'] = "some value"; ?>

<script type="text/javascript" >
var i = "<?php echo $option['product_option_id']; ?>"
$("#series").chained("i");

</script>

This should work

dk396
  • 91
  • 1
  • 2
0
<script type="text/javascript" >
    var v = <?php echo $option['product_option_id']; ?>;
    $("#series").chained("'#"+v+"'" );
</script>
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89