Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input type button cant send PHP Variable?

Tags:

html

php

I Have table on browser Like This

  Name  | Action        |
________________________
Septiyo | Edit | Delete |
Fahmi   | Edit | Delete | 
Tejo    | Edit | Delete |

For Edit, usually I use link like this

  echo "<a href='edit.php?ID=$data['ID']'>Edit</a>";

With link, I can Include the ID variable and send to other Page.

The question is, if I change The Link with Button html. How can I include the variable?

My Button like this

 echo  "<input type='button' value='Edit' onclick='window.location=edit.php?ID=$data['ID']'>";

and it not Work.

Can anyone Help me?

Im very Appreciated your Answer.

Thanks.

like image 656
Uchsun Avatar asked May 09 '26 05:05

Uchsun


2 Answers

Why not wrap your button in an <a> with the href set to the php page?

Like so:

echo <a href=yourPage.php><input type="button" value="Edit"></a>

A better option to consider might be taking out the input and styling the link to look like a button. It is standards compliant as well, where the other option may not be.

like image 77
tjons Avatar answered May 11 '26 20:05

tjons


Do as this:

<a href='edit.php?ID=<?php echo $data['ID']; ?>'>Edit</a>

PHP is processed on the server side. So, it must know that the code you are writing is php and it knows when you put it inside the php tags <?php //php code ?>

That was just pointed the answer for the OP would be:

For you send data through a button you have some ways:

  • Call a function to do the redirection
  • put the value in a form and submit it.
  • With a form it would be:

    <form action="edit.php" method="post">
       <input type="hidden" name="ID" value="<?php echo $data['ID']; ?>">
       <input type="submit" value="Send">
    </form>
    

    Another way would be through some javascript function:

    <input 
      type="button" 
      onclick="javascript:window.open('edit.php?ID=<?php echo $data['ID']; ?>','','');">
    

    And one more:

    <script>
         function sendValue(id){
             window.location='edit.php?ID=' + id;
         }
    </script>
    <input 
      type="button" 
      onclick="javascript:sendValue('<?php echo $data['ID']; ?>')>
    
    like image 23
    Jorge Campos Avatar answered May 11 '26 18:05

    Jorge Campos



    Donate For Us

    If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!