title: Passing a PHP string and an integer as arguments to a JavaScript function
body: <p>I am trying to pass a string and an int from PHP as the arguments for a JavaScript function. I tried a lot of different solutions and understand that it has something to do with escape characters. But I can't figure it out, so I hope you can help me.
  This line is what is giving me troubles:</p>
  <pre><code>&lt;button onclick=&quot;addToCart(&lt;?php echo $productName ?&gt;, &lt;?php echo $price ?&gt;)&quot;&gt;Add to cart&lt;/button&gt;
  </code></pre>
  <p>If I pass the id for example, there is no problem, so it has to do with the fact that the product name is a string.</p>
  <p>Entire Code:</p>
  <pre><code>    &lt;?php
      $databaseConnection = connectToDatabase();
  
      $sql = &quot;SELECT * FROM product&quot;;
      $res = $databaseConnection-&gt;query($sql);
      $count = mysqli_num_rows($res);
  
      if ($count &gt; 0) {
          //products available
          while ($row = mysqli_fetch_assoc($res)) {
              $id = $row['id'];
              $productName = $row['productName'];
              $imageName = $row['imageName'];
              $price = $row['price'];
              $quantity = $row['quantity'];
      ?&gt;
              &lt;!--display all products--&gt;
              &lt;div class=&quot;col-4&quot;&gt;
                  &lt;img src=&quot;img/&lt;?php echo $imageName; ?&gt;&quot;&gt;
                  &lt;h4&gt;&lt;?php echo $productName; ?&gt;&lt;/h4&gt;
                  &lt;p&gt;&lt;?php echo $price; ?&gt; kr&lt;/p&gt;
                  &lt;p style=&quot;color: green&quot;&gt;&lt;?php echo $quantity; ?&gt; left in stock&lt;/p&gt;
                  &lt;button onclick=&quot;addToCart(&lt;?php echo $productName ?&gt;, &lt;?php echo $price ?&gt;)&quot;&gt;Add to cart&lt;/button&gt;
  
              &lt;/div&gt;
      &lt;?php
  
          }
      }
  
      ?&gt;
  </code></pre>
  <p>The function is in it is own .js file, and works if I pass to ints instead of strings.</p>
  <pre><code>function addToCart(x,y) {
      alert(x + y);
  }
  </code></pre>