du77887 2013-09-22 18:44
浏览 101
已采纳

从PHP文件调用jQuery函数

I am having problems trying to activate a jQuery function from a PHP.

The following is my own test version.

index.php file

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Welcome to temp index</title>

    <link rel="stylesheet" href="style.css" type="text/css" />
    <script type="text/javascript" src="scripts/jquery-1.8.3.min[2].js"></script>
    <script type="text/javascript">

        $(document).ready(function() {

            //posts info into the userinfo.php file
            $.post('userinfo.php', { activate:"colourchange"}, function(data){
                $('report').html(data);
            });

            //the function which is meant to be activated from the php file
            function colourchange(){
                document.body.style.backgroundColor = 'green';   
             };         

         }); 

    </script>

</head>

<body>
  <h1>hello</h1>

<div id="report">
</div>

</body>

</html>

PHP file I am attempting to call my jQuery function from

<?php

if( $_REQUEST["activate"])
{
  $activate = $_REQUEST['activate'];
};  

if($activate == 'colourchange') 
{
 // This is the code that I believe not to be working as this isn't 
 // activating the jQuery function to work. (The page background isn't changing colour)

    echo "<script>colourchange(); </script>"; 
};

?>

Thank you to anyone who has an idea of what to do it is much appreciated.

I have now tried ...

<?php
require 'userinfo.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Welcome to temp index</title>

<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.8.3.min[2].js"></script>
<script type="text/javascript">

 function colourchange(){
    document.body.style.backgroundColor = 'green';   
  };



  $(document).ready(function() {
    $.post('userinfo.php', { activate:"colourchange"}, function(data){
      $('report').html(data);
    });    
 }); 


</script>

</head>

<body>
  <h1>hello</h1>

<div id="report">
  </div>



</body>

</html>

and the PHP index file is ...

<?php


if( $_REQUEST["activate"])
{

  $activate = $_REQUEST['activate'];

};  




  if($activate=='colourchange')
  { 
    echo "colourchange();";
  };

?>
  • 写回答

2条回答 默认 最新

  • duanli5662 2013-09-22 18:57
    关注

    I don't exactly understand why you are using PHP to execute a jQuery function at all - perhaps this is just an oversimplified example. Assuming your primary goal is to have the PHP response trigger a specific Javascript function (and perhaps allow for the PHP response to trigger several different responses) you'd do something like this:

    First change the callback from

    function(data){
      $('report').html(data);
    }
    

    to something like:

    function(data) {
      if (data.indexOf("colourchange") >= 0) {  // php page returned "colorchange" or a string containing "colourchange" 
         colourchange();
      } else if (data.indexOf("moodchange") >= 0) {
         moodchange();  /// etc... you can add more trigger functions here
      }
    }
    

    And second... just have your PHP page return "colourchange" instead of an HTML snippet:

    if($activate == 'colourchange') 
    {
      echo "colourchange"; 
    };
    

    Does that help?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波