更新变量时不刷新页面


Update a variable without refreshing page

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Tylers inclass 3</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <style>
            #game{
                position: relative;
                width: auto;
                display: block;
                top:150px;
                left:390px;
                margin-left: auto;
                margin-right: auto;

            }
            #bg{
                position: fixed;
                bottom: 310px;
                left:230px;

            }
            #input
            {
                position: relative;
                top: 670px;
                left: 450px;
                z-index: 1;
            }

        </style>
    </head>
    <body>
        <div id="input">
            <form method="post">
            <table>
                    <tr>
                        <td align="right">Name: </td>
                        <td align="left"><input type="text"id="name"/></td>
                    </tr>
                    <tr>
                        <td align="right">Bet: </td>
                        <td align="left"><input type="text"id="bet"/></td>
                        <td align="right">Credit: </td>
                        <td align="left"><input type="text" name="credit" value="100" readonly="true"id="credit"/></td>
                    </tr>
            </table>
        </form>
        </div>
        <div id="bg"> 
            <img src= "img/machine.png" width="1200" height="580" alt="machine"/>
        </div>
        <div id="h">
        </div>
        <div id="h1">
        </div>
        <div id="game">
            <?php
            function display() {
                //creating the array of pictures that will also be referenaced in the code as item
                $items = array(
                    '<a href="?item=cherry">  <img src= "img/cherries.jpg" width="150" height="150" alt="cherry"/></a>',
                    '<a href="?item=cherry">  <img src= "img/cherries.jpg" width="150" height="150" alt="cherry"/></a>',
                    '<a href="?item=lemon">  <img src="img/lemon.png" width="150" height="150" alt="lemon"></a>',
                    '<a href="?item=lemon">  <img src="img/lemon.png" width="150" height="150" alt="lemon"></a>',
                    '<a href="?item=lemon">  <img src="img/lemon.png" width="150" height="150" alt="lemon"></a>',
                    '<a href="?item=lemon">  <img src="img/lemon.png" width="150" height="150" alt="lemon"></a>',
                    '<a href="?item=lemon">  <img src="img/lemon.png" width="150" height="150" alt="lemon"></a>',
                    '<a href="?item=bar">  <img src="img/bar.jpg" width="150" height="150" alt="bar"></a>',
                    '<a href="?item=bar">  <img src="img/bar.jpg" width="150" height="150" alt="bar"></a>',
                    '<a href="?item=bar">  <img src="img/bar.jpg" width="150" height="150" alt="bar"></a>',
                    '<a href="?item=star">  <img src="img/star.png" width="150" height="150" alt="star"></a>',
                    '<a href="?item=star">  <img src="img/star.png" width="150" height="150" alt="star"></a>',
                    '<a href="?item=star">  <img src="img/star.png" width="150" height="150" alt="star"></a>',
                    '<a href="?item=star">  <img src="img/star.png" width="150" height="150" alt="star"></a>'
                );



                $num1 = $items[rand(0, 12)];
                $num2 = $items[rand(0, 12)];
                $num3 = $items[rand(0, 12)];
                echo $num1;
                echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
                echo $num2;
                echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
                echo $num3;
                echo '<br><br><br><br>';
                echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';

                $credits = 100;
                $bet;
                //  if($items):
                //  endif;
                //echo $num2 = $items[$random_Num];
                // echo $num3 = $items[$random_Num];
                //setting a link to parent page to go back and play the full game again
                echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="./" id="goback"><img src="img/button.png" width="100" height="100" alt="star" onclick=""></a>';
                echo $_GET['name'];

                //game logic test cases for rock beating scissors paper beating rock and scissors beating paper
            }

            display();
            ?>
        </div>
    </body>
</html>

嘿家伙!我有一个问题。所以我希望能够接收玩家的名字和赌注,并跟踪他们的出价。然而,我刷新或"更新"老虎机的方式是链接回父页面。换句话说,我只是刷新页面点击。我如何改变这些图片点击按钮img而不重新加载页面。任何帮助都是宝贵的!谢谢你

您可以通过ajax发送用户数据。例如,在jQuery

$(document).ready(function(){
     $("form").submit(function(event){
          event.preventDefault();
          $.post("sumbitName.php",{
                "name":$("#name").val(),
                "bet":$("#bet").val()
          },function(result){
               // Update...
          });
     });
 });