如何使用按钮作为链接?

时间:2015-10-27 15:04:05

标签: php html button

任何人都可以告诉我如何使用html按钮作为连接到PHP的链接?我不太清楚如何做到这一点,找不到任何可以帮助我实现它的有用代码。我很新,所以有点挣扎着。谢谢

http://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>]

3 个答案:

答案 0 :(得分:1)

虽然这些答案会为您提供按钮,但它们不会向您显示如何连接&#39;它与PHP。为了让按钮触发PHP代码,您必须将其放在表单中并将表单的操作设置为PHP脚本。然后,当提交表单时,它会将数据POST到您指定的php脚本。像这样:

<强> 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.";
}
?>

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是一种方式:

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


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