[英]Why does my PHP not activate when I embed it with JavaScript?
I embedded a PHP file with JavaScript but the PHP didn't work. 我用JavaScript嵌入了一个PHP文件,但PHP没有用。 Here's what I did: 这是我做的:
$.get("php/file.php");
In the PHP file: 在PHP文件中:
<?php
echo "Testing PHP File"
?>
I would really appreciate it if someone answered me! 如果有人回答我,我真的很感激!
you should used $.get()
like this 你应该像这样使用$.get()
$.get('php/file.php', function(data) {
alert(data);//here is your "Testing PHP File" data
});
Make sure: this will work on any event like click
or other (i suppose you know about server side and client side) 确保:这适用于任何事件,如click
或其他(我想你知道服务器端和客户端)
$('.class_name').click(function() {
$.get('php/file.php', function(data) {
alert(data);
});
});
$.get("php/file.php");
works on client side and since you are not doing anything after the request, it will look like its not working. 在客户端工作,因为你在请求后没有做任何事情,它看起来不起作用。
Always consider reading the Docs . 始终考虑阅读文档 。
There its explained very nicely. 它解释得非常好。 Copying the code from there as an example. 从那里复制代码作为示例。
var jqxhr = $.get( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
You need a callback function to make this work. 您需要一个回调函数才能使其工作。 In this case, that can be the second paremeter 在这种情况下,这可以是第二个参数
$.get('php/file.php', function(data) {
// do someting with your data
});
I will suggest you to read the jQuery Docs or read some stuff, but for you the really helpful content on that page is examples section, in that first example is same as your question which is as below: 我建议你阅读jQuery文档或阅读一些内容,但对于你来说,该页面上真正有用的内容是示例部分,第一个示例与您的问题相同,如下所示:
$.get( "test.php" );
it will Request the test.php page(in your case php/file.php), but ignore the return results. 它将请求test.php页面(在您的情况下为php / file.php),但忽略返回结果。 If you want to alert the returned data then use function() in $.get() as below: 如果要提醒返回的数据,请使用$ .get()中的function(),如下所示:
$.get( "test.php", function( data ) {
alert( "Data Loaded: " + data );
});
You can also print it on html if you want , if you have any tag with id="output"
in your html here i am taking it for example you can have any jquery selector(class,element etc) then you can print it as below: 如果你愿意,也可以在html上打印它,如果你的html中有id="output"
任何标签我在这里拿它例如你可以有任何jquery选择器(类,元素等)然后你可以打印它下面:
$.get( "php/file.php", function( data ) {
$('#output').html(data);
});
//output: Testing PHP File
you can do this to catch your data outside the $.get callback
你可以这样做来捕获$.get callback
之外的数据
var data_;
$.get('php/file.php', function(data
{ data_ = data});
alert(data_);
you can instead call the php file using php include
instead of jquery $.get
你可以使用php include
而不是jquery $.get
来调用php文件
<script>
<?php include 'php/file.php';?>
</script>
In the above example, whatever you echo inside the <script>
tag must be a valid JavaScript syntax. 在上面的示例中,无论您在<script>
标记内回显什么,都必须是有效的 JavaScript语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.