-1

我在尝试从 PHP 激活 jQuery 函数时遇到问题。

以下是我自己的测试版。

index.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">

        $(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 文件中调用我的 jQuery 函数

<?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>"; 
};

?>

非常感谢任何知道该怎么做的人。

我现在已经尝试...

<?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>

PHP索引文件是...

<?php


if( $_REQUEST["activate"])
{

  $activate = $_REQUEST['activate'];

};  




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

?>
4

2 回答 2

1

我完全不明白你为什么要使用 PHP 来执行一个 jQuery 函数——也许这只是一个过于简单的例子。假设您的主要目标是让 PHP 响应触发特定的 Javascript 函数(并且可能允许 PHP 响应触发多个不同的响应),您将执行以下操作:

首先将回调从

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

类似于:

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
  }
}

其次...只需让您的 PHP 页面返回“colorchange”而不是 HTML 片段:

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

这有帮助吗?

于 2013-09-22T18:57:52.593 回答
0

you could try this-- example call the jquery function on success

  $.ajax({
     url: //the url,
     data: //the data,
     type: "POST",
     success: function(data){

         if(data == "ok"){
        colorChange();
        }
     }
   });

here the data will be the success result returned by the php code..

you can call a function depending on the data returned

于 2014-01-04T12:50:08.273 回答