Amare
Amare

Reputation: 715

How to call a php variable in jQuery?

I want to get the value of $jrowa['rate_score'] in PHP part as num3 in jQuery part. But the output is NaN for the alert in num3.

Here is my PHP code.

$jsqla = mysql_query("select id,name,rate_score,rate_number,video_image from products where genre='$genre' limit 0,5");

$arate_num = $jrowa['rate_number'];
    if($jrowa['rate_number'] > 0){ 
        $ratea = $jrowa['rate_score'] / $jrowa['rate_number']; 
        $ratea2 = $jrowa['rate_score'];
        $rateid = $jrowa['id'];
        $ratenum = $jrowa['rate_number'];
    }else{ 
        $ratea = $jrowa['rate_score']; 
        $ratea2 = $jrowa['rate_score'];
        $rateid = $jrowa['id'];
        $ratenum = $jrowa['rate_number'];
    }

Here is my HTML code.

<div class="col-sm-2 portfolio-item" style="width: 20%;">

     <input class="rating form-control input-star-rate" id="<?php echo $rateid; ?>" name="rating" value="<?php echo $ratea; ?>" data-min="0" data-max="5" data-step="0.3" data-size="xs" style="display: none; text-align: center;"/>

</div>

Here is my jQuery code.

$(function(){
    $(document).ready(function(e) {
        var $stars = $('.input-star-rate');

        $stars.bind('change', function() {
            var $this = $(this); //  assign $(this) to $this
            var ratingValue = $this.val();
            alert(ratingValue);
            var ratingValue2 = parseFloat(ratingValue);
            alert(ratingValue2);

            var ratingValuePrevious = parseFloat($(this).attr("value"));
            var ratingValuePrevious2 = parseFloat(ratingValuePrevious);
            alert(ratingValuePrevious2);

            var id = $this.attr("id");
            alert(id);

            var phpvalue = "<?php echo $jrowa['rate_score']; ?>";
            var num3 = parseInt(phpvalue, 10);
            alert(num3);

        });
    });
});

Upvotes: 1

Views: 278

Answers (1)

user4281203
user4281203

Reputation:

try this.

html code:

<input class="rating form-control input-star-rate" id="<?php echo $rateid; ?>" name="rating" value="<?php echo $ratea; ?>" data-min="0" data-max="5" data-step="0.3" data-size="xs" style="display: none; text-align: center;" lats="<?php echo $rateid; ?>"/>

then in jquery:

var vals = $(this).attr('lats');// this will store your php variable value in jquery variable vals.

Upvotes: 1

Related Questions