Jump to content

self submitting form problem


cheesybiscuits

Recommended Posts

Got this page, a player clicks the "accept challenge" button, then it is recorded in the database that the challenge as been started and once the form has been self submitted, the button is disabled. It does what I expect, but I have to refresh the page again for the button to be displayed as disabled. How can I do it so the page is changed once the form has been submitted?

<?php
session_start();what  but after clicking a button I have to refresh the page again, before the button is displayed as disabled
include('functions.php');
connect();
?>
<!DOCTYPE html>
<html>
<head>
<title>University Crusade</title>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
<meta name="viewport" content="width=device-width, minimum-scale=1,maximum-scale=1, user-scalable=no">
</head>
<body>
<?php
if (isset($_SESSION['userid'])) {
include('safe.php');

if (isset($_POST['start1'])) {
	mysql_query("UPDATE chal1 SET start1=1 WHERE userid='".$_SESSION['userid']."'") or die ("Could not update start1");
}
if (isset($_POST['start2'])) {
	mysql_query("UPDATE chal1 SET start2=1 WHERE userid='".$_SESSION['userid']."'") or die ("Could not update start2");
}
if (isset($_POST['start3'])) {
	mysql_query("UPDATE chal1 SET start3=1 WHERE userid='".$_SESSION['userid']."'") or die ("Could not update start3");
}
?>
<ul id="tab-nav">
	<li><a href="stats.php" id="tab-character">Character</a></li>
	<li><a href="games.php" class="active" id="tab-games">Games</a></li>
	<li><a href="account.php" id="tab-account">Account</a></li>
</ul>
<div id="wrapper">
	<h2 id="name">Select a Challenge</h2>
	<ul id="table-view">
		<li>
			<fieldset>
			<legend>"Easter Egg" Challenge</legend>
			<div id="rewards">
				<p class="instructions">
					Find each of the 4 "Easter Egg" barcodes around
					the university campus.
				</p>
				<p>
					Rewards: Every egg is worth 
					<span class="stat">50</span> XP and 
					<span class="stat">35</span> gold coins.
				</p>
			</div>
			<form action="games.php" method="POST">
<?php 
if ($start1 == 1) {
	echo "<br />The value is 1";
	echo "<button class=\"buttons\" type=\"submit\" name=\"start1\" disabled=\"disabled\">Accept challenge</button>";
} elseif ($start1 == 0) {
	echo "<br />The value is 0";
	echo "<button class=\"buttons\" type=\"submit\" name=\"start1\">Accept challenge</button>";
}
?>
			</form>
			</fieldset>
		</li>
		<li class="even">
			<fieldset>
				<legend>Increase Strength</legend>
				<div id="rewards">
					<p class="instructions">
						You'll find the barcode in the Sports Centre.
					</p>
					<p>
						Rewards: Every time you visit the Sports Centre
						you'll receive <span class="stat">2</span> Str. points.
					</p>
				</div>
				<form action="games.php" method="POST">
<?php 
if ($start2 == 1) {
	echo "<br />The value is 1";
	echo "<button class=\"buttons\" type=\"submit\" name=\"start2\" disabled=\"disabled\">Accept challenge</button>";
} elseif ($start2 == 0) {
	echo "<br />The value is 0";
	echo "<button class=\"buttons\" type=\"submit\" name=\"start2\">Accept challenge</button>";
}
?>
				</form>
			</fieldset>
		</li>
		<li>
			<fieldset>
				<legend>Increase Intelligence</legend>
				<div id="rewards">
					<p class="instructions">
						You'll find the barcode in the university Library.
					</p>
					<p>
						Rewards: Every time you visit the Library
						you'll receive <span class="stat">2</span> Int. points.
					</p>
				</div>
				<form action="games.php" method="POST">
<?php 
if ($start3 == 1) {
	echo "<br />The value is 1";
	echo "<button class=\"buttons\" type=\"submit\" name=\"start3\" disabled=\"disabled\">Accept challenge</button>";
} elseif ($start3 == 0) {
	echo "<br />The value is 0";
	echo "<button class=\"buttons\" type=\"submit\" name=\"start3\">Accept challenge</button>";
}
?>
				</form>
			</fieldset>
		</li>
	</ul>
</div>
<div id="footer">
	<a class="buttons" href="logout.php">log me out</a>
</div>
<?php
} else {
	die ("
		<div id=\"wrapper\">
			<p>Opps! You don't seem to be logged in...</p>
			<a class=\"buttons\" href=\"index.php\">login now</a><br />
			<p>Don't have an account? No worries, just <a class=\"buttons\" href=\"register.php\">register for one.</a></p>
		</div>
	");
}
?>

Thanks

Link to comment
Share on other sites

In the safe.php file

<?php
$statsGet = mysql_query("
SELECT * FROM stats WHERE userid='".$_SESSION['userid']."'
") or die ("Could not get statistics from database");
$stats = mysql_fetch_assoc($statsGet);

$chal1Get = mysql_query("
SELECT * FROM chal1 WHERE userid='".$_SESSION['userid']."'
") or die ("Could not find data on challenge 1 from database");
$chal1 = mysql_fetch_assoc($chal1Get);

$task1 = $chal1['task1'];
$task2 = $chal1['task2'];
$task3 = $chal1['task3'];
$task4 = $chal1['task4'];
$start1 = $chal1['start1'];
$start2 = $chal1['start2'];
$start3 = $chal1['start3'];
$start4 = $chal1['start4'];


$curInt = $stats['int'];
$curStr = $stats['str'];
$curXp = $stats['xp'];
$curLevel = $stats['level'];
$curGold = $stats['gold'];

if ($curXp>=0&&$curXp<=74) {
$curLevel = 1;
mysql_query("UPDATE stats SET level='$curLevel' WHERE userid='".$_SESSION['userid']."'")
or die ("Could not update current level 1");
} elseif ($curXp>=75&&$curXp<=157) {
$curLevel = 2;
mysql_query("UPDATE stats SET level='$curLevel' WHERE userid='".$_SESSION['userid']."'")
or die ("Could not update current level 2");
} elseif ($curXp>=158&&$curXp<=323) {
$curLevel = 3;
mysql_query("UPDATE stats SET level='$curLevel' WHERE userid='".$_SESSION['userid']."'")
or die ("Could not update current level 3");
} elseif ($curXp>=324&&$curXp<=655) {
$curLevel = 4;
mysql_query("UPDATE stats SET level='$curLevel' WHERE userid='".$_SESSION['userid']."'")
or die ("Could not update current level 4");
} elseif ($curXp>=656&&$curXp<=1319) {
$curLevel = 5;
mysql_query("UPDATE stats SET level='$curLevel' WHERE userid='".$_SESSION['userid']."'")
or die ("Could not update current level 5");
}
?>

Link to comment
Share on other sites

So, $start1, $start2, $start3 has nothing to do with posting the form, it is only database call?  Yet, you are using that to determine the state of the button?  So, you should either.

 

1. Check the form submission to set the button.

OR,

2. Move the include for safe.php BELOW your updates, so that you pull the info AFTER you update it. (currently you update the info after you pull it).

Link to comment
Share on other sites

jcbones,

The variables $start1, $start2 and $start3 contain a value of 1 or 0. 1 meaning the challenge has already been accepted. The values are checked first in safe.php and then the page buttons are displayed - a value of 0 displays a normal button, a value of 1 displays a disabled button. If a button is clicked the database is updated. I'll try what you said and add my safe.php file at the bottom and see if it fixes things.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.