1

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);

        });
    });
});
4
  • 2
    Your Javascript must be inside the PHP file. Commented Nov 27, 2014 at 11:09
  • In A PHP file. You can also access a global variable in another PHP file. Commented Nov 27, 2014 at 11:21
  • @S.Pols dosent that require the file with global variable included in first file using require_once or other function Commented Nov 27, 2014 at 11:26
  • Depents how you defined it, if you use define you don't need to include the file, but yes it needs to be loaded once. Commented Nov 27, 2014 at 11:29

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

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.