繁体   English   中英

如何将html按钮链接到php?

[英]How to link the html button to php?

单击“打开/关闭”按钮后,如何连接按钮(位于HTML页面中)以连接到php url。 一旦单击按钮,代码基本上必须调用php,并且在调用php时,它需要将启动/关闭以及启动时间注册到我的数据库中。 可以请任何人用这段代码帮助我,因为我是新手,确实很卡住。 这个东西需要没有javascript或jquery的东西才能工作。

http://i.stack.imgur.com/8JAYn.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>


    <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>

    </body>
    </html>

我认为您将必须分阶段解决此问题。

如果您试图获取HTML字段值以通过PHP连接到数据库,则您需要先查看HTML标记,因为它还不足以实现这一目标。 输入元素需要放在表单元素中,以便启动数据库的PHP写或回调。 例如;

<form method="GET/POST" action="theNameOfYourPHPFile.php">
  <input class="switch-input" type="checkbox" name="washingMachine1" />
<input class="switch-input" type="checkbox" name="washingMachine2" />

等等...

一旦所有这些都准备就绪,您就可以使用指向表单的php文件中的php函数访问输入中的值。 通过此访问它们的开/关值

<?php $washingMachine1 = $_REQUEST['washingMachine1']; 
etc... 

?>

但是,您需要更多的PHP编码实践才能进一步做到这一点。 值得花一些时间来了解有关表单以及它们通过GET和POST方法与PHP交互的更多信息。 祝好运。

有两种方法可以使用表单提交从按钮调用php文件,另一种方法是使用ajax提交表单。

<form action='url of php file' method="post or get" ..... .... </form>

在提交按钮上,单击表单将提交以按操作定义。

其他方式

使用简单的按钮,然后单击按钮使用ajax调用php文件而无需重定向

在输入字段或要发送的值之前使用a并在表单操作中添加要将输入字段值发送到的php页面,然后在该php页面中根据需要编写插入查询。 例如:

我将使用AJAXJQuery

处理复选框的选中事件,单击时使用AJAX调用更新值:

$('.switch-input').click(function() {
   var $this = $(this);
   // $this will contain a reference to the checkbox   
   if ($this.is(':checked')) {
      // the checkbox is checked -> update db value
      var data = { 'id' : 1 }; //this is static, instead of 1 use JQuery to get the actual id of the record you want to update.
      $.ajax({
            url: 'update_val.php',
            type: 'POST',
            data: data,
            success: function(result){
                alert('success!');
            }
        });
   } else {
    // the checkbox is not checked
   }
});

有关更多信息,我建议您看一下以下参考资料:

希望这对您有所帮助:)

注意:要使用此方法,您显然必须存储对象的ID,以便在调用ajax函数时可以检索它们并将它们通过POST或GET传递到PHP页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM