javascript包含onclick文件 [英] javascript include file at onclick

查看:117
本文介绍了javascript包含onclick文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

喜欢,

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

请帮助!

推荐答案

没有。这是PHP语法。 PHP在服务器上完全执行,结果将发送回客户端。点击是在客户端。 Javascript处理这些。你不能把它们混合在一起。

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.

你可以使用AJAX向服务器发出文件请求并使用JS显示其内容。 jQuery有一个名为 .ajax() <的简单方法/ a>为此

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({
  url: 'file.php',
  success: function(data) {
    $('#result').html(data);
    alert('Load was performed.');
  }
});

此方法将加载 file.php 并在id result 的元素中显示。

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

另一种选择是提交表单。您将文件的名称放在隐藏的表单字段中并单击,然后提交表单。 PHP获取该信息,您从 $ _ GET [] $ _ POST [] 中读取文件的名称,make确定它是有效的(不希望某人能够修改你的HTML并在你的服务器上包含任何文件),然后包含它并再次渲染页面。

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.

这篇关于javascript包含onclick文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆