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

183
Views
¿Cómo puedo obtener el valor de múltiples anclas en javascript que se generan a partir de php y enviar el valor a otra página de Php usando AJAX?

Así es como se muestran mis anclas en la web. Necesito obtener el valor del ancla y enviarlo a otro archivo PHP, así que usaría el valor del ancla en mi consulta SQL para dar la condición de mostrar todo el ID de producto asociado con el valor del ancla.

 <?php include"validations/connection.php"; $sql = "SELECT * from category ORDER BY CHAR_LENGTH(categoryName)"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo' <div class="col-sm-12"> <ul class="nav flex-column"> '; while($row = $result->fetch_assoc()){ $id = $row['categoryId']; $sqltwo = "SELECT COUNT(productId) AS num FROM products WHERE categoryId = '$id'"; $resultwo = $conn->query($sqltwo); while($rowtwo = $resultwo->fetch_assoc()){ echo' <li class="nav-item"> <button value="'.$row['categoryId'].'" class="link-show">'.$row['categoryName'].'</button> <span class="badge badge-danger ml-1">'.$rowtwo['num'].'</span> </li> '; } } echo' </ul><hr> </div> '; } ?>

Y aquí está mi JavaScript

 <script> var id = document.querySelectorAll('.link-show').value; document.querySelectorAll('.link-show').forEach(item => { item.addEventListener('click', event => { var xhr = new XMLHttpRequest(); xhr.open('GET','validations/retrievebycategory.php?id='+id, true); xhr.onload = function(){ console.log(this.responseText); } xhr.send(); }) }) </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

 // Remove this, it's querying all links // var id = document.querySelectorAll('.link-show').value; document.querySelectorAll('.link-show').forEach(item => { item.addEventListener('click', event => { // In the event handler, "this" is set to event.currentTarget (the button, in this case). // Equivalent formulations: // let id = event.currentTarget.value; // (from event, standard behavior, equivalent to "this") // let id = event.target.value // (from event's initial target, equivalent in most cases, except if the "click" is generated by a nested element; generally unsafe to use unless the intent is clear) // let id = item.value; // (from closure) let id = this.value; var xhr = new XMLHttpRequest(); xhr.open('GET','validations/retrievebycategory.php?id='+id, true); xhr.onload = function(){ console.log(this.responseText); } xhr.send(); }) })

Ver modificaciones y explicaciones en el código

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!