weixin_33737774 2014-07-13 15:43 采纳率: 0%
浏览 15

PHP和Javascript事件处理

I know that php is a server-side language. However I am confused on how to hook up a html button event to a page that uses php. I understand this will probably involve Javascript, some css and html and maybe Ajax. I've been looking at some examples but they don't explain the important pieces I am looking for. In summary I want to see the code for the click event and how php gets notified there was a click.

I apologize if this is a strange question. I'm used to old application programming and I'm trying to understand some of these newer practices.

  • 写回答

2条回答 默认 最新

  • weixin_33671935 2014-07-13 15:47
    关注

    The actual click event will never be sent to PHP. The outcome of the click is what the server (PHP) will see.

    For example, if your button redirects the user to a different page, your PHP will be "notified" as soon as the GET request reaches your sever.

    For a form element, the submit button will issue a POST request to a certain URL, that request is what you can "interpret" as the click event. The same goes for an AJAX request.

    评论
  • weixin_33720078 2014-07-13 15:53
    关注

    This is a button in your form

    <form id="form1">
        <div class="col-md-8">
                              <button type="submit" class="submitbtn" id="submitbtn">Save</button>
                            </div>
    </form>
    

    This is your java-script, usually jquery or even angularjs is awesome to do this trick

    $("#form1").submit(function () { 
    
    
    $.ajax({
                    type: "POST",
                    cache: false,
                    data:  $("#form1").serializeArray(),
                    success: function (data) {
                        if (data.ok) {
                           alert('Awesomeness');
                        }
                        else {
                            alert('Crapness');
                        }
    
                    },
                    error: function () {
                        console.log("html error");
                         alert('Crapness');
                    }
                });
    

    })

    Then back in your php you just recieve the data like old school programming, with a post variable

    $something = $_POST['Forminput'];
    if(!empty($something)){
    
       //do something and return result
       echo "{\"ok\":\"true\", \"data\":\"some data for u\"}";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何利用闲置机械硬盘变现
  • ¥15 信号处理中的凸优化问题
  • ¥15 arm虚拟机无法和物理机互通
  • ¥15 Android导航条遮盖异常
  • ¥15 计算机网络技术基础问题
  • ¥15 设置mac系统只能访问指定网站
  • ¥15 西门子博途 s7 1200控制三台步进电机
  • ¥15 基于非参数的方向距离函数求污染物影子价格(有偿)
  • ¥15 vue+element 生成table
  • ¥15 实验 4 FIFO 算法和 LRU 算法-C 程序实现