Home > Enterprise >  Is there a way, to send the value of a button to different pages in PHP?
Is there a way, to send the value of a button to different pages in PHP?

Time:03-04

I am working on project and came across the problem: When i click a button on the e.g. //website/manageuser.php I want the value of the button in the //website/edituser.php

I tried using the <value> tag in a button but failed. I wasn't able to manage to transfer the value through the files.

My manageuser.php looks like this: Test

After i press the e.g. "Delete" button, i get forwarded to the /deleteuser.php where I want to print the value behind the button. Unfortunatly, it isn't working with a StackOverflow Javascript script.

Image of my /deleteuser.php:

enter image description here

Code of the important part of manageuser.php:

<table>
    <tr>
            
<?php
$query = "select * from users";
$records = mysqli_query($con, $query); 
while($data = mysqli_fetch_array($records)) 
{   
?>
        <th>
            <p/>
<?php
    print($data["full_name"]);
?>                  
            <button type="button" value=" <?php print($data['full_name']) ?>" onclick="window.location.href='./edituser.php'">Edit</button>
            <button type="button" onclick="window.location.href='./deleteuser.php'"> Delete </button> 
        </th>
<?php
}
?>  
    </tr>
</table>

Code of the important part of deleteuser.php:

<!DOCTYPE html>
<html>
   <body>
      
      <script>
         var str1 = document.getElementById("btn").innerHTML;
         var str2 = document.getElementById("btn").value;
         document.write("Button text: " str1);
         document.write("<br>Button value: " str2);
      </script>
   </body>
</html>

CodePudding user response:

You don't need the value. Just append the value from $data as a query parameter to the URL.

<button type="button" onclick="window.location.href='./deleteuser.php?id=<?php echo $data['id'] ?>"> Delete </button>

Then in deleteuser.php you can use $_GET['id'].

  • Related