Help: Simple form/php multiple buttons...

Discussion in 'PHP' started by Skillman13, Dec 7, 2009.

  1. #1
    I have this code which is like a double or nothing game...

    And if its heads an option comes up (a button) to take your winnings, -or the original button which will bet again...

    The problem is,

    The second button resets the whole form, -and does not update values/execute php...

    Can anyone suggest a fix to this? or how i can get this working?

    <form id="form1" name="form1" method="post" action="">
    <input type="submit" name="Submit" id="Submit" value="Play" />
    </form>
    <?
    if (isset($_POST['Submit'])) {
    echo "You pay the man $10 and watch as he flips the coin..." ."<br>";
    $coin = (rand()%2);
    if ($coin == 0) {
    echo "It lands on heads," . "<br>";
    $bet = $bet * 2;
    echo "You are now betting $". $bet ."<br>";
    mysql_query("UPDATE Doubleornothing SET Money = '$bet' WHERE Username = '$username'");
    ?>
    <form id="form2" name="form2" method="post" action="">
    <input type="submit" name="Submit2" id="Submit2" value="Take winnings" />
    </form>
    <?
    if (isset($_POST['Submit2'])) {
    echo "You take your $". $bet;
    mysql_query("UPDATE Zombie SET Money = Money '$bet' WHERE Username = '$username'");
    mysql_query("UPDATE Doubleornothing SET Money = '10' WHERE Username = '$username'");
    }
    }
    if ($coin == 1) {
    echo "It lands on tails," . "<br>";
    echo "You lose everthing..." . "<br>";
    mysql_query("UPDATE Doubleornothing SET Money = '10' WHERE Username = '$username'");
    }
    }

    Thanks alot,

    James
     
    Skillman13, Dec 7, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    It should be something like this
    
    <?
    if ($_POST['action'] == "Play")
    {
    	echo "You pay the man $10 and watch as he flips the coin..." ."<br>";
    	$coin = (rand()%2);
    	if ($coin == 0) {
    		echo "It lands on heads," . "<br>";
    		$bet = $bet * 2;
    		echo "You are now betting $". $bet ."<br>";
    		mysql_query("UPDATE Doubleornothing SET Money = '$bet' WHERE Username = '$username'");
         }
    	 if ($coin == 1) {
    		echo "It lands on tails," . "<br>";
    		echo "You lose everthing..." . "<br>";
    		mysql_query("UPDATE Doubleornothing SET Money = '10' WHERE Username = '$username'");
    	}
    }
    if ($_POST['action'] == "Take winnings"){
    	echo "You take your $". $bet;
    	mysql_query("UPDATE Zombie SET Money = Money '$bet' WHERE Username = '$username'");
    	mysql_query("UPDATE Doubleornothing SET Money = '10' WHERE Username = '$username'");
    }
    	
    	
    
    ?>
    <form id="form1" name="form1" method="post" action="">
    <input type="submit" name="action" id="Submit" value="Play" />
    <input type="submit" name="action" id="Submit2" value="Take winnings" />
    </form>
    
    Code (markup):
     
    javaongsan, Dec 7, 2009 IP
  3. akhil1311

    akhil1311 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you have to put form action. Also check the update query.
    mysql_query("UPDATE Zombie SET Money = Money '$bet' WHERE Username = '$username'");
    PHP:
    It should be like this

    mysql_query("UPDATE Zombie SET Money = '$bet' WHERE Username = '$username'");
    PHP:
     
    akhil1311, Dec 7, 2009 IP
  4. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It sort off works, I want it so the Play button is before the game text, -and the 'Take winnings' button only appears after there is some winnings -on mine, after one heads.

    Anyone suggestions?
     
    Skillman13, Dec 7, 2009 IP
  5. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    Try this

    <?
    <form id="form1" name="form1" method="post" action="">
    if ($_POST['action'] == "Play")
    {
    	echo '<input type="submit" name="action" id="Submit" value="Play" />';
    	echo "You pay the man $10 and watch as he flips the coin..." ."<br>";
    	$coin = (rand()%2);
    	if ($coin == 0) {
    		echo "It lands on heads," . "<br>";
    		$bet = $bet * 2;
    		echo "You are now betting $". $bet ."<br>";
    		mysql_query("UPDATE Doubleornothing SET Money = '$bet' WHERE Username = '$username'");
         }
    	 if ($coin == 1) {
    		echo "It lands on tails," . "<br>";
    		echo "You lose everthing..." . "<br>";
    		mysql_query("UPDATE Doubleornothing SET Money = '10' WHERE Username = '$username'");
    	}
    }
    if ($_POST['action'] == "Take winnings"){
    	echo "You take your $". $bet;
    	mysql_query("UPDATE Zombie SET Money = Money '$bet' WHERE Username = '$username'");
    	mysql_query("UPDATE Doubleornothing SET Money = '10' WHERE Username = '$username'");
    }
    ?>
    
    <? if ($bet > 0) { ?>
    <input type="submit" name="action" id="Submit2" value="Take winnings" />
    <? } ?>
    </form>
    Code (markup):
     
    javaongsan, Dec 8, 2009 IP