Pass the dynamic variable when button is pressed

Stack Overflow Asked by Francy on September 19, 2020

I have create this example code:

    // Create connection
    $conn = Connection();

    $result = $conn->query("SELECT * FROM `users` WHERE 1");

  if ($result->num_rows > 0)
    while($row = $result->fetch_assoc())
      $name=  $row["name"] ;
      $id=  $row["id"] ;
      echo "<div class='media text-muted pt-3'>";

      echo "<button class='mr-2 rounded btn-primary icon32 select_c'></button>";

      echo "<p class='media-body pb-3 mb-0 small lh-125 border-bottom border-gray'>";
      echo "<strong class='d-block text-gray-dark'>". $name ."</strong>";
      echo  $id;
      echo "</p>";
      echo "</div>";

I want to pass the id variable when the reference button is pressed using the post method. How can I do?

One Answer

You can use value on the button then use ajax:

echo "<button class='mr-2 rounded btn-primary icon32 select_c' value='".$row["id"]."' onClick='ajaxpost(this)'></button>";

Now use Ajax:


function ajaxpost(elem) {
                var id= elem.value;

                        url: "page.php",
                        type: "POST",
                        data: {id: id},
                        success: function (data) {
                         //what you want



Correct answer by Simone Rossaini on September 19, 2020

Add your own answers!

Related Questions

How to store a list of objects in sqflite?

1  Asked on January 17, 2021 by 0x4b50


SQL to allocate rows from one table to another

3  Asked on January 17, 2021 by user3093820


JavaScript LocaleDateString back to Date Object

1  Asked on January 16, 2021 by victor-molina


Python global variables unexpected behavior

3  Asked on January 16, 2021 by user118967


Spying module and mocking module function

3  Asked on January 16, 2021 by ninita


Palindrome Checker for strings in C

1  Asked on January 16, 2021 by bradley-felix


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir