Help with $_GET Function

Discussion in 'PHP' started by richbate, Jun 1, 2007.

  1. #1
    I'm building an online game as a poject at the moment, for one of the pages, it allows to user to purchase units spending there gold and turns. I have a table with a form outputting the unit data from an SQL Database as an array, so whenever i add a new unit onto the database, it will appear on the list.

    This is the code so far:

    <?php
    while($table = mysql_fetch_array($units2)){
    ?>
    <tr>
    <td width="68" align="center"><?php echo $table['name']; ?>
    </td>
    <td width="58" align="center"><?php echo $table['price']; ?>
    </td>
    <td width="100" align="center"><input name="<?php echo $table['name']; ?>" type="text" id="<?php echo $table['id']; ?>" size="2" maxlength="4"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td><input name="Submit" type="submit" id="Submit" value="Buy Units"></td>
    </tr>
    </table>
    </form>



    Now my question is, how can i gather the data from the form when it submits. I know how to do the old $_GET feature, but as the table is dynamic and an array, its always changeing.

    how can i make a list of $_GET's pulling the ID Details off the SQL Database?
     
    richbate, Jun 1, 2007 IP
  2. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    There are a few options you could do, a simple one would be to create a hidden form field with all the IDs you need to process when it's submitted.

    <input type="hidden" name="myIds" value="1,2,3,4,5" /> // use PHP to print out the value field


    Then when the form is submitted on the PHP side

    
    if(isset($_POST['myIds'])){
    	$myIds = explode(",", $_POST['myIds']);
    	foreach($myIds as $index=>$myId){
    		if(isset($_POST[$myId])){
    			// validate that myId is a valid id, like an int or something
    			
    			// process the data from the form
    		}
    	}
    }
    
    PHP:
     
    mrmonster, Jun 1, 2007 IP