-1

Im trying to pass a php variable from php page and use that variable in jq.

trying to assing to the image a class that is a php variable

the php code:

while($row = $results->fetch_assoc()) {
$id = bin2hex(openssl_random_pseudo_bytes(8));
$pro_makat=$row["product_makat"];
$pro_id = $row['product_id'];
$products_list .= <<<EOT

<li>
<form class="form-item">
<h3 id="title" style="color:#cc00cc;">{$row["product_title"]}</h3>
<div><a href=""><img id="image" src="admin_area/product_images/{$row['product_image']}" class="$id" ></a></div>

and that image class $id I want to use in my jq code

    $(".$id").click(function(e) { 
    e.preventDefault(); 
    $.post("product_details.php", {
        makat : $('#makat').val()
    }, 
        function(data){

        }
    );

how is it possible to make it work?

Svidlak
  • 88
  • 1
  • 11
  • [tag:jq] is a Linux command line utility for handling JSON data. Is that really what you want here? –  Jul 29 '15 at 00:03
  • im new to programming but heres basically what im trying to do: get images from the db with while(){} and each image to have same class but unique value for that class so I can manipulate it with jq (make a unique popup window for each image) – Svidlak Jul 29 '15 at 00:08
  • 1
    `".$id"` is invalid you should do `".$id."` notice the missing `.` this is like the `+` in javascript, for string concatenation . – ArtisticPhoenix Jul 29 '15 at 00:12
  • @svidlak You can't do any of what you want with [tag:jq]. If you mean [tag:jQuery] then say jQuery. –  Jul 29 '15 at 00:24

2 Answers2

0

to echo an variable inside html/javascript just do:

$(".<?php echo $id ?>").click(function(e) {
....

...assuming you are OUT of the EOT

Jeff
  • 6,895
  • 1
  • 15
  • 33
0

You almost had it but for a syntax error.

 $('.".$id"').click(function(e) { 
 ....

Should be

 $('.".$id."').click(function(e) {
  ....

Notice the missing . which is like the + in javascipt or the concatenation operator. But you still will need the css selector such as . for class and # for id, and be sure to not forget the ' single quote for the javascript. Although I am pretty sure because you have a HEREDOC, you could get away with just doing this instead $(."{$id}")... or $(.'{$id}')... maybe even just $(.'$id')...

I know it gets a bit confusing with all these different languages using different symbols, or using them for different things. Personally I'm not a big fan of PHP using the ., but because of the loose typing the + is out because you can actually add string values in php such as '1' + 1 = 2 so you see that is one of the reason PHP uses that instead of the plus. What should PHP do in that case add them or make it 11? So they use the dot.

That is assuming this is all within the heredoc <<<EOT otherwise you'll need to use <?php echo $id; ?>

I love heredocs ... just saying. What is the advantage of using Heredoc in PHP ?

Just for clarity when using the HEREDOC or NEWDOC you have to end them on their own line. So you have to do them like this

$a = array(
    'content' =>  <<<DOC


DOC
);

OR

$content =  <<<DOC


DOC;

Same for NEWDOC with is like the ' single quote in that there is no variable interpolation so

$novar = 'Hello';

echo  <<<'DOC'
$novar
DOC;

echo  <<<DOC
$novar
DOC;

So the first prints

 $novar

But the second prints

 Hello
Community
  • 1
  • 1
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • he will still need the `.` before the "id" to make jQuery search for the classname – Jeff Jul 29 '15 at 00:17
  • There are also newdoc, so the heredoc is like the `"` and the new doc `<<<'DOC'` is like the `'` the trick with them is the ending must be on a line by themself and must end the line. Including no indents. – ArtisticPhoenix Jul 29 '15 at 00:24