简体   繁体   中英

Why is my PHP code not working?

I have made a code --

<?php

header("Content-type: text/javascript");
require_once('../php/config.php');
$domail_theme = $_GET['theme'];

?>

<?php
  if($domail_theme=='default')
  {
?>

  $('div.theme_content').mouseover(function () {
    $(this).stop().animate({
      backgroundColor : '#234'
    }, 250, 'linear', function() { });
  });

  $('div.').mouseout(function () {
    $(this).animate({
      backgroundColor : '#ddd'
    }, 400, 'linear', function() { });
  });

<?php
  }
?>

And here is the script including tag --

<script src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>

The thing I want is that when I mouse over on the div element with class theme_content it's background changes according to the given one in the animate function. It is working for the input element but that script is original javascript and in this I have included php that's why I'm thinking whether the php code I used is wrong or my javascript . Also when I include this code in javascript file it works correctly. And by the I'm including the script tag in between the body tag, is it wrong? Please help me out with this one.

Thanks in advance!

Try to put it inside script. <script></script>

And also it's possible that DOM might not have been loaded. so try putting wrappint in $(document).ready(function() { /*Your code here*/})

And also put alert('hello'); to check if that code is being executed or not. It might be useful in debugging.

The most likly explanation is that 'theme" is not set to 'default' in your URL request.

This should be easy to check. Also you can "View" -> "Page Source" in your browser and look at the html your php program is actually generating. If my guess is right you won't see your mouseover function in the page.

Hi you should write like this:-

<?php
 if($domail_theme=='default')
{
?>
<script>

$('div.theme_content').mouseover(function () {
$(this).stop().animate({
  backgroundColor : '#234'
}, 250, 'linear', function() { });
});

$('div.').mouseout(function () {
$(this).animate({
  backgroundColor : '#ddd'
}, 400, 'linear', function() { });
});
</script>
<?php
}
?>

Just need add tag between on your php if statement.

Hope can help you.

如果将type ='text / javascript'添加到脚本标签中,效果会更好吗?

<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.php?theme=default"></script>

Why not just put the php logic in your main file and include the javascript as plain javascript?

<?php
 if($domail_theme=='default')
{
?>
<script type='text/javascript' src="domail_res/scripts/java/settings_form_css.js"></script>
<?php
}
?>

Seems like overcomplicating things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM