如何通过函数添加值 - PHP

时间:2015-07-06 12:34:23

标签: php html css

我正在尝试创建一个函数,每次单击一个按钮时,该函数会将数字添加到给定变量中。

我有4个按钮:农场,洞穴,房子,赌场 enter image description here

所以我想在这里实现的是我需要将按钮生成的随机数发送到一个变量,该变量将在"你的黄金"上添加所有的SCORE。部分。所以,让我们点击农场和洞穴按钮,这样洞穴将有20个洞穴,15个农场已经有35个金币。

这是我的form.php

<div class="wrapper">

    <div id="gold">
        <form action="process-game.php" method="post" >
            YOUR GOLD: <input type="hidden" name="building" value="gold"/>
        </form>
    </div>


<div class="farm_form">
    <h2>Farm</h2>
    <form action="process-game.php" method="post" >
        <input type="hidden" name="building" value="farm"/>
        <input type="submit" value="Find Gold!"/>
    </form>
</div>


<div class="farm_form">
    <h2>Cave</h2>
    <form action="process-game.php" method="post">
        <input type="hidden" name="building" value="cave"/>
        <input type="submit" value="Find Gold!"/>
    </form>
</div>


<div class="farm_form">
    <h2>House</h2>
    <form action="process-game.php" method="post">
        <input type="hidden" name="building" value="house"/>
        <input type="submit" value="Find Gold!"/>
    </form>
</div>


<div class="farm_form">
    <h2>Casino</h2>
    <form action="process-game.php" method="post">
        <input type="hidden" name="building" value="casino"/>
        <input type="submit" value="Find Gold!"/>
    </form>
</div>

</div>

这是我的process.php:

<?php
    session_start();

    if(isset($_POST['building'])){
        echo earn_gold();
    }

    function earn_gold(){
        if($_POST['building'] == "farm"){
            $gold = rand(10,20);
        }else if($_POST['building'] == "cave"){
            $gold = rand(5,10);
        }else if($_POST['building'] == "house"){
            $gold = rand(2,5);
        }else if($_POST['building'] == "casino"){
            $gold = rand(0,50);
        }
        return $gold;
    }


?>

知道怎么做吗?

4 个答案:

答案 0 :(得分:1)

我知道,你基本上想要一个PHP解决方案。不过,我无法抗拒地向你展示,在JavaScript / jQuery中做同样的事情是多么容易。看看它或者完全忽略它。这取决于你......; - )

// define gold amounts for each button (min,max):
var finds={farm:[10,20],cave:[5,10],house:[2,5],casino:[0,50]}; 

$(function(){
 $(':submit').click(function(){ // for all submit buttons: bind the click event to a function ...
   var place=$(this).closest('div[id]').attr('id'); // get the id of the buttin's parent div
   var fnd=finds[place]; // get the min/max array for the current button
   with ($('#gold span')) // locate and use the <span> inside the div with id=gold
     text(parseFloat(text()) // get the current value of the span (convert to float)
          +fnd[0]+Math.ceil(Math.random()*(fnd[1]-fnd[0]))); // add the gold ...
 });
})
div {display:inline-block; width: 120px; border:1px solid grey}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="farm_form" id="gold">
  <h2>Your Gold</h2><span>0</span>
</div><br>
<div class="farm_form" id="farm">
  <h2>Farm</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="cave">
  <h2>Cave</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="house">
  <h2>House</h2><input type="submit" value="Find Gold!"/>
</div>
<div class="farm_form" id="casino">
  <h2>Casino</h2><input type="submit" value="Find Gold!"/>
</div>

答案 1 :(得分:0)

当用户点击查找黄金时,然后提交一个表格,将黄金添加到总结果中。例如,如果用户获得3金币,则添加黄金变量。

$totalGold = 0;

when user.click button then $totalgold +3;

答案 2 :(得分:0)

拥有一个表单元素并使用javascript为每个按钮分配onclick事件,并且可以使用ajax动态提交表单并将结果显示在同一页面上。

答案 3 :(得分:0)

如果你真的需要它,我猜你可以用会话来做。

if (isset($_POST['building'])) {
    // getting random gold 
    $earn_gold = earn_gold();
    // adding to session called "gold"
    $_SESSION['gold'] = (isset($_SESSION['gold']) ? $_SESSION['gold'] + $earn_gold : $earn_gold);
    // redirect back to process page
    header('Location: process.php');
    exit;
}

然后像这样输出

<div id="gold">
    <form  method="post" >
        YOUR GOLD: <input type="hidden" name="building" value="gold"/> 
        <?php 
            // if set, outputting sessions "gold" value
            if (isset($_SESSION['gold'])) echo $_SESSION['gold']; 
        ?>
    </form>
</div>