0

Can anyone show me how to use html button as a link to connect to php? I'm not quite sure how to do it and can't find any useful code that can help me to achieve it. I'm pretty new so bit struggling with it.thanks

https://i.stack.imgur.com/yDHEN.jpg

[![<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
h1 {
    padding-top: 50px;
    text-align: center;
    color: white;
    margin-right:60px;
}
</style>
    <title> Washing Machine Control System</title>
    <link rel="stylesheet" type="text/css" href="BK.css"/>
    <link rel="stylesheet" type="text/css" href="MachineButtons.css"/>
    <script src="http://code.jquery.com-1.11.3.min.js"></script>
</head>

<body bgcolor = "Black"> 


<h1>Machine Controller</h1>

    <br></br>
    <br></br>



<form method="GET/POST" action="theNameOfMyPHPFile.php">

<div id="icons"><img src="D:\Year 4 (Sem1&2)\Mobile Technologies\Coursework\Website\WM1.jpg"  alt="Machine One" style="width:150px;height:228px;"/></a></div>

        <label class="switch">
            <input class="switch-input" type="checkbox" />
            <span class="switch-label" data-on="M1 On" data-off="M1 Off"></span> <span class="switch-handle"></span> 
        </label>

<div id="icons2"><img src="D:\Year 4 (Sem1&2)\Mobile Technologies\Coursework\Website\WM2.jpg" alt="Machine Two" style="width:150px;height:228px;"/></a></div>

        <label class="switch">
            <input class="switch-input" type="checkbox" />
            <span class="switch-label" data-on="M2 On" data-off="M2 Off"></span> <span class="switch-handle"></span> 
        </label>

<div id="icons4"><img src="D:\Year 4 (Sem1&2)\Mobile Technologies\Coursework\Website\WM3.jpg"  alt="Machine Three" style="width:150px;height:228px;"/></a></div>
        <label class="switch">
            <input class="switch-input" type="checkbox" />
            <span class="switch-label" data-on="M3 On" data-off="M3 Off"></span> <span class="switch-handle"></span> 
        </label>

<div id="icons3"><img src="D:\Year 4 (Sem1&2)\Mobile Technologies\Coursework\Website\WM4.jpg" alt="Machine Four" style="width:150px;height:228px;"/></a></div>

        <label class="switch">
            <input class="switch-input" type="checkbox" />
            <span class="switch-label" data-on="M4 On" data-off="M4 Off"></span> <span class="switch-handle"></span> 
        </label>
</form>     
</body>
</html>]
Sunny Razario
  • 31
  • 1
  • 7

3 Answers3

1

While these answers will get you the button, they will not show you how to 'connect' it with PHP. In order to have a button fire PHP code, you must put it in a form and set the form's action to a PHP script. Then, when the form is submitted, it will POST data to the php script you specified. Like so:

HTML:

<form action="myscript.php">
    <input type="submit" name="onButton" value="On" />
    <input type="submit" name="offButton" value="Off" />
</form>

PHP:

<?php 
if (isset($_POST['onButton'])) {
    echo "On button was pressed.";
} else if (isset($_POST['offButton']){
    echo "Off button was pressed.";
}
?>
Mikel Bitson
  • 3,583
  • 1
  • 18
  • 23
  • @Script47 Edited, thanks! – Mikel Bitson Oct 27 '15 at 15:14
  • @MikelBitson Do i have to take out the label tag out for this? and I don't want any text box for this. i only need the button to push the button that is press to php, so php need to add to database if the button is ON/OFF. thanks – Sunny Razario Oct 27 '15 at 16:51
  • @SunnyRazario Hey there Sunny! I've adjusted my answer to include two submit buttons and the PHP to detect which was clicked. – Mikel Bitson Oct 27 '15 at 17:40
0

echo '<a href="someurl.php"><button>I\'m a button</button></a>';

Brad Kent
  • 4,982
  • 3
  • 22
  • 26
0

This is one way:

<button onclick="go()">Button</button>


<script>
    function go() {
     window.location.href = "/path/to/url";
    }
</script>
Justice
  • 290
  • 2
  • 7