【问题标题】:How to link the html button to php?如何将html按钮链接到php?
【发布时间】:2016-01-26 16:56:52
【问题描述】:

单击“开/关”按钮后,如何连接按钮(位于 HTML 页面中)以连接到 php url。一旦单击按钮,代码基本上必须调用 php,当调用 php 时,它需要将 ON/OFF 以及开始时间注册到我的数据库中。可以请任何人帮助我解决此代码,因为我是新手并且真的被卡住了。这个东西需要在没有 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>

【问题讨论】:

    标签: javascript php jquery html button


    【解决方案1】:

    我会使用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 页面时可以检索它们.

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        在提交按钮点击表单被提交以通过动作定义。

        另一种方式

        使用简单的按钮并点击使用 ajax 的按钮调用没有重定向的 php 文件

        【讨论】:

          【解决方案4】:

          我认为你必须分阶段解决这个问题。

          如果您尝试获取 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 函数访问输入中的值。通过 this 访问它们的开/关值

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

          但您需要更多的 PHP 编码练习才能更进一步。值得花一些时间来了解更多关于表单及其通过 GET 和 POST 方法与 PHP 交互的信息。祝你好运。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-02-24
            • 1970-01-01
            • 2016-02-16
            • 1970-01-01
            • 1970-01-01
            • 2011-09-05
            • 1970-01-01
            • 2013-12-11
            相关资源
            最近更新 更多