简体   繁体   English

当我使用JavaScript嵌入时,为什么我的PHP没有激活?

[英]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
});

more on $.get 更多关于$ .get

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.

相关问题 为什么onClick呈现时会激活? - Why does my onClick activate when it is rendering? 当我添加if时,为什么我的javascript不做任何事情? - Why does my javascript not do anything when i added my if? 我是否可以将javascript内嵌的php用于我的移动网站? - Can I use a php embed inside a javascript for my mobile website? 为什么删除回车后我的 javascript 不起作用? - Why does my javascript not work when I remove carriage returns? 为什么此javascript函数在错误的时间激活? - Why does this javascript function activate at the wrong time? 单击链接PHP时激活Javascript - Activate Javascript when click link PHP 在安装了自定义模块的情况下激活开发人员模式时,为什么会出现白屏? - Why am I getting an white screen when I activate the developer mode with my custom module installed? 当我使用“和”或“或”时,为什么我的替换在javascript中正常工作,但在我使用“&”或“/”或“+”时却没有 - Why does my replace work properly in javascript when I use “and” or “or” but not when I use “&” or “/” or “+” 为什么我的输入在浏览器中运行时没有更新? - Javascript - Why does my input not update when I run it in my browser? - Javascript 为什么我的正则表达式可以在RegexPal中工作,但在运行Javascript时却不能工作? - Why does my regular expression work in RegexPal, but not when I run my Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM