在两个javascript函数之间切换

时间:2012-03-27 07:16:44

标签: php javascript

基本上我想让php决定加载哪个版本的网站 类似的东西:

    <?php 

    $sql="SELECT _1 FROM Player_Registry WHERE Player_Name = $_SESSION[user_name]"; 
    $result_1=mysql_query($sql); 
    if($result_1 < 10) { ?> 
      <script type="text/javascript"> 
        function setdescription_1() { 
          document.getElementById('title_box').innerHTML = 'Alchemist'; 
        } 
      </script> 
    <?php 
    } 
    else { 
    ?> 
      <script type="text/javascript"> 
        function setdescription_1() { 
          document.getElementById('title_box').innerHTML = 'Master Alchemist'; 
        } 
      </script> 
    <?php 
    }?> 

问题是,即使我将条件设置为真,它们都会运行,我仍然得到第二个。

完整的源代码与我所能看到的内容完全相同但是没有php出现在网站预览中(它实际上是因为我的其他计算机可以查看没有看到php,但是当它查看源代码时,它可以看到它应该无法执行的操作):     

<head>

<title>Blank</title>

<link rel="icon" type="image/png" href="Pictures/favicon.png">

<meta name="description" content="" />

<meta name="keywords" content="" />

<meta name="author" content="" />

<meta name="owner" content="" />

<meta name="copyright" content="" />

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<link rel="stylesheet" href="TreeStyle.css" type="text/css" />
</head>

<body>


<div style="width:100%; height:12.5%;">
    <p style="font-size: 300%;"><b>Skill Tree: Alchemist</b></p>
</div>
<div style="width:100%; height:75%;">
<?php
$sql="SELECT _1 FROM Player_Registry WHERE Player_Name = $_SESSION[user_name]";
$result_1=mysql_query($sql);
if($result_1 < 10)
    {
    ?>
    <script type="text/javascript">
    function setdescription_1()
        {
        document.getElementById('title_box').innerHTML = 'Alchemist';
        document.getElementById('desc_box').innerHTML = 'Turn surroundings into base element costs.';
        }
    </script>
    <?php
    }
else
    {
    ?>
    <script type="text/javascript">
    function setdescription_1()
        {
        document.getElementById('title_box').innerHTML = 'Master Alchemist';
        document.getElementById('desc_box').innerHTML = 'Equiped Philosopher&#39;s Stone allows bypass of element costs.';
        }
    </script>
    <?php
    }
$sql="SELECT _2 FROM Player_Registry WHERE Player_Name = $_SESSION[user_name]";
$result_2=mysql_query($sql);
if($result_2 < 10)
    {
    ?>
    <script type="text/javascript">
    function setdescription_2()
        {
        document.getElementById('title_box').innerHTML = 'Learn Runes';
        document.getElementById('desc_box').innerHTML = 'Create Runes and Cores up to Lv.<?php echo "$result_2" ?>';
        }
    </script>
    <?php
    }
else
    {
    ?>
    <script type="text/javascript">
    function setdescription_2()
        {
        document.getElementById('title_box').innerHTML = 'Comprehension';
        document.getElementById('desc_box').innerHTML = 'Create Runes and Cores up to Lv.10';
        }
    </script>
    <?php
    }
?>
    <script type="text/javascript">
    function cleardescription()
        {
        document.getElementById('title_box').innerHTML = 'Skill Name';
        document.getElementById('desc_box').innerHTML = 'This is the skill description.';
        }
    </script>
    <table>
        <tr>
            <td></td>
            <td><a href='AddSkillPoints.php?skill=_1' onmouseover="setdescription_1()" onmouseout="cleardescription()"><img src="Alchemist.png" /></a></td>
            <td><img src="Blank_Tile.png"/></td>
            <td><a href='AddSkillPoints.php?skill=_2' onmouseover="setdescription_2()" onmouseout="cleardescription()"><img src="Learn_Runes.png"/></td>

etc..............

    </table>
    </div>
    <div style="width:100%; height:12.5%;">
        <b><p id="title_box" style="font-size: 150%;">Skill Name</p></b><br />
        <p id="desc_box">This is the skill description.</p>
    </div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果您的PHP实际被解析,我不相信你会得到两者。 如果没有解析,如果您查看网页的来源,您将看到粘贴的代码与粘贴时完全相同

确保解析php - 如果没有,那就是问题。

脚本标记通常不会干扰解析,除非您必须发布不允许标记的处理 - 例如,如果您使用某种框架,您不应该输入php,而是使用某些标记化代码

这是一个更好的例子 - 注意我将脚本移动到头部但没有真正优化 - 所以如果你在一次调用中获得两个值并创建一个对象数组,那么php会更优雅

<?PHP
    $sql="SELECT _1 FROM Player_Registry WHERE Player_Name = $_SESSION[user_name]";
    $result_1=mysql_query($sql);
    $sql="SELECT _2 FROM Player_Registry WHERE Player_Name = $_SESSION[user_name]";
    $result_2=mysql_query($sql);
%><html>
    <head>
    <title>Blank</title>
    <link rel="icon" type="image/png" href="Pictures/favicon.png">
.
.
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <link rel="stylesheet" href="TreeStyle.css" type="text/css" />
    <script type="text/javascript">
    var result1 = parseInt("<?PHP echo $result_1; ">,10);
    var skillLevel = (result1>=10?'Master ':'')+'Alchemist';
    var skillDesc  = result1>=10?"Equipped with Philosopher´s Stone allows bypass of element costs.":"Turn surroundings into base element costs.";

    var runeLevel  = result2>=10?'Comprehension':'Learn Runes';
    var runeDesc   = 'Create Runes and Cores up to Lv.'+result2; 

    window.onload=function() {
      setdescription_1();
    }
    function setdescription_1() {
          document.getElementById('title_box').innerHTML = skillLevel;
          document.getElementById('desc_box').innerHTML =  skillDesc; 
    }         
    function setdescription_2() {
          document.getElementById('title_box').innerHTML = runeLevel;
          document.getElementById('desc_box').innerHTML =  runeDesc; 
        }
    function cleardescription() {
            document.getElementById('title_box').innerHTML = 'Skill Name';
            document.getElementById('desc_box').innerHTML = 'This is the skill description.';
    }
        </script>



    </head>