javascript - using PHP variable within onClick -


i wondering wrong tag using uses php variable within onclick event.

after looking around other answers on have tried reason doesnt work.

<a onclick="setcartdisplay('<?=$prodid;?>'); return false;" href="prodtest2.php?action=add&id=<?=$id;?>"> 

here code leading , including link tag doesnt work.

$prod_query = 'select * *****.*****';         $prod_details = db_query_into_array_enhanced($mysql_connection, $prod_query);                                $count = count($prod_details);         for($i = 0; $i < $count; $i++)              {?>                  <?  $prodid = $prod_details[$i]['catid'];                 $proddesc = $prod_details[$i]['shortdescription'];                 $prodprice = $prod_details[$i]['rrp'];             ?>              <tr>                  <td><?=$prodid;?></td>                  <td><?=$proddesc;?></td>                  <td><?=$prodprice;?></td>                  <td><a onclick="setcartdisplay('<?=$prodid;?>'); return false;" href="prodtest2.php?action=add&id=<?=$prodid;?>">add cart</a></td> 

a better solution store id php, id of anchor element , retrieve after using javascript.

as of html 5 characters valid id (and product id guessing there not duplicates, if there add class instead prefix). way require have class on anchors.

this gets around having set onclick of a tag, in eyes bad idea mixes html javascript, , imagine if had 1000 of these links on page , function name changed, can start see why becomes better solution;

the html;

<a id="<?= $prodid; ?>" class="product" href="prodtest2.php?action=add&id=<?= $id; ?>"> 

and javascript (jquery)

$(".product").click(function() {      var id = $(this).attr("id");      setcartdisplay(id); }); 

the reason link not working in first place because returning false function call disables default behaviour of click event.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -