user3078244
user3078244

Reputation: 53

php form submit failing

i have an array that populates a form with images. what i am trying to do is when you click an image it executes a query so i set up a basic test code to see if i could get this to work. my array populates the form but when i click to go to the itemcheck page to determine what to execute the if statement fails when it should be true, cand i cant figure out why.

form

<?php
$field = 0; 
echo '<form action="itemcheck.php" method="post">  <table><tbody>';
mysql_connect("localhost", "root", "nathan") or
die("Could not connect: " . mysql_error());
mysql_select_db("game");
$result = mysql_query("SELECT item_id FROM user_item WHERE user_id =   '".getStat('id')."' LIMIT 30");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
if ($field % 5 == 0) echo '<tr>'; 
 echo '<td id="fog" valign="top" width="48">
    <input  type="image" src="img/items/'. intval($row["item_id"]). '.png"    name="item'.$row["item_id"].'" >
'; 

   if ($field % 5 == 4) 
   {
   echo '</tr>'; 
   $field = 0;
   }
   else
   $field++; 

}
mysql_free_result($result);
?> 
</tr></table></form>

and just a simple check

<?php 
include 'includes/stats.php';
include 'includes/login-check.php';    <---session exists
if ($_POST['item2'])
{
echo'it appears to be working.';
} else {
echo 'failed';
}

?>

this should execute true because the output from the array is 
<form blah blah blah>
<input  type="image" src="img/items/2.png" name="item2" >
</form>

any idea on why this is failing?

Upvotes: 1

Views: 43

Answers (2)

elixenide
elixenide

Reputation: 44841

You can submit a form with type="image", but it can't carry any data. See this answer for details.

Because there is no associated $_POST data, if ($_POST['item2']) fails.

Upvotes: 1

wpdaniel
wpdaniel

Reputation: 762

in your html:

<button type="submit" name="submit">Submit</button>

or

<input type="submit" name="submit" value="Submit"/>

and in your php:

if(isset($_POST['submit'])) {

// do this 

}

else {

//whatever 

}

Upvotes: 0

Related Questions