如何在Javascript中触发php代码,如果条件 [英] How to trigger php code, in Javascript if condition

查看:71
本文介绍了如何在Javascript中触发php代码,如果条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我试图做的是触发PHP代码,只有当if条件为真(javascript)时,我明白PHP是服务器端,JavaScript是客户端。 PHP代码的包含工作除了1件事情以外完美,它实际上在页面加载时触发,而不是在if条件发生时触发。如果你能帮助我如何做到这一点将受到赞赏。我希望只有当if条件为真时才包含php文件。



感谢您提前感谢。

这里是正在使用的代码:

 < html> 
< script src =// cdnjs.cloudflare.com/ajax/libs/visibility.js/0.5/visibility.min.js\"> ;</script>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.jstype =text / javascript>< / script>
< script>
var seconds = 20;
var flag = false;

$ b $(document).ready(function(){
if(!flag)
Visibility.every(1000,tick);



函数tick()
{
display();

if(seconds> 0)
{
秒 - ;
}
else
{
if(!flag){
document.getElementById('more')。innerHTML + =< ;? php include_once('Code.php');
?>;
document.getElementById('more')。style.visibility ='visible';
flag = true;



函数display()
{
$(#timer)。html(seconds);
}
});
< / script>
< div id =TrafficHeaderstyle =height:100px; background-color:gray; padding:20px;>
< div id =timer>< / div>
< div id =morestyle =visibility:hidden;>< a href =http://www.double-it.com/Traffic/index.php>查看下一个广告< / A>< / DIV>< / DIV>
< iframe id =myframesrc =<?php echo''。$ URL;?> height =100%width =100%frameborder =0>
< / iframe>
< / body>
< / html>


解决方案

这是不可能的,至少不是您正在做这件事情。

原因是服务器完成评估你的之后,在客户机上计算 if > php 脚本。

第一台服务器必须完成您的php脚本的处理,然后想象它必须打包它,然后将整个软件包发送到客户端,然后解包并呈现html并在html中评估javascript,或者要求像图片,css或其他脚本文件这样的外部文件。



如果您尝试添加一些额外的javascript,则可以添加以编程方式在头部添加脚本标记。如果您想在服务器上执行某些操作,您可以轻松地在服务器上调用脚本。



以下是如何从JavaScript执行脚本如果为真:

  ... 
if(!flag){
xmlHttp = new XMLHttpRequest( );
xmlHttp.open(GET,http://link/to/your/code.php ?, = special& parameters = sent,false);
xmlHttp.send(null);

document.getElementById('more')。innerHTML + = xmlHttp.responseText;
document.getElementById('more')。style.visibility ='visible';
flag = true;
}
...


Ok , so what I'm trying to do is trigger PHP code, only when the if condition is true (in javascript) , I understand that php is server side, and javascript is client side. The include of the php code works perfect except 1 thing , it gets triggered on page load actually , not when the if condition happens. If you can help me how to do this will be rly appreciated. I want the php file to be included ONLY when the if condition is true

thanks in advance

here's the code am using :

<html>
    <script src="//cdnjs.cloudflare.com/ajax/libs/visibility.js/0.5/visibility.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
var seconds=20;
var flag=false;


$(document).ready(function(){
if (!flag)
Visibility.every(1000, tick);



function tick()
{
      display();

        if (seconds>0)
        {
         seconds--;
        }
        else
        {
          if (!flag){
          document.getElementById('more').innerHTML +="<?php include_once('Code.php'); 
          ?>";
          document.getElementById('more').style.visibility='visible';
          flag=true;
          }
        }
}
function display()
{
   $("#timer").html(seconds);
}
});
</script>
<div id="TrafficHeader" style="height:100px; background-color:grey; padding:20px;">
<div id="timer"></div>
<div id="more" style="visibility: hidden;"><a href="http://www.double-it.com/Traffic/index.php">View Next Ad</a></div></div>
<iframe id="myframe" src="<?php echo ''.$URL;?>" height="100%" width="100%" frameborder="0">
</iframe>
</body>
</html>

解决方案

This is not possible, at least not the way you are going about it.
The reason is that the if condition is evaluated on the client machine long after the server finish evaluating your php script.

First server has to finish procesing your php script, then imagine it has to pack it and then send the whole package to the client, which then unpacks it and renders the html and evaluates the javascript in html, or asks for external files like images, css or other script files.

If you are trying to include some extra javascript ,than you can add extra script tag in the head programatically. If you want to do something on the server, you can call a script on a server just as easily.

Here is how you can exeecute that script from javascript only if the if is true:

...
if (!flag){
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", "http://link/to/your/code.php?and=special&parameters=sent", false );
    xmlHttp.send( null );

    document.getElementById('more').innerHTML += xmlHttp.responseText;
    document.getElementById('more').style.visibility='visible';
    flag=true;
    }
...

这篇关于如何在Javascript中触发php代码,如果条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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