1

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?

2 Answers2

1

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.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • ya i was figuring that. any idea on how to go about doing this then because there are 1-30 images produced for this form and i need to execute certain code based on the id of the picture. ide use css to change a submit button but the image would changed based on the item id – user3078244 Feb 03 '14 at 01:22
  • Why not just use a link and put the image id in the URL, like `mypage.whatever?image=2`? You could also do this with javascript, or even create one `
    ` per image and use a hidden field.
    – elixenide Feb 03 '14 at 01:28
0

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 

}
wpdaniel
  • 714
  • 8
  • 25