Javascript进入无限循环

时间:2015-04-14 03:44:34

标签: javascript php infinite-loop

当我取消选中输入复选框时,share.php javascript会进入无限循环,打开新窗口。

所有3检查都没有问题。

在某些方面它出错了,但我找不到。 我不确定为什么会这样?

<form action="functions/share.php" method="POST" target="_blank">
    <p>
        <textarea class="form-control" style="resize: none;height: 90px;padding: 10px;border-radius: 3px;" name="summary" required>Build a fully customizable Personal Website in seconds - for free.</textarea>
    </p>

    <div class="text-center">
        <input type="checkbox" name="LinkedIn" value="yes" checked><i class="fa fa-linkedin"></i> 
        <input type="checkbox" name="FaceBook" value="yes" checked><i class="fa fa-facebook"></i>
        <input type="checkbox" name="Twitter" value="yes" checked><i class="fa fa-twitter"></i>
    </div>

    <p><input type="submit" class="btn btn-primary btn-lg btn-block" value="Share" ></p>
</form> 

share.php

<?php

    if(isset($_POST['summary'])){
        $linkedIn = "";
        $faceBook = "";
        $twitter = "";

        if($_POST['LinkedIn'] == "yes"){
            $linkedIn = "http://www.linkedin.com/";
        }

        if($_POST['FaceBook'] == "yes"){
            $faceBook = "http://www.facebook.com/";
        }

        if($_POST['Twitter'] == "yes"){
            $twitter = "http://www.twitter.com/";
        }

    }

?>
<html>
<body>
    <div id="linkedin" style="display: none;">
        <?php 
            echo htmlspecialchars($linkedIn); 
        ?>
    </div>
    <div id="facebook" style="display: none;">
        <?php 
            echo htmlspecialchars($faceBook); 
        ?>
    </div>
    <div id="twitter" style="display: none;">
        <?php 
            echo htmlspecialchars($twitter); 
        ?>
    </div>
    <script>

         var div = document.getElementById("linkedin");
         var linkedin= div.textContent;
         var div = document.getElementById("facebook");
         var faceBook= div.textContent;
         var div = document.getElementById("twitter");
         var twitter= div.textContent;

         if(linkedin != " "){

            window.open(linkedin);
         };
         if(faceBook != " "){

            window.open(faceBook);
         };
         if(twitter != " "){

            window.open(twitter);
         };

         close();

    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

首先,您必须更改

中的复选句子

if($_POST['LinkedIn'] == "yes")

要  if(isset($_POST['LinkedIn']))

其次,您必须删除JS代码close();

中的close语句

当您进行这两项更改时,选中的链接将会打开。如果有一个未选中的框,则会打开一个包含链接http://localhost/functions/share.php的窗口。要解决这个问题,请声明JS&#39; JS中的链接。代码。

您的最终代码:

<?php

    if(isset($_POST['summary'])){
        $linkedIn = "";
        $faceBook = "";
        $twitter = "";

        if(isset($_POST['LinkedIn']))
            $linkedIn = htmlspecialchars("http://www.linkedin.com/");
        else
            $linkedIn = "";

        if(isset($_POST['FaceBook']))
            $faceBook = htmlspecialchars("http://www.facebook.com/");
        else
            $faceBook = "";

        if(isset($_POST['Twitter']))
            $twitter = htmlspecialchars("http://www.twitter.com/");
        else
            $twitter = "";

    }

?>
<html>
<body>
    <script>

         var linkedin= "<?php echo $linkedIn; ?>";
         var faceBook= "<?php echo $faceBook; ?>";
         var twitter= "<?php echo $twitter; ?>";

         if(linkedin != "")
            window.open(linkedin);
         if(faceBook != "")
            window.open(faceBook);
         if(twitter != "")
            window.open(twitter);
         //close();

    </script>
</body>
</html>