Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Javascript error custom function to enter data into form field

List of products displayed from database using PHP. When product selected javascript function called to enter product into HMTL form field.

<div id="addProductPanel">
 <div class="popularProduct">Popular products</div>
 <div class="closeButton" onclick="closePopularProductWindow()">X</div>
 <?php
 $query_popular_product="SELECT product_code FROM database_table GROUP BY product_code ORDER BY product_code DESC";
      $result_popular_product = $link->query($query_popular_product);
      while($row = $result_popular_product->fetch_assoc()) {
        $product_code = $row['product_code'];
        echo '<div class="popularProduct" onclick="setProduct("'.$product_code.'")">'.$product_code.'</div>';
      }
 ?>
</div>

The js function setProduct is:

function setProduct(product) {
    console.log(product);
    document.getElementsByName("product")[0].value = product;
    document.getElementById("addProductPanel").style.display = "none";
}

When loading the page and using Chrome inspector console, selecting a product doesn't print the product in the console and produces the error Uncaught SyntaxError: Unexpected end of input with HTML displayed as below:

<div class="popularProduct" onclick="setProduct("PROD123")">PROD123</div>

I have tried typing the following HTML and this works successfully with the javascript function:

<div class="popularProduct" onclick="setProduct('PROD123')">PROD123</div>

Cannot get the PHP syntax working with the product values from the database.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use single quotes instead of double quotes:

echo '<div class="popularProduct" onclick="setProduct(\''.$product_code.'\')">'.$product_code.'</div>';

The double quotes will terminate the double quotes from onclick="setProduct(...

(Single quotes inside single quotes need to be escaped, so use \')

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!