How Do I Store Use A PHP Variable In My JS Script?
<p>I'm wondering where I would put code to take a variable from PHP (used in numbering classes) and store it in a variable that jQuery can access?</p>

<p>Here is an example:</p>

<pre><code>$(".object1-22").hover(function(e){
    $(".object2-22").show();
}, function(e) {
    $(".object2-22").hide();
});
</code></pre>

<p>So in my code, I have two objects. When you hover over the 22nd instance of object1, I want it to only display the 22nd instance of object2.</p>

<p>How could I perform this task?</p>

<p>As a note, I already have ids and classes set up properly.</p>

<p><strong>Edit: As requested, here is an example snippet of code and a jsfiddle.</strong></p>

<pre><code>&lt;script type="text/javascript"&gt;
var id = "&lt;?php echo $promotion.promotion_id; ?&gt;";
$(".blast-"+id).hover(function(){
    $(".promo-"+id).show(250);
}, function() {
    $(".promo-"+id).hide(250);
});
&lt;/script&gt;
</code></pre>

<p>I'm using the solution above, but it isn't working. However, when I replace the PHP snippet with a number (in this case, something like 516 would work), I get the exact reaction to what I need. For some reason, I just can't pull that variable from PHP and I'm not sure what to do.</p>

<p>For the jsfiddle, since you can't put PHP code into it, I made an example of what needs to happen using a single number. Imagine that the "var id" in this case was pulled from PHP. That's what I need to happen.</p>

<p>The jsfiddle is <a href="http://jsfiddle.net/Xwrff/7/" rel="nofollow">here</a>.</p>

<p><strong>Edit2:</strong> I now have it working in a strange way. Using the fiddle's formatting as my example, it seems that whether or not I hover over ".object1-24", ".object1-25", etc, it will always show/hide ".object2-22", regardless of which one I hover over, and no others. Thoughts?</p>

<p><strong>Edit3:</strong> Okay, so its working now, I just had some formatting errors... however, instead of being able to do this:</p>

<pre><code>&lt;script type="text/javascript"&gt;
var id = "&lt;?php echo $promotion.promotion_id; ?&gt;";
$(".blast-"+id).hover(function(){
    $(".promo-"+id).show(250);
}, function() {
    $(".promo-"+id).hide(250);
});
&lt;/script&gt;
</code></pre>

<p>I had to do something like this:</p>

<pre><code>&lt;script type="text/javascript"&gt;
$(".blast-"+&lt;?php echo $promotion.promotion_id; ?&gt;).hover(function(){
    $(".promo-"+&lt;?php echo $promotion.promotion_id; ?&gt;).show(250);
}, function() {
    $(".promo-"+&lt;?php echo $promotion.promotion_id; ?&gt;).hide(250);
});
&lt;/script&gt;
</code></pre>

<p>It looks very messy, and I'd rather use the first method, by why isn't it working?</p>

<p>The script seems to be getting the variables properly, but it is only assigning all the spots where I do "+id" to the most recently grabbed version of that variable... what gives?</p>
