By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

can $_POST have variables?

P: 5
Hi Guys,

This is quite long, so i request that you please bear with me.

Quick Version:
1. can $_POST have variables
2. if yes, can i get help in trying to work out my problem - check if a particular button, out of x number of buttons, has been pressed and to retrieve relevant data
3. if no, what other possible ways are there to solve my problem - check if a particular button, out of x number of buttons, has been pressed and to retrieve relevant data
4. is there an easier way to to do what im trying to achieve with the code.

Long Version:
My php is very rusty, so please bear with me. It might be a silly question but in PHP can the $_POST contain a variable? as in $_POST[$var]?

Basically what Im trying to do is check if a particular button, out of x number of buttons, has been pressed and retrieve the relevant data. Im begining to think that its not possible for $_POST to contain a variable, so if you have any suggestions to my problem, i would very much appreciate the help. Ill post the code below and then an explanation after.

Ive been going at this code for quite sometime, so it will be messy and my explanation might be confusing, but ill do my best. to shorten the code, ive gotten rid of all the comments and unnecessary code, so even if you try out the code, there shouldnt be any errors.


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html
  2.       PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3.       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  5. <head> 
  6.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7.  
  8. <title>Stock Transaction</title>
  9. <link rel="stylesheet" href="css/stylepopup.css"/>
  10. <style type="text/css" media="screen,projection">
  11.             @import url(css/calendar.css);
  12.         </style>
  13.  
  14.  
  15. <script type="text/javascript" src="javascript/calendar.js"></script>
  16. </head>
  17.  
  18. <?php
  19. include("include/sqlcon.php");
  20. $querya="SELECT StoreID FROM Store";
  21.             $resulta = mysql_query ($querya);
  22.             $x=mysql_num_rows($resulta);
  23.             $y=0;
  24.             while($nt=mysql_fetch_array($resulta) and $y<$x ){
  25.             $a="submit".$y;
  26.                 if(isset($_POST[$a])) {
  27.                     $query="SELECT StockID FROM Stock WHERE store_id = $y";
  28.                     $result = mysql_query ($query);
  29.                     while ($nt=mysql_fetch_array($result)) {
  30.                         $queryitem="SELECT item_id FROM Stock WHERE StockID = $nt[StockID]";
  31.                         $item = mysql_query ($queryitem);
  32.                         $querystore="SELECT store_id FROM Stock WHERE StockID = $nt[StockID]";
  33.                         $store = mysql_query ($querystore);
  34.                         $stockid=$_POST[$nt[StockID]];
  35.                         $quantity=$_POST['transferquantity'.$nt[StockID]];
  36.                         $comments=$_POST['comments'.$nt[StockID]];
  37.  
  38.                         echo "Button: $a <br/>";
  39.                         echo "item: $item <br/>";
  40.                         echo "store: $store <br/>";
  41.                         echo "stockid: $stockid <br/>";
  42.                         echo "quantity: $quantity <br/>";
  43.                         echo "comments: $comments <br/>";
  44.                     }
  45.                 }
  46.             $y++;
  47.             }
  48.  
  49. echo "<body>";
  50. ?>
  51. <h3>Stock Transaction</h3>
  52.  
  53.             <div>
  54.             <select name="choosestore" size="10" style="width:150px">
  55.             <?php
  56.             include("include/sqlcon.php");
  57.             $querya="SELECT StoreID,name FROM Store";
  58.             $storearray = array();
  59.             $resulta = mysql_query ($querya);
  60.             $x=mysql_num_rows($resulta);
  61.             $y=0;
  62.             $c=0;
  63.             $d=0;
  64.             while($nt=mysql_fetch_array($resulta) and $y<$x ){
  65.                 echo "<option value=\"$nt[StoreID]\"
  66.                     onclick=\"";
  67.                     while ($d<$x){
  68.                         echo "setVisibility('sub$d', 'none');\n";
  69.                         $d++;
  70.                     }
  71.                 echo "setVisibility('sub$y', 'inline');\"\>$nt[name]</option>\n\n";
  72.                 $storearray[] = $nt['StoreID'];
  73.                 $y++;
  74.                 $d=0;        
  75.                 }
  76.             ?></select>
  77.         </div>
  78.             <?php
  79.                 foreach ($storearray as $b){
  80.                     $query="SELECT Stock.StockID AS StockID, Item.ItemID AS ItemID, Item.name AS Item, Stock.store_id AS store_id, Stock.quantity AS quantity FROM Stock INNER JOIN Item ON Item.ItemID=Stock.item_id WHERE store_id = $b";
  81.                     $result = mysql_query ($query);
  82.                     echo "<div id=\"sub$c\" class=\"a\" style=\"width:500px; height:200px; overflow:scroll; scrollable:\">\n";
  83.                     echo "<form method=\"post\" action=".htmlentities($_SERVER['PHP_SELF'])." onsubmit=\"return checkform(this);\">";
  84.                     echo "<table>\n<tr><td><b>Item</b></td><td><b>Available</b></td><td><b>Transfer</b></td><td><b>Comments</b></td></tr>\n";    
  85.                     while ($nt=mysql_fetch_array($result)) {
  86.                         echo "<tr><td>$nt[Item]</td><td>$nt[quantity]</td><td><input type=\"text\" size=\"3\" name=\"transferquantity$nt[StockID]\" value=\"0\"/></td><td><lable>Comment: </lable><textarea name=\"comments$nt[StockID]\" cols=\"40\" rows=\"2\"></textarea></td></tr>\n";
  87.                     }
  88.     echo "</table><br/><br/>
  89.     <div>
  90.     <input type=\"submit\" name=\"submit$c\" value=\"Add to Records\"/>
  91.     <input type=\"button\" value=\"Cancel\" onclick=\"window.close()\"/> 
  92.     </div>
  93.     </form>";
  94.     echo "\n</div>\n\n";
  95. $c++;
  96. }
  97. ?>
  98. </body>
  99. </html>
  100.  

Basically what im trying to do is create some kind of inventory system. The user is presented with a list of stores. Once the user clicks on a store, a list of items that the store contains is shown. The user can then enter the quantity as well as any comments. Inorder to see if the code was working I tried to echo out the variables instead of putting it through to the database, but they pretty much always come out blank

Where im having trouble is that there is no set number of stores, so for example a user can have 3 or 10 stores and add 3 0r 10 more if they please. Therefor what i have done is based on the number of stores in the database, i have created that many forms using a loop, i.e. a form for each store.

The submit button of each form is based on the number the store appears in the list that is retrieved from the database (i.e. 1st store from database = 0, button is submit0
2nd store from database = 1, button is submit1
3rd store from database = 2, button is submit2
....etc)

What i tried to do after the submit button is pressed is retrieve the list of stores from the database again (which will obviously be in the same order as the first time it was retrieved), and create a loop that will change the integer value of submit.

So theoretically if $_POST does accept variables, then i should be able to get the details of the particular form that has been submitted...but thats not the case.

I know that my code is not perfect or even up to good standards, i could possibly have made a mistake which i may have looked over, however i dont think i messed up anywhere.
To sum up:
1. can $_POST have variables
2. if yes, can i get help in trying to work out my problem - check if a particular button, out of x number of buttons, has been pressed and to retrieve relevant data
3. if no, what other possible ways are there to solve my problem - check if a particular button, out of x number of buttons, has been pressed and to retrieve relevant data
4. is there an easier way to to do what im trying to achieve with the code.

I would greatly appreciate your help.

Thanks
Nov 2 '11 #1

✓ answered by johny10151981

The answer is you can,

You can pass any variable as array index..

here is
example

Expand|Select|Wrap|Line Numbers
  1. $v="Something";
  2. $c=$_POST[$v];
  3.  
  4.  

Share this Question
Share on Google+
2 Replies


100+
P: 1,059
The answer is you can,

You can pass any variable as array index..

here is
example

Expand|Select|Wrap|Line Numbers
  1. $v="Something";
  2. $c=$_POST[$v];
  3.  
  4.  
Nov 3 '11 #2

P: 5
thanks johny10151981, turns out you can use variables in POST

actually i found out where i went wrong. after looking for which button was pressed, i queried the database for the number the store appears in the list, as opposed to the id of the store....i guess it pays to take a break, otherwise you miss the things that stare you right in the face,lol.
Nov 3 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.