Javascript 在 jQuery 单击中运行 php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6116931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run php function inside jQuery click
提问by Pinkie
How do i run a PHP function inside jQuery click event. I have the following which is not correct. when the user clicks on a button, i want a new directly created.
我如何在 jQuery 单击事件中运行 PHP 函数。我有以下不正确的。当用户点击一个按钮时,我想要一个新的直接创建。
$('button').click(function(){
<?php mkdir('/test1/test2', 0777, true); ?>
return false;
})
回答by Cargowire
You cannot run PHP inside a jquery function. PHP is run on the server side whereas jquery/javascript is run on the client side. However you can request a php page using jquery and with the PHP code in that page run the mkdir that you want.
您不能在 jquery 函数中运行 PHP。PHP 在服务器端运行,而 jquery/javascript 在客户端运行。但是,您可以使用 jquery 请求一个 php 页面,并使用该页面中的 PHP 代码运行您想要的 mkdir。
JS:
JS:
$.ajax({
url: 'test.php',
success: function(data) {
alert('Directory created');
}
});
test.php FILE:
test.php 文件:
<?php mkdir('/test1/test2', 0777, true); ?>
回答by Flatlin3
First of all you should understand how php works (no offense but this is essential). Why PHP script is not workig in a web browser?To accomplish what you need you have to use ajax (to request a script on the server using javascript)
首先,您应该了解 php 的工作原理(无意冒犯,但这是必不可少的)。为什么 PHP 脚本在 Web 浏览器中不起作用?要完成您需要的操作,您必须使用 ajax(使用 javascript 在服务器上请求脚本)
PHP File (createdir.php):
PHP文件(createdir.php):
<?php
mkdir('/test1/test2', 0777, true);
?>
JavaScript Code:
JavaScript 代码:
$('button').click(function() {
$.ajax({
url: 'createdir.php',
success: function(){
alert('dir created');
}
});
return false;
});
I have not validated if the code acually works. If you encounter any problems you should have a look at the jquery documentation (it's awsome :-) ) http://api.jquery.com/jQuery.ajax/
我还没有验证代码是否有效。如果您遇到任何问题,您应该查看 jquery 文档(它太棒了 :-))http://api.jquery.com/jQuery.ajax/
回答by 2ndkauboy
Just do an ajax request and then execute the PHP server side:
只需做一个ajax请求,然后执行PHP服务器端:
$('button').click(function(){
$.ajax({url: 'mkdir.php'});
return false;
})
and the php:
和 php:
<?php mkdir('/test1/test2', 0777, true); ?>
That's all you need.
这就是你所需要的。
回答by Bas Slagter
You are mixing up client side and server side code here. The PHP code is already executed on the server when the user clicks the button and therefore nothing will happen. You can use the xmlhttprequest (or ajax) for this.
您在这里混淆了客户端和服务器端代码。当用户单击按钮时,PHP 代码已经在服务器上执行,因此什么都不会发生。您可以为此使用 xmlhttprequest(或 ajax)。
回答by Cheluis
Why not to call an ajax function on the server side? You could do something like :
为什么不在服务端调用ajax函数呢?你可以这样做:
$('button').click(function()
{
$.ajax
({
type: "POST",
url: "some.php",
data: "val1:value&lvaln:valn",
success: function(msg)
{
alert( "Data Saved: " + msg );
}
});
return false;
});
Documentation here
文档在这里
Try not to embed php code into html. If you can avoid this practice, the better.
尽量不要将 php 代码嵌入到 html 中。如果你能避免这种做法,那就更好了。
Hope it helps
希望能帮助到你
回答by g.d.d.c
JavaScript - Client Side.
JavaScript - 客户端。
PHP - Server Side.
PHP - 服务器端。
You need to issue an AJAX call of some kind in order to communicate with the Server. There's no way for JavaScript to create directories on your Remote Server (this would be a huge security hole otherwise).
您需要发出某种 AJAX 调用才能与服务器通信。JavaScript 无法在您的远程服务器上创建目录(否则这将是一个巨大的安全漏洞)。
回答by Racooon
you have to do:
你必须要做:
//mkdir.php
<?php mkdir('/test1/test2', 0777, true); ?>
<script>
$('button').click(function(){ $('#hidden').load('mkdir.php'); return false; })
</script>
<div id='hidden' style='display:none;'></div>
回答by romaninsh
You can greatly benefit from a framework supporting AJAX.
您可以从支持 AJAX 的框架中受益匪浅。
As author of Agile Toolkit I can recommend you to try it, the code would look like this:
作为 Agile Toolkit 的作者,我建议您尝试一下,代码如下所示:
$button=$page->add('Button');
if($button->setLabel('Create Directory')->isClicked()){
if(mkdir('/tmp/123', 0777, true)){
$button->js()->univ()->alert('Directory Created'); // executes JS code
}else{
$button->js()->univ()->alert('Problem'); // executes JS code
}
}
Other frameworks may offer other ways to simplify AJAX and keep it in one file.
其他框架可能会提供其他方法来简化 AJAX 并将其保存在一个文件中。
回答by Jijo John
Buddy , first of all understand that jquery is a client side programming language which is running on clients browse and Php runs on webserver .Copy the php code in to a php file and create a request to the php file from jquery using
伙计,首先要了解 jquery 是一种客户端编程语言,它运行在客户端浏览器上,而 PHP 运行在 web 服务器上。将 php 代码复制到 php 文件中,然后使用 jquery 创建对 php 文件的请求
$.ajax();