简体   繁体   English

在两个JavaScript函数之间切换

[英]Switch between two javascript functions

basically I want php to decide which version of the site pieces to load something along the lines of: 基本上,我希望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 
    }?> 

issue is that they both run so even if I set the condition to true I still get the second. 问题是它们都运行,所以即使我将条件设置为true,我仍然会得到第二个。

The complete source which is identical to what is being viewed-as-source as far as I can tell but none of the php shows up in the site preview (it is actually there though because my other computer can view the site without seeing the php but when it views source it can see it which it shouldn't be able to): 据我所知,完整的源代码与正在查看的源代码完全相同,但是站点预览中没有显示任何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>

I do not believe you get both if your PHP is actually parsed. 我不相信如果您的PHP实际被解析,您不会两者兼得。 If it is not parsed, you will see the code you pasted exactly as you pasted it if you look at the source of the web page 如果未解析,则查看网页的源代码后,您将看到与粘贴的代码完全相同的代码

Make sure the php is parsed - if not, then that is the issue. 确保php被解析-如果没有,那就是问题所在。

A script tag will generally not interfere with parsing unless you have to post processing that does not allow the tags - for example if you use some kind of framework where you are not supposed to enter php but instead some tokenised code 除非您必须发布不允许标签的处理,否则脚本标签通常不会干扰解析-例如,如果您使用某种框架,而该框架不应该输入php而是一些标记化的代码

Here is a better example - note I moved the script to the head but did not optimise really - so the php can be more elegant if you get both values in one call and create an object array 这是一个更好的示例-请注意,我将脚本移到了头部,但并没有真正进行优化-因此,如果您在一次调用中获得两个值并创建一个对象数组,则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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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