du77887
du77887
2013-09-22 18:44

从PHP文件调用jQuery函数

  • function
  • php
  • javascript
  • 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条回答

为你推荐

换一换