2012-08-15 100 views
0

我是新来的PHP和SVG。这里是我的代码:如何刷新PHP服务器

<?php 
    // Print a login-id 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 

    mysql_select_db("db") or die(mysql_error()); 
    { 
     $username = $_COOKIE['ID_my_site']; 

     $pass = $_COOKIE['Key_my_site']; 

     $check = mysql_query("SELECT * FROM users WHERE username = '$username'") 
      or die(mysql_error()); 

     while($info = mysql_fetch_array($check)) 
     { 
      $targetNo = "1"; 
      if(!empty($_GET)) { 
       $targetNo = $_GET["newT"]; 
      } 

      mysql_connect("localhost", "root", "") or die(mysql_error()); 
      mysql_select_db("db") or die(mysql_error()); 
      $data = mysql_query("SELECT * FROM alphabet where id >= $targetNo;") 
       or die(mysql_error()); 
?> 

<aside style="font-size:larger;font-style:italic;color:green;float:left;width:100px;"> 
    <div style="background:pink;height:710px"> 
     Username: <?php print $username ?> 
    <br><br><br><br> 
    Score: <?php print $targetNo-1 ?> 
</aside> 

<aside style="float:left;width:1000px;"> 
    <div style="background:lightblue;height:710px"> 
     <svg onload='Init(evt)'> 
      <title>Fish Game</title> 

      <SCRIPT LANGUAGE='JavaScript'> 
       var SVGDocument = null; 
       var SVGRoot = null; 

       function Init(evt) { 
        SVGDocument = evt.target.ownerDocument; 
        SVGRoot = SVGDocument.documentElement; 
       } 

       function ToggleOpacity(evt, targetId, targNo) { 
        if(targetId == parseInt(targNo)) { 
         newTarget = SVGDocument.getElementById(targetId); 
         newTarget.setAttributeNS(null, 'opacity', '0'); 

         targetId++; 
         document.location="AtoZ.php?newT=" + targetId; 
        } 
       } 
      </script> 

      <?php 
       // status 
       $a= mysql_query("SELECT * FROM alphabet where id = $targetNo;") 
       or die(mysql_error()); 
       $targetAlphabet = mysql_fetch_array($a); 

       $target = $targetAlphabet['Alphabet']; 
      ?> 

      // status 
      <text x="10" 
        y="20" 
        style="font-family:Times,serif;fill:#B40404;font-size:20px" 
        >Please find the letter: <?php print $target ?></text> 

      //Fish 
      <?php 
       static $info; 
       while ($info = mysql_fetch_array($data)) { 
        $id = $info['ID']; 
        $al = $info['Alphabet']; 
      ?> 
        //fish body 
        <g id='<?php print $id; ?>' onclick='ToggleOpacity(evt, id,"<?php print $targetNo ?>")'> 
         <circle cx="<?php print $info['body_cx']; ?>"cy="<?php print $info['body_cy']; ?>" r="<?php print $info['body_r']; ?>" stroke="black" stroke-width="1" fill="<?php print $info['body_fill']; ?>" /> 

         //tail 
         <path d="<?php print $info['tail']; ?>" fill="<?php print $info['tail_fill']; ?>" stroke="<?php print $info['tail_stroke']; ?>" stroke-width="1" /> 

         //eye 
         <circle cx="<?php print $info['eye_cx']; ?>" cy="<?php print $info['eye_cy']; ?>" r="<?php print $info['eye_r']; ?>" stroke="black" stroke-width="1" fill="<?php print $info['eye_fill' ];?>" /> 
         <circle cx="<?php print $info['pupil_cx']; ?>" cy="<?php print $info['pupil_cy']; ?>" r="<?php print $info['pupil_r']; ?>" stroke="black" stroke-width="1 " /> 

         //'Alphabet' 
         <text x="<?php print $info['al_x']; ?>" y="<?php print $info['al_y']; ?>" style="font-family:Times,serif;fill:#B40404;font-size:<?php print $info['size']; ?>"><?php print $al; ?></text> 

         <animateMotion 
          from="<?php print $info['from']; ?>"  to="<?php print $info['to']; ?>" 
          dur=" <?php print $info['duration']; ?>" repeatCount="indefinite" /> 
        </g> 
      <?php 
       } 
     } 
      ?> 
     </svg> 
</aside> 
<?php 
    } 
?> 

我正在做这个游戏,需要我点击基于字母顺序的鱼。目前,在点击鱼时,它会刷新页面来更新服务器。因此,有没有办法在不刷新页面的情况下更新服务器?我的代码中有动画,因此,每当鱼被点击和消失时,页面刷新本身,导致鱼动画再次重新启动。我希望鱼不停地移动,因此,有没有办法不断更新服务器?我的动画连接到mysql。

我真的需要一些建议,因为我不能自己做。

+4

关于'AJAX'的研究。 – mellamokb 2012-08-15 13:24:59

+0

看看AJAX,试试这个链接例如:http://stackoverflow.com/questions/5143191/inserting-into-mysql-from-php-jquery-ajax – 2012-08-15 13:27:06

+0

我可以使用刷新div吗? – Trifers 2012-08-16 01:40:55

回答

1

是的,你可以用JavaScriptJSON这样做,它与PHP真的很好的结合。

你可以使用Ajax的功能从jQuery的: * http://api.jquery.com/jQuery.ajaxSetup/ * http://api.jquery.com/jQuery.ajax/

发送包含数据发送到服务器,那么你可以使用json_decode(在PHP)服务器上的JSON字符串翻译的JSON到数组。

如果您需要更多帮助,只需询问:)

+0

我知道他们两人,但我不知道如何做自己的编码。有点令人困惑。 – Trifers 2012-08-15 13:50:05

+0

嗯,我读了有关ajax数据库阅读的wat。我对理解它很慢,所以我不太确定它是如何工作的。 – Trifers 2012-08-15 15:37:15