I have tried all I can think of but yet cannot figure out why I my form value is not posting the right value.

Here is the code:

$conn = connect();


$flags = Getflags();
$category = Getcategories();
$newitem = GetNewItems();

[COLOR="red"]//This passes the value to the display.php just fine but it is the wrong value.[/COLOR]
$cid = $_POST['xcid'];            


[COLOR="Red"]//Here is the function I am having problems with. I am pulling out the right records and the right values. Yet when I use the onclick submit that I set up using the image I get the ID value of the last record in the mysql database instead of the ID value for the record..[/COLOR]

function Getflags()
{
    $res = sql_select_country();       

$flags = '<form action="display.php" method="post"><table>';   

while($resultset = @mysql_fetch_array($res)) 
{
$id = $resultset['id']; 
$country=$resultset['country_name'];
$flag=$resultset['flag_image'];

$flags .='
<tr>
[COLOR="red"]///This is the value I need to have fixed the hidden field[/COLOR]
    <td><input type="hidden" name="xcid" value="'.$id.'" />'. $country .'</td>
    <td><input type="image" src="'. $flag .'"  onclick="javascript:this.form.submit()"/></td> 
   <td></td>
    </tr> ';
    $i += 1; 
    }
    $flags .=' </table></form>';
   mysql_free_result($res);

return $flags;

}

//select categories to edit or delete
function Getcategories()
{

$category = '<form action="display.php" method="post"><table>';
global $conn;
$sql="SELECT ID, category FROM category";
$res=mysql_query($sql, $conn);
while ($resultset = @mysql_fetch_array($res))
    {
    $category .= ($resultset['id']);
    $category .= '<tr>
    <td><input type="submit" value="' . $resultset['category'] . '" /></td> 
    </tr>';
    $i += 1; 
    }
$category .= '</table></form>';
mysql_free_result($res);
return $category; 
}



[COLOR="red"]//Display.php code[/COLOR]
$conn = connect();

 $var = $_GET['xcid']; [COLOR="red"]///Shows the value of the last record only??? So confused[/COLOR]
 $content = $var;

 

If anybody can help me see where I am going wrong I am so close yet so far!!!! Help!!!

Thank you

    Ofcourse it shows the last value. If you insert multiple form elements with the same name and send the form, it will give you the last value with the same name. All xcid's are now in the same form. When you click the flag, it will send all information inside form.

    There are multiple solutions to this. You could create the form inside the loop but then it will increase the output size of your script. Probably the easiest would be to just use get method to achieve that and get rid of the form completely eg.:

    $flags .= '<tr><td></td><td><a href="display.php?xcid='.$id.'"><img src="'.$flag.'" alt="" /></a></td><td><td></tr>';
    

    Now when someone clicks the flag it will be found in $_GET['xcid'] variable.

    There are other ways also but this is the easiest.

      Write a Reply...