【问题标题】:how to use button as a link? [closed]如何使用按钮作为链接? [关闭]
【发布时间】:2015-10-27 15:04:04
【问题描述】:

谁能告诉我如何使用 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>]

【问题讨论】:

标签: php html button


【解决方案1】:

虽然这些答案会让您获得按钮,但它们不会向您展示如何将其与 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.";
}
?>

【讨论】:

  • @Script47 已编辑,谢谢!
  • @MikelBitson 我必须为此取出标签吗?我不想要任何文本框。我只需要按钮将按下的按钮按下到 php,因此如果按钮为 ON/OFF,则 php 需要添加到数据库中。谢谢
  • @SunnyRazario 嘿,Sunny!我已经调整了我的答案,包括两个提交按钮和 PHP 来检测哪个被点击了。
【解决方案2】:

这是一种方式:

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


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

【讨论】:

    【解决方案3】:

    echo '&lt;a href="someurl.php"&gt;&lt;button&gt;I\'m a button&lt;/button&gt;&lt;/a&gt;';

    【讨论】:

    • 这表明没有与 PHP 交互,正如最初问题中所要求的那样。
    • 编辑为“与 php 交互”
    • 改变了我的投票!干杯!
    猜你喜欢
    • 2012-12-11
    • 2021-06-23
    • 1970-01-01
    • 2019-03-03
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多