0

I'm having difficulties with the onclick jquery within my php code, how do I get a php variable so that name=variable and price=variable?

    <?php

    $result = mysqli_query($db,"SELECT * FROM table");

    while($row = mysqli_fetch_array($result)) {
      echo '<ul>';
      echo '<li>' . $row['selection'] . '<div style="float:right;"><a href="#" onclick="simpleCart.add('name='. $row['selection'] . ','price='. $row['price'] .');return false;
     "> '. $row['price'] . '</a></div></li>';
      echo '</ul>';
      }

      ?>

How do I get around this? Thanks

Don212
  • 1
  • Have you ever seen anyone call a function like `somefunc(param1='x', param2='y')`? Why not try `somefunc('x', 'y')`? – developerwjk Jun 23 '14 at 19:58
  • Learning basic PHP string syntax would be a good start: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single – Marc B Jun 23 '14 at 19:58
  • @developerwjk I think I've seen people use php functions to do such things. I saw it on `count` the other day when I was referencing something. `count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )` however that isn't the way it actually formats. I can understand the confusion though. None the less it is wrong and OP needs to parse his variables differently and correctly. I can understand the confusion though. – Jem Jun 23 '14 at 20:04

2 Answers2

0

change your code with this line...

echo '<div style="float:right;"><a href="#"
onclick="simpleCart.add(name='.$row['selection'].',price='.$row['price'].')"
</a></div>';
RNK
  • 5,582
  • 11
  • 65
  • 133
0

Not faniliar with simplecart, but You definitely need to crrectly escape your quotes.

<?php

    $result = mysqli_query($db,    "SELECT * FROM table");

    while($row = mysqli_fetch_array($result)) { 
        echo '<ul>'; 
        echo '<li>' . $row['selection'] . '<div style="float:right;"><a href="#" onclick="simpleCart.add(\'name=\''. $row['selection'] . ',\'price=\''. $row['price'] . ');return false; "> '. $row['price'] . '</a></div></li>'; echo '</ul>'; }

?>
ArVan
  • 4,225
  • 8
  • 36
  • 58