javascript 包含文件 onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11778928/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:19:05  来源:igfitidea点击:

javascript include file at onclick

phpjavascriptincludeonclick

提问by Jam Ville

Is there a way to include some php file at javascript onClick?

有没有办法在 javascript 中包含一些 php 文件onClick

like,

喜欢,

if(isset(something)) ?
include "file.php";

Please help!

请帮忙!

回答by sachleen

No. That's PHP syntax. PHP executes completely on the server and the result is sent back to the client. Clicks are on the client side. Javascript handles those. Your can't mix the two.

不,那是 PHP 语法。PHP 完全在服务器上执行,并将结果发送回客户端。点击是在客户端。Javascript 处理这些。你不能把两者混为一谈。

You can use AJAX to make a request to the server for a file and display its contents using JS, though. jQuery has a simple method called .ajax()for this

不过,您可以使用 AJAX 向服务器请求文件并使用 JS 显示其内容。jQuery 有一个简单的方法来调用.ajax()这个

$.ajax({
? url: 'file.php',
? success: function(data) {
? ? $('#result').html(data);
? ? alert('Load was performed.');
? }
});

This method will load the contents of file.phpand show that in an element with the id result.

此方法将加载file.phpid的内容并将其显示在一个元素中result

Another option is to submit a form. You put the name of the file in a hidden form field and onclick, submit the form. PHP gets that information and you read the name of the file from $_GET[]or $_POST[], make sure it's valid (wouldn't want somebody to be able to modify your HTML and include any file on your server) and then include it and render the page again.

另一种选择是提交表格。您将文件名放在隐藏的表单字段中,然后单击提交表单。PHP 获取该信息,然后您从$_GET[]或读取文件名$_POST[],确保它有效(不希望有人能够修改您的 HTML 并在您的服务器上包含任何文件),然后包含它并再次呈现页面。

回答by Deep Frozen

You can simply use jQuery's loadfunction:

您可以简单地使用 jQuery 的load功能:

$("#elem").click(function()
{
    $("#placeToLoad").load("http://path/to/file");
}

@sachleen his way is the same as this, only loadjust grabs the file and puts it in the given location, instead of having to cope with success functions etc. If loaddoesn't succeed, it won't do anything.

@sachleen他的方式和这个一样,load只是抓取文件放在给定的位置,而不用处理成功函数等。如果load不成功,它不会做任何事情。

jQuery .load()

jQuery .load()

回答by Kumar Ajay A.K

When you make a request to a file using the jQuery load() method, it sends data just like any regular request through your browser. It has no knowledge of any of your existing PHP variables unless you pass them along with your ajax request as data.

当您使用 jQuery load() 方法向文件发出请求时,它会像通过浏览器发送任何常规请求一样发送数据。它不知道您现有的任何 PHP 变量,除非您将它们作为数据与 ajax 请求一起传递。

function sendAJAX(){
$('#container').load(
"stuff.php",  // url
{ // json object of data to pass to the page
    stuff: "all your stuff goes in here...",
    moreStuff:  "even more stuff"
});
console.log('sendAJAX'); 

};

};

回答by Marcio

You can't include php file because it's server side code which can't be executed in client side.

您不能包含 php 文件,因为它是无法在客户端执行的服务器端代码。

But what you could actualy do is use ajax to call the file.php and execute its content and than append the result to your document.

但是您实际上可以做的是使用 ajax 调用 file.php 并执行其内容,然后将结果附加到您的文档中。