简体   繁体   English

在HTML onClick()事件上调用PHP函数?

[英]Calling PHP function on HTML onClick() event?

I know there is one answer out on stackoverflow, but that is related to form submission. 我知道有一个关于stackoverflow的答案,但这与表单提交有关。

Mine is different implementation. 我的是不同的实现。 I need to call a function, which registers a variable in $_SESSION[], and then redirects to another page(html/php). 我需要调用一个函数,该函数在$ _SESSION []中注册一个变量,然后重定向到另一个页面(html / php)。 Now I don't want to use AJAX. 现在,我不想使用AJAX。 Please help me with this. 请帮我解决一下这个。

That is not possible. 这是不可能的。 Php is executed on the server before the html is served. 在投放html之前,在服务器上执行php。 All php functions have been evaluated to HTML at the time onClick occurs. 在发生onClick时,所有php函数均已评估为HTML。

You can do a form and set action="someFile.php" and use $_GET['someVaiable'] or $_POST['someVariable'] 您可以执行一个表单并设置action =“ someFile.php”并使用$ _GET ['someVaiable']或$ _POST ['someVariable']

Use a form with a submit button. 使用带有提交按钮的表单。

Store whatever you need to pass in hidden inputs (or use non-hidden inputs if it is supposed to be user configurable). 存储需要传递给隐藏输入的所有内容(或者,如果应该由用户配置,则使用非隐藏输入)。

When the form is submitted, sanity check the data, set any session data you want, then redirect. 提交表单后,请仔细检查数据,设置所需的任何会话数据,然后重定向。

You don't need to use a JavaScript click event. 您不需要使用JavaScript click事件。 The default behaviour of a submit button will handle everything you've asked for (that the client could contribute). 提交按钮的默认行为将处理您要求的所有内容(客户端可以提供)。

If you are passing this with javascript to the php you could try 如果您通过JavaScript将其传递给php,则可以尝试

parameters.something = x;
parameters.somethingelse = y;
parameters.somethingelse1 = z;
$.post("class_handler", parameters)

and then your PHP needs to have a class_handler that will react when POST occurs. 然后您的PHP需要有一个class_handler,它将在POST发生时作出反应。

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

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