Jump to content

[SOLVED] Update Run On Same Page When Form Submited?


Recommended Posts

Can anyone help me? I'm wanting to have a page where people could train their characters (this is the beginning of an RPG game). What I want to do is if they press the train button, a query is run for whatever option they have selected. Say they chose test1 as the option and pressed train. I want to update the table:

 

energy - 2

experience + 50

 

The first thing I need to know how to do is have this script run on the same page only when they press the train button (it's a submit button actually; does that matter? or does it need to be a regular button?). The second thing is how to write the script to where it runs the script I want when the button is pressed. Here is the page I have so far:

 

<?php

include("connect_db.php");

$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];

}

$display_block = "
<h3>Train Your Character To Level Up</h3>
<table>
<tr>
<td valign=top>
<form>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>
</table>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sailor Moon RPG - Training Board</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Sailor Moon RPG - Training Board</h1>
<?php print $display_block; ?>
</div>
<?php include("bottomnav.php"); ?><!-- FOOTER -->
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters
are<br /> 
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br />
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav">
<ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul>
</div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

For what you wish to do is change text on a page that has already been sent from the server. PHP is a server side scripting language so can't do stuff once the process has left the server. So what you need to do is to use Javascript (AJAX) to make a request and then display the result.

 

Using JQuery's ajax functionality will make the task extremely easy.

 

 

Okay, well if I run it on a second page instead, how do I write the if statement? I tried this:

 

training_board.php:

<?php

include("connect_db.php");

$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];

}

$display_block = "
<h3>Train Your Character To Level Up</h3>
<table>
<tr>
<td valign=top>
<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>
</table>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sailor Moon RPG - Training Board</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Sailor Moon RPG - Training Board</h1>
<?php print $display_block; ?>
</div>
<?php include("bottomnav.php"); ?><!-- FOOTER -->
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters
are<br /> 
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br />
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav">
<ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul>
</div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

 

train.php:

<?php

include("connect_db.php");

$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];

}

if ($_POST['train'] == 'test1') {

$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];
    $update_energy = ($player_info['energy'] - 2);

}

$accept_scout_username = mysql_query("UPDATE training SET energy ='$update_energy' WHERE identity = '$identity'");

}

?>

 

But I am getting this error:

 

Notice:  Undefined index: train in C:\wamp\www\train.php on line 18

 

What am I doing wrong?

It's not an error it's a notice, most of us suppress our errors so we can be sloppy :P A notice is just to warn you that you have done some sloppy code. Such as call on non existancing variables etc

 

if ((isset($_POST['train'])) && ($_POST['train'] == 'test1'))

 

will get rid of the notice.

Prolly because you are running the update outside the loop.  If you are wanting to just decrease the int value of 'energy' you can have mysql do it with one query and save some processing time.

 

UPDATE training SET energy =energy+2 WHERE 1;

I don't understand what you mean. i tried putting the update statement inside of the whole loop, but that didn't work either. How would the statement:

 

UPDATE training SET energy =energy+2 WHERE 1;

 

take two away from the energy field? And how would I change my code? I don't quite understand what you mean. Sorry. Can you help explain it better?

<?php

include("connect_db.php");
/******
* Code doesn't do anything.
******
$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];

}
*/
if ($_POST['train'] == 'test1') {
/*******
* This code only updates the last identity in the table training.
*******
$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];
    $update_energy = ($player_info['energy'] - 2);

}

$accept_scout_username = mysql_query("UPDATE training SET energy ='$update_energy' WHERE identity = '$identity'");

**********
* This query updates all the identities in the table training.
**********/

mysql_query("UPDATE training SET energy=energy-2 WHERE 1");

}

?>

Well,  I choose test1 from the select list named train. Is this the right coding? Here's the code from the form:

 

<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
</form>

 

Am I writing something wrong?

 

Your PHP makes absolutely no sense. You have a worthless loop in there that just gets information from the table for no reason. Then you do the same thing, and for some reason you just update the last entry in the table. This won't work how you expect it to at all. If you want to grab a users info, then you have to set a where clause or something.

 

<?php

include("connect_db.php");

//why do you do this...
$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];

}
//the above code makes no sense at all. you are overwriting the variable everytime the loop runs
//you will only have the data from the last loop run...


if ($_POST['train'] == 'test1') {

//again, this makes no sense at all
$get_player_info = "select * from training";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];
    $update_energy = ($player_info['energy'] - 2);

}//makes no sense at all

//this is going to update the very last entry... not whoever is trying to train.
$accept_scout_username = mysql_query("UPDATE training SET energy ='$update_energy' WHERE identity = '$identity'");

}

?>

 

I very strongly suggest that you try a simpler problem before you tackle an RPG. you seem to not know the basics of pulling the right information from a mysql table. To be completely honest, this code will probably run, but won't do anything useful, and to make it do something useful will probably take 3 pages of explanation.

 

here is a  php/mysql tutorial

Another php/mysql tutorial

 

I suggest you read up on those tutorials, try a simpler project, and then tackle the RPG. It will be much less of a headache

 

 

I am working on the where clause now. But for some reason, I am doing the last page wrong. I have a page where they choose which identity to train, then the page that displays the identity with appropriate info, then the third page should update the values, but I have a flaw in my where clause. Could I not use $_GET? Here are the pages I have:

 

choose_train.php:

<?php

session_start();

if(!isset($_SESSION['loggedIn'])) {
header("Location: login.php");
}

include ("connect_db.php");

//show scouts characters
$get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'";
$get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error());
    while ($list_scouts = mysql_fetch_array($get_scouts_res)) {
    $identity = ucwords($list_scouts['identity']);
    $scout_id = $list_scouts['id'];
    echo "<ul class=\"character_list\"><li><a href=\"training_board.php?identity=$identity\">$identity</li></ul> ";
    }
    
//show knights characters
$get_knights = "select * from knights where username = '".$_SESSION['userName']."'";
$get_knights_res = mysql_query($get_knights, $conn) or die(mysql_error());
    while ($list_knights = mysql_fetch_array($get_knights_res)) {
    $identity = ucwords($list_knights['identity']);
    $knight_id = $list_knights['id'];
    echo "<ul class=\"character_list\"><li><a href=\"showprofile_knights.php?id=$knight_id\">$identity</li></ul> ";
    }

//show fdark_warriors characters
$get_fdark_warriors = "select * from fdark_warrior where username = '".$_SESSION['userName']."'";
$get_fdark_warriors_res = mysql_query($get_fdark_warriors, $conn) or die(mysql_error());
    while ($list_fdark_warriors = mysql_fetch_array($get_fdark_warriors_res)) {
    $identity = ucwords($list_fdark_warriors['identity']);
    $topic_id = $list_fdark_warriors['id'];
    echo "<ul class=\"character_list\"><li><a href=\"showprofile_fdark_warrior.php?id=$topic_id\">$identity</li></ul> ";
    }

//show mdark_warriors characters
$get_mdark_warriors = "select * from mdark_warrior where username = '".$_SESSION['userName']."'";
$get_mdark_warriors_res = mysql_query($get_mdark_warriors, $conn) or die(mysql_error());
    while ($list_mdark_warriors = mysql_fetch_array($get_mdark_warriors_res)) {
    $identity = ucwords($list_mdark_warriors['identity']);
    $topic_id = $list_mdark_warriors['id'];
    echo "<ul class=\"character_list\"><li><a href=\"showprofile_mdark_warrior.php?id=$topic_id\">$identity</li></ul> ";
    }
    
$display_block = "";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

</body>
</html>

 

<?php

include("connect_db.php");

$display_block = "<h3>Train Your Character To Level Up</h3>
<table>
<tr>";

$get_player_info = "select * from training where identity = '$_GET[identity]'";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];


$display_block .= "
<td valign=top>
<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>";
}

$display_block .= "</table>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sailor Moon RPG - Training Board</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Sailor Moon RPG - Training Board</h1>
<?php print $display_block; ?>
</div>
<?php include("bottomnav.php"); ?><!-- FOOTER -->
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters
are<br /> 
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br />
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav">
<ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul>
</div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

 

train.php:

<?php

session_start();

include("connect_db.php");

if ((isset($_GET['train'])) && ($_GET['train'] == 'test1')) {

$display_block = "<h3>Train Your Character To Level Up</h3>
<table>
<tr>";

$get_player_info = "select * from training WHERE identity = '$identity'";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];
    $update_energy = ($energy - 2);
    $update_experience = ($experience + 50);

$lose_energy = mysql_query("UPDATE training SET energy ='$update_energy' WHERE identity = '$identity'");
$gain_experience = mysql_query("UPDATE training SET experience ='$update_experience' WHERE identity = '$identity'");

$display_block .= "
<td valign=top>
<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>";

}

$display_block .= "</table>";

} else {

print "didn't work :-(";

}


?>

<html>
<head>
<title></title>
</head>
<body>
<?php print $display_block; ?>
</body>
</html>

ahh ok this code looks much better.

one thing

$get_player_info = "select * from training where identity = '$_GET[identity]'";

 

should be

$get_player_info = "select * from training where identity = '".$_GET['identity']."'";

you have to surround associative array keys with single quotes.

 

also in train.php I dont see anywhere where $identity is set before this line


$get_player_info = "select * from training WHERE identity = '$identity'";

which needs identity to be set for the query to run correctly

create a hidden field with the value of the identity, than using the get value, assign identity.

like

$display_block .= "
<td valign=top>
<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
<input type='hidden' name='id' value='$indentity' />
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>";

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.