简体   繁体   English

按钮在单击调用jQuery函数和访问PHP

[英]Button On click call jquery function and acess php

I am very new to Wordpress and Woocommerce. 我对Wordpress和Woocommerce非常陌生。 I have few doubts wrt jquery in Wordpress. 我对Wordpress中的jquery毫无疑问。 Say i have a function 说我有功能

function test(){
      alert("test");
      <?php
      error_log("Test ---------------------------- ", 0);
      ?>
  }

and a button: 和一个按钮:

<input type="button" id="btnclick" onclick="test();" value="Test" />`

error log is printing on page load but not on click. 错误日志是在页面加载时打印的,而不是在单击时打印的。 But i want to execute code inside php block only when user clicks on button.Is there a way to achieve this ? 但是我只想在用户单击按钮时在php块内执行代码。有没有办法实现这一目标? Thanks in advance` 预先感谢`

jPO has already explained how to solve this in a good way, but I thought I should explain why this happens. jPO已经解释了如何很好地解决此问题,但是我认为我应该解释为什么会发生这种情况。

PHP is executed on the server. PHP在服务器上执行。 Once the page has been sent to the client, the PHP is no more. 将页面发送到客户端后,PHP不再可用。 JavaScript happends on the client, and can be executed as long as the user is viewing the webpage. JavaScript发生在客户端上,并且只要用户正在查看网页就可以执行。 Since they do not live during the same timeperiod they are not aware of each other and can not be mixed in that way. 由于他们不在同一时期生活,所以他们彼此之间不认识,也无法以这种方式混合在一起。

When you visit the page in your browser, the browser sends a request to the server. 当您在浏览器中访问该页面时,浏览器会将请求发送到服务器。 On the server the PHP interpreter goes through the code of the requested page, executing everything between <? 在服务器上,PHP解释器浏览请求页面的代码,执行<?之间的所有操作<? and ?> . ?> It does not understand what the other stuff around it is - it could be HTML, JS, plain text, anything, the PHP interpreter does not know and does not care. 它不了解周围的其他内容-它可能是HTML,JS,纯文本或其他内容,PHP解释器不知道也不在乎。 That is why it writes to the error log on page load. 这就是为什么它在页面加载时写入错误日志的原因。

When the PHP interpreter is done it has produced a document looking like this: PHP解释程序完成后,将产生一个如下所示的文档:

function test(){
      alert("test");
  }

That is sent to the client, and the JS (without any instruction to write to the error log) is run on the client when the button is pushed. 它被发送到客户端,并且当按下按钮时,JS(没有任何写入错误日志的指令)在客户端上运行。

Not possible like that. 不可能那样。 If you'd like to do so. 如果您愿意的话。 You need something like ajax method in php which you can call. 您需要可以在php中使用类似ajax方法的东西。 Let's say you have a file in the root of your project called ajax.php, there you can define a function named test(), then you have to have a $_REQUEST translator, which calls your function test(), so the ajax.php would look like this 假设您在项目的根目录中有一个名为ajax.php的文件,可以在其中定义一个名为test()的函数,然后必须有一个$ _REQUEST转换器,该转换器会调用函数test(),因此就是ajax。 PHP将看起来像这样

<?php
  // checks if you sent a parameter named method and calls the method
  // if you provide parameter named params it will send them too
  if(isset($_REQUEST)){
    if(isset($_REQUEST["params"]))
      ajax($_REQUEST["method"],$_REQUEST["params"]);
    else
      ajax($_REQUEST["method"]);
  }

  function ajax($function,$data = null){
    $function($data);
  }
  function test(){
    error_log("Test ---------------------------- ",0);
  }

and your ajax would look like this 你的ajax看起来像这样

function test(){
  $.ajax({
    url:"ajax.php",
    data:{
      method:"test"
    }
  });
}

hope it helps 希望能帮助到你

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

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