Ratee Posted June 6, 2012 Share Posted June 6, 2012 Okay so i'm making a map where users can go search for Pokemon and capture them... for my new Pokemon game. but when i click to capture them it does not work... no error messages or nothing shows up it just refreshes the page and that's it.. please could someone help me to work this out as i am a Beginner, iv posted my code below, thanks:) The PHP <?php $map = rand(1, 10); $result = mysql_query("SELECT * FROM firemap ORDER BY RAND() LIMIT 1"); while($row = mysql_fetch_array($result)) if ($map==1) { echo "</br>"; echo $row['image']; echo "<br>"; echo "A wild "; echo $row['name'] . " Appeared. </br> Level: " . $row['level']; echo "<br />"; echo "<input type='hidden' name='$row[name]'>"; echo "<form action='map1.php' method=post>"; echo "<input type='hidden' name='$row[name]'>"; echo "<input type='submit' class='catchpokemon' value='Catch $row[name]' /></form>"; }else{ echo "<div class=\"pokemontext\">"; echo "<b>No wild Pokémon appeared.</b>"; echo "</br>"; echo "<i>Keep moving around to find one.</i>"; echo "<div>"; } if(isset($_POST['$row[name]'])) { echo "<div class=\"pokemonalign\">"; echo $row['image']; echo "</br>"; echo 'You have captured Wild'; echo $row['name'] . ""; echo "</div>"; $add = mysql_query("INSERT INTO pokemon (pokedex_id, owner_id, type, nickname) VALUES ('$row[id]', '$id', '1', '$row[name]')"); } else { } ?> SQL Table structure for table `firemap` `id` bigint(255) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `level` int(11) NOT NULL, `image` varbinary(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ; Thank you for your time. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 You are trying to access $row in a situation where it hasn't been set (the $_POST check). Turn on all error reporting, and give the form fields a static name so you know what to expect. Quote Link to comment Share on other sites More sharing options...
ejay Posted June 6, 2012 Share Posted June 6, 2012 I think you want if(isset($_POST["$row[name]"])) instead of if(isset($_POST['$row[name]'])) if this forum is not replacing your quotes. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 That will do nothing. That would check if a form field with the literal name of $row[name] had been sent. What he's doing is giving the fields the name of the pokemon. He needs to instead make the name something static like "pokemon_id" and use the id of the value, then check if $_POST['pokemon_id'] is set. Quote Link to comment Share on other sites More sharing options...
ejay Posted June 6, 2012 Share Posted June 6, 2012 I have been under the impression that it's the other way around Quote Link to comment Share on other sites More sharing options...
Ratee Posted June 6, 2012 Author Share Posted June 6, 2012 thank you for your reply's everyone but i still don't understand.. jesirose please could you give me a example? Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 When you submit the form, the entire page gets processed again, which means that $row['name'] will have a different value, since you're doing it randomly. Make the name of the form field (of which you have two, you only need it once), something like id, whatever your primary key is. Then for the value of the hidden input you'd do $row['id']. I have been under the impression that it's the other way around Yes, you were right, I apologize. The original way he has it is checking for the literal string - but changing it to your way won't work, because the value of $row['name'] will likely be different, due to the random factor. Quote Link to comment Share on other sites More sharing options...
ejay Posted June 6, 2012 Share Posted June 6, 2012 Following jesirose's suggestion, here's what you want (I'm bored to hell) <?php if (isset($_POST["pokemon_id"])) { $result = mysql_query("SELECT * FROM firemap WHERE id = " . intval($_POST["pokemon_id"])); $row = mysql_fetch_assoc($result); // or whatever you do with plain mysql functions if (isset($row['id'])) { echo "<div class=\"pokemonalign\">"; echo $row['image']; echo "</br>"; echo 'You have captured Wild'; echo $row['name'] . ""; echo "</div>"; $add = mysql_query("INSERT INTO pokemon (pokedex_id, owner_id, type, nickname) VALUES ('$row[id]', '$id', '1', '$row[name]')"); } } $map = rand(1, 10); $result = mysql_query("SELECT * FROM firemap ORDER BY RAND() LIMIT 1"); while ($row = mysql_fetch_array($result)) { if ($map == 1) { echo "<br />"; echo '<img src="' . $row['image'] . '" alt="" />'; echo "<br />"; echo "A wild "; echo $row['name'] . " Appeared. <br /> Level: " . $row['level']; echo "<br />"; echo "<form action='map1.php' method='post'>"; echo "<input type='hidden' name='pokemon_id' value='$row[id]' />"; echo "<input type='submit' class='catchpokemon' value='Catch $row[name]' />"; echo "</form>"; } else { echo "<div class=\"pokemontext\">"; echo "<b>No more wild Pokémon appeared.</b>"; echo "<br />"; echo "<i>Keep moving around to find one.</i>"; echo "</div>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Ratee Posted June 6, 2012 Author Share Posted June 6, 2012 okay thank you so much for your help everyone! there is just one small problem still... when the Pokemon is captured is still displays "No more wild Pokémon appeared. Keep moving around to find one." i was wondering if there is anyway to get rid of this when a Pokemon is caught? Quote Link to comment Share on other sites More sharing options...
ejay Posted June 6, 2012 Share Posted June 6, 2012 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST["pokemon_id"])) { $result = mysql_query("SELECT * FROM firemap WHERE id = " . intval($_POST["pokemon_id"])); $row = mysql_fetch_assoc($result); // or whatever you do with plain mysql functions if (isset($row['id'])) { echo "<div class=\"pokemonalign\">"; echo $row['image']; echo "</br>"; echo 'You have captured Wild'; echo $row['name'] . ""; echo "</div>"; $add = mysql_query("INSERT INTO pokemon (pokedex_id, owner_id, type, nickname) VALUES ('$row[id]', '$id', '1', '$row[name]')"); } } } else { $map = rand(1, 10); $result = mysql_query("SELECT * FROM firemap ORDER BY RAND() LIMIT 1"); while ($row = mysql_fetch_array($result)) { if ($map == 1) { echo "<br />"; echo '<img src="' . $row['image'] . '" alt="" />'; echo "<br />"; echo "A wild "; echo $row['name'] . " Appeared. <br /> Level: " . $row['level']; echo "<br />"; echo "<form action='map1.php' method='post'>"; echo "<input type='hidden' name='pokemon_id' value='$row[id]' />"; echo "<input type='submit' class='catchpokemon' value='Catch $row[name]' />"; echo "</form>"; } else { echo "<div class=\"pokemontext\">"; echo "<b>No more wild Pokémon appeared.</b>"; echo "<br />"; echo "<i>Keep moving around to find one.</i>"; echo "</div>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.