AnswerBun.com

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:

js:

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

                    $.ajax({
                        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

Remove web-inspector-hide-shortcut

0  Asked on January 9, 2021 by dumi-padureanu

   

Pandas – Lambda inside apply to return a row

1  Asked on January 9, 2021 by marcio

         

Unclear webhook eventNotification documentation

1  Asked on January 9, 2021 by nolan-aguirre

 

c – Access not within mapped region error

0  Asked on January 9, 2021 by rhcp_lean

     

How to reduce committed memory usage in Java

1  Asked on January 9, 2021 by simon-hu

   

mutool / mulib edit text (with a c programm)

1  Asked on January 9, 2021 by atticus

   

Running browsermob with selenium grid on docker

2  Asked on January 8, 2021 by eduardoreynoso

         

How do I exclude a repeated click event?

2  Asked on January 8, 2021 by jessy

   

handling promises using await and async

2  Asked on January 8, 2021 by jass-preet

   

How to compare data from the same column in a dataframe (Pandas)

4  Asked on January 8, 2021 by abraham-arreola

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP