使用CodeIgniter访问jQuery中的PHP变量[重复]

I have the following defined in my controller:

$data['is_amazon'] = $product->is_amazon;
$data['is_ebay']   = $product->is_ebay;

Directly below this in the controller I'm calling a .js file (jQuery).

$this->template->add_js('js/is_ebay_amazon_prouct_edit.js');

I'd like to access these php variables in my jQuery script, I've tried this approach:

var amazon = '<?php echo $is_amazon; ?>';
var ebay = '<?php echo $is_ebay; ?>';


jQuery(document).ready(function($) {

    alert(amazon);
    //alert(ebay);

});

Although this just displays my physical text in the alert, I.E:

<?php echo $is_ebay; ?>

What is the best approach here in terms of making these variables accessible in my jQuery?

</div>

Try this

do one thing.. you need to create two input hiddent field in view. like this

<input type='hidden' id="amazon" value="<?php echo $is_amazon; ?>" />
<input type='hidden' id="ebay" value="<?php echo $is_ebay; ?>" />

and call that value in you jquery lik this

$(document).ready(function() {

    //alert($('#amazon').val()+" "+$('#ebay').val());   

     console.log("amazon"+$('#amazon').val());
     console.log("ebay"+$('#ebay').val());    
});

now it will work for u.. try it.

There is no option to directly assign php variables to javascript. To do this you need to assign assign it to html elements like,

<input type="hidden" id ="amazon" value="<?php echo $is_amazon; ?>">

We can get the value in jquery from this html element by using this code,

var amazon = $('#amazon').val();

For security reasons the php variables cant assign to javascript variables.

Try this

<input type="hidden" id="fromAmazon" value="<?php echo $is_amazon ?>"/>
<input type="hidden" id="fromEbay" value="<?php echo $is_ebay ?>"/>

In jquery After loading jquery.js file

jQuery(document).ready(function($) {

    var amazon = $("#fromAmazon").val();
    var ebay = $("#fromEbay").val();

    console.log("amazon",amazon);
    console.log("ebay",ebay);

});