Jump to content

post value always last record selected value


dartfrogs

Recommended Posts

I am retrieving data from a mysql table and I am using the image with an onclik attribute to submit the information from the records retrieved.

 

When I echo the post value of the record click it always comes back as the value of the last record retrieved from the table.

 

here is the code.

 

<?php session_start(); 

include ("battlegear.php");

include("lib/io.lib.php");

 

 

 

$title .= ' <table width="100%"><tr><td align="center"><font color="red">Welcome To</font></td></tr></table>';

$footercontent .= '<table class="bd" width="100%"><tr><td class="hr">Battlegear Toys</td></tr></table>'; 

 

$conn = connect();

 

 

$flags = Getflags();

$category = Getcategories();

$newitem = GetNewItems();

 

///this is the value I need passed to the other page...

echo $cid = $_POST['xcid']; 

 

This is the function I am using to get the records from mysql and I am using an image with the onclick to submit the info. to test to see if I get the right value to pass I have echoed the value back to the same page but it keeps coming up with the value of the last record. I am not sure where I am going wrong or if this is even the right way to try this.

 

 

function Getflags() 

{

    $res = sql_select_country();     

       

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

   

    while($resultset = @mysql_fetch_array($res))

    {

    $id = $resultset['id'];

    $country=$resultset['country_name'];

    $flag=$resultset['flag_image'];

   

    $flags .='

    <tr>

    <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;

                 

}

 

function sql_select_country()

{

  global $conn;

  $sql = "SELECT `id`, `country_name`,  `flag_image`  FROM `country`";

 

  $res = mysql_query($sql, $conn) or die(mysql_error());

  return $res;

}

 

function connect()

{

  $conn = mysql_connect("localhost", "root", "raccoon+eyes");

  mysql_select_db("battlegear_inventory");

  return $conn;

}

 

 

// stuff strings into template and return html string

  echo use_template("templates/page.html",

    array("#content#","#title#", "#flags#", "#footercontent#", "#category#", "#newitem#"),

    array($content, $title, $flags, $footercontent, $category, $newitem));

   

 

?>

 

I have placed the code on the internet the link is

 

http://www.magnetic-webdesign.com/newsite/index.php

 

When you click on a country flag image it is suppose to pass the id value through to the next page but as you can see it only passes the last id value of the last record.

 

I hope this is making sense to you all. Any help I can get to fix this would be great.

Link to comment
Share on other sites

I figured out the problem... Yippee!

 

function Getflags()
{
    $res = sql_select_country();       
    $cid = $_POST['flag'];   
    $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>
    <td>'. $country .'</td>
    <td><input type="image" src="'. $flag .'" name="flag" value="'.$id.'"  onclick="javascript:this.form.submit()"/></td> 
   <td></td>
    </tr> ';
    //$i += 1; 
    }
    $flags .=' </table></form>';
   mysql_free_result($res);

    return $flags;
                  
}

 

You can see where I made the changes and it works...

Link to comment
Share on other sites

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.