Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
Is there a way to pass value of a Button on click to another php file?

My aim is to get the lec_id which is the Button value when the button is clicked and pass it to chapters.php where I use the button value for a SQL query.

Below is part of my code for 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";
                    }
?>

my button onclick function

  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';
    }

I want to use the Button value here in chapters.php for a SQL query.

<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 Respostas
Responde à pergunta

0

Welcome to Stack Overflow! As Barmar stated in their comment, you can pass data to a PHP file using URL parameters, or more commonly known as GET parameters. Here's how you do it.

From your file with your button in it, you can create a form like this one:

<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>

And then from your PHP file, you can get that passed data like this:

echo $_GET["data"]

$_GET is a global PHP array that contains all of the URL parameters sent to the file. you can pass multiple values as GET parameters to a file. You can read all about the $_GET variable here. I hope this helps!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda