Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

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'].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading