简体   繁体   English

如何将html按钮链接到php?

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

how do I connect the button (which is in HTML page) to connect to php url, once the ON/OFF button is clicked. 单击“打开/关闭”按钮后,如何连接按钮(位于HTML页面中)以连接到php url。 the code basically have to call the php once the button is clicked and when the php is called it need register the ON/OFF to my database along with starting time. 一旦单击按钮,代码基本上必须调用php,并且在调用php时,它需要将启动/关闭以及启动时间注册到我的数据库中。 can please anyone help me out with this code because i'm new and really stuck. 可以请任何人用这段代码帮助我,因为我是新手,确实很卡住。 this thing needs to work without javascript or jquery stuff. 这个东西需要没有javascript或jquery的东西才能工作。

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

I think you're going to have to solve this in stages. 我认为您将必须分阶段解决此问题。

If you're trying to get the HTML field values to connect to a database via PHP, you'll need to look at your HTML mark up first, as it's not prepared enough to achieve this. 如果您试图获取HTML字段值以通过PHP连接到数据库,则您需要先查看HTML标记,因为它还不足以实现这一目标。 Input elements need to sit within form elements in order to initiate a PHP write-to or callback from a database. 输入元素需要放在表单元素中,以便启动数据库的PHP写或回调。 For example; 例如;

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

etc... 等等...

Once you've got this all in place, you'll then be able to access the values in the inputs using your php functions in the php file you're pointing the form to. 一旦所有这些都准备就绪,您就可以使用指向表单的php文件中的php函数访问输入中的值。 Accessing their on/off values via this 通过此访问它们的开/关值

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

?>

But you'll need more PHP coding practice to take this further. 但是,您需要更多的PHP编码实践才能进一步做到这一点。 Worth spending some time to learn more about forms and their interaction with PHP via GET and POST methods. 值得花一些时间来了解有关表单以及它们通过GET和POST方法与PHP交互的更多信息。 Good luck. 祝好运。

there is two way to call a php file from button using form submit and another is submit a form using ajax. 有两种方法可以使用表单提交从按钮调用php文件,另一种方法是使用ajax提交表单。

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

on submit button click form is submited to define by action. 在提交按钮上,单击表单将提交以按操作定义。

another way 其他方式

use simple button and on click of button using ajax call a php file without redirect 使用简单的按钮,然后单击按钮使用ajax调用php文件而无需重定向

Use a before the input fields or the values you want to send and add the php page to which you want to send the input field values in form action and in that php page write the insert query according to your needs. 在输入字段或要发送的值之前使用a并在表单操作中添加要将输入字段值发送到的php页面,然后在该php页面中根据需要编写插入查询。 eg: 例如:

I would use AJAX and JQuery . 我将使用AJAXJQuery

Handle the checked events of the checkbox's and when clicked update the value with an AJAX call: 处理复选框的选中事件,单击时使用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
   }
});

For more information I recommend you to give a look to the following references: 有关更多信息,我建议您看一下以下参考资料:

Hope this helped you :) 希望这对您有所帮助:)

NOTE: To use this method you obviously have to store your objects' id's so that you can retrieve them when you call the ajax function and pass them via POST or GET to the PHP page. 注意:要使用此方法,您显然必须存储对象的ID,以便在调用ajax函数时可以检索它们并将它们通过POST或GET传递到PHP页面。

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

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