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

226
Views
¿Hay alguna forma de pasar el valor de un botón al hacer clic en otro archivo php?

Mi objetivo es obtener el lec_id, que es el valor del botón cuando se hace clic en el botón y pasarlo a chapters.php, donde uso el valor del botón para una consulta SQL.

A continuación se muestra parte de mi código para index.php

 <?php $con = mysqli_connect("localhost", "root", "", "lectureHub"); if(!$con) { die("Could not connect to MySql Server:" . mysqli_error()); } $query = "select * from lectures"; $result = mysqli_query($con, $query); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $lec_id = $row['lec_id']; $lec_name = $row['lec_name']; $lec_number = $row['lec_number']; $lec_views = $row['lec_views']; echo "<button id=linkButton name={$row['lec_name']} value={$row['lec_id']} type='button' class='btn btn-outline-primary lecture' onclick='buttonClicked(this)'> {$row['lec_name']} </button> "; } } else { echo "0 results"; } ?>

mi botón onclick función

 function buttonClicked(btn) { btn.click_counter = (btn.click_counter || 0) + 1; document.getElementById("num_clicks_feedback").textContent = `btn ${btn.getAttribute('name')} has been clicked ${btn.click_counter} times`; localStorage.setItem("lecId", btn.getAttribute('value')); location.href = 'index.php?action=lec_hub/chapters'; }

Quiero usar el valor del botón aquí en chapters.php para una consulta SQL.

 <html> <head></head> <body> <?php echo "<p id='lecId'></p>"; $con = mysqli_connect("localhost", "root", "", "lectureHub"); if(!$con) { die("Could not connect to MySql Server:" . mysqli_error()); } $query = "select * from chapters where <<this is where i want to use lecId>> "; ?> <script> function getValue(){ var lecId = localStorage.getItem("lecId"); document.getElementById("lecId").innerHTML = lecId; var resetValue= 0; localStorage.setItem("lecId", resetValue) } getValue() </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¡Bienvenido a Stack Overflow! Como dijo Barmar en su comentario, puede pasar datos a un archivo PHP usando parámetros de URL, o más comúnmente conocidos como parámetros GET. Así es como lo haces.

Desde su archivo con su botón en él, puede crear un formulario como este:

 <form action="chapters.php" method="get"> <input type="text" name="data" /> <!-- This is the value that will be passed --> <input type="submit" value="Button" /> <!-- This is your button --> </form>

Y luego, desde su archivo PHP, puede obtener los datos pasados como este:

 echo $_GET["data"]

$_GET es una matriz PHP global que contiene todos los parámetros de URL enviados al archivo. puede pasar múltiples valores como parámetros GET a un archivo. Puede leer todo sobre la variable $_GET aquí . ¡Espero que esto ayude!

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!