Jump to content

Recommended Posts

Hmmmm. Let me try and be a little clearer. When the information is submitted from the form, there is information dependent on the submitted information that does not update. Essentially, I am trying to make a purchase. The purchase goes through, however the amount of money the user has does not reflect the purchase until you go to another page, or refresh the current page.

Here is the posting code, which works...

 

<?PHP
if (isset($_POST['submit'])) 
	{
		 $fighters_buy=$_POST['fighters'];
		 $rangers_buy=$_POST['rangers'];
		 $clerics_buy=$_POST['clerics'];
		 $mages_buy=$_POST['mages'];
		 $rogues_buy=$_POST['rogues'];
		 $assassins_buy=$_POST['assassins'];
		 $monks_buy=$_POST['monks'];
		 $engineers_buy=$_POST['engineers'];
		 $siege_buy=$_POST['siege'];
		 $cotg_buy=$_POST['cotg'];
	}
		if($fighters_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($rangers_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($clerics_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($mages_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($rogues_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($assassins_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($monks_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($engineers_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($siege_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
		if($cotg_buy < 0)
	{
		print "Please enter a positive number.";
		echo $footer;
		exit();
	}
$gold_spent=
($fighters_buy * $config[fighters]) + 
($rangers_buy * $config[rangers]) +
($clerics_buy * $config[clerics]) +
($mages_buy * $config[mages]) +	
($rogues_buy * $config[rogues]) +
($assassins_buy * $config[assassins]) +
($monks_buy * $config[monks]) +
($engineers_buy * $config[engineers]) +
($siege_buy * $config[siege]) +
($cotg_buy * $config[cotg]);

$total_buy = $fighters_buy+$rangers_buy+$clerics_buy+$mages_buy+rogues_buy+$assassins_buy+$monks_buy+$engineers_buy+$cotg_buy+
			  $user[fighters]+$user[rangers]+$user[clerics]+$user[rogues]+$user[assassins]+$user[monks]+$user[engineers]+$user[cotg];
$total_purchase = $fighters_buy+$rangers_buy+$clerics_buy+$mages_buy+rogues_buy+$assassins_buy+$monks_buy+$engineers_buy+$cotg_buy;

if($total_buy > $total_hire)
	{
		print "You need more barracks to house that many troops.";
		echo $footer;
		exit();
	}

if($total_buy > $user[pop])
	{
		print "You need more peasants to hire that many troops.";
		echo $footer;
		exit();		
	}
if($gold_spent > $user['gold'])
	{
		print "You need $$gold_spent to hire that many troops.";
		echo $footer;
		exit();		
	}
else
	{
		 $new_gold=$user['gold']-$gold_spent;
		 $new_pop=$user['pop']-$total_buy;
		 $new_fighters=$user['fighters']+$fighters_buy;
		 $new_rangers=$user['rangers']+$rangers_buy;
		 $new_clerics=$user['clerics']+$clerics_buy;
		 $new_mages=$user['mages']+$mages_buy;
		 $new_rogues=$user['rogues']+$rogues_buy;
		 $new_assassins=$user['assassins']+$assassins_buy;
		 $new_monks=$user['monks']+$monks_buy;
		 $new_engineers=$user['engineers']+$engineers_buy;
		 $new_siege=$user['siege']+$siege_buy;
		 $new_cotg=$user['cotg']+$cotg_buy;
		 $new_pop=$user['pop']-$total_purchase;
		 print "You purchased $total_purchase troops for $$gold_spent.";

	}

//Update DB
$query= "UPDATE members SET 
		pop='".$new_pop."',
		fighters='".$new_fighters."', 
		rangers='".$new_rangers."', 
		clerics='".$new_clerics."', 
		mages='".$new_mages."', 
		rogues='".$new_rogues."', 
		assassins='".$new_assassins."', 
		monks='".$new_monks."', 
		engineers='".$new_engineers."', 
		siege='".$new_siege."', 
		cotg='".$new_cotg."', 
		gold='".$new_gold."' WHERE leader = '".($_SESSION['SESS_LEADER_NAME'])."'";  
$result = mysql_query($query); 
	if($result) 
		{
	} 
	else 
	{ 
			echo mysql_error(); 
	}

?>

 

code that is in a different table, but on the page and not reflecting changes made by the above code:

    <th>Resources</th>
    <td align="left"> </td>
  </tr>
  <tr>
    <td><strong>Turns:</strong></td>
<td align="left"><?PHP echo $user['turns'] ?></td>
  </tr>
  <tr>
    <td><strong>Gold:</strong></td>
    <td align="left">$<?PHP echo number_format($user['gold']) ?></td>
  </tr>
  <tr>
    <td><strong>Food:</strong></td>
<td align="left"><?PHP echo number_format($user['food']) ?></td>
  </tr>
  <tr>
    <td><strong>Mana:</strong> </td>
<td align="left"><?PHP echo number_format($user['mana']) ?></td>
  </tr>
  <tr>
    <td><strong>Land:</strong></td>
    <td align="left"><?PHP echo number_format($user['land']) ?></td>
  </tr>
  <tr>
    <td><strong>Peasants:</strong> </td>
<td align="left"><?PHP echo number_format($user['pop']) ?></td>
  </tr>

 

When posting, if you purchase anything, it will only be reflected by manually refreshing the page. ie change in gold, change in population, change in number of troops.

Here, where you have this bizarre little snippet...

 

if($result)
       {
      }
    else
      {
          echo mysql_error();
      }

 

You need to redirect back to this same page once your update is successful.

 

if ($result) {
  header('Location: scriptname.php');
  exit();
} else {
  echo mysql_error(); 
}

 

On a side note. Simply echoing mysql_error() like that if there is a problem could pose a security risk.

Is your SELECT query inside some conditional logic so that is it not executed on the same page request that is due to the form submission?

 

I would recommend the following headers to prevent caching of dynamically produced content (and make sure you sure you are not attempting to output headers after you have output content) -

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 

I have a page with a form. What I want the page to do is show the quantity submitted on the page as soon as it is posted. At this time when it is posted, it shows the values previous to posting the form info. I have tried meta and php no cache to no avail.

 

I was referred here by a friend, who raved about how great these guys were, and especially patient with newbies. The condescending tone is not much appreciated by someone teaching themselves for less than a month. I am in the process of learning the language, and was assured I could use this as an aid to do so. Sorry if I offended you by my lack of experience in a help forum....

I was referred here by a friend, who raved about how great these guys were, and especially patient with newbies. The condescending tone is not much appreciated by someone teaching themselves for less than a month. I am in the process of learning the language, and was assured I could use this as an aid to do so. Sorry if I offended you by my lack of experience in a help forum....

 

Where did that come from?

Sorry. I am a little frustrated. I thought the signature:

Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

 

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

 

was directed at me personally...

OK, if you forgive me for having my momentary lapse of goofiness,how can I post a conformation to the page I am redirected to?

 

		if ($result) 
	{
  			header('Location: barracks.php'); <- To this
      This->	print "You purchased $total_purchase troops for $$gold_spent.";
  			exit();
	} 
		else 
	{
		trigger_error();		
	}

Well, I figured out something that  works for me. Thought I would post in the event it may solve someone else's problem

 

		if ($result) 
	{
			header('refresh: 4; url= barracks.php');
			echo $success_header;
			print "You hired $total_purchase troops for $$gold_spent.";
			echo $footer;
  				exit();
	} 
			else 
	{
  				trigger_error(); 
	}

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.