Avoiding the $_POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Avoiding the $_POST

Post by SheDesigns »

I'm trying to create a form with a loop.. my only problem is that the server I'm working with, wants all the variables defined at the start of the file.

Code: Select all

$i = 0;
while($i<=$countofrows)
  {     
        $checking0 = "remove_" . $i;
        $checkforcheck = $$checking0;
        echo "$checkforcheck<br>";
        if($checkforcheck == "checked")
        
        {
            // Box has been checked, find Unique ID for deletion
            $checking1 = "msg_" . $i;
            $uniqueid = $$checking1;
            echo "$uniqueid<br><br>";
            //$query = "delete from Leads where organization = '$uniqueid'";
            //$result = mysql_query($query);                
        }     
                 
  $i++;
  }
For instance, just calling $countofrows does not work. I need to add the line, $countofrows = $_POST['countofrows']; Which is okay when I'm asking for defined variables.. but the number of records I'm looping are dynamic. : (

Is there a way around this without editting the globals? I don't have access to them, it's not my server, it' a clients.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Avoiding the $_POST

Post by requinix »

Everywhere you try to use a $variable you should use $_POST["variable"] instead. Literally.

Code: Select all

// $countofrows
$_POST["countofrows"]
 
// $checking0 = "remove_" . $i;
// $checkforcheck = $$checking0;
$checking0 = "remove_" . $i;
$checkforcheck = $_POST[$checking0];
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Avoiding the $_POST

Post by RobertGonzalez »

Your are moving from an environment that had register_globals on to one in which it is off. That is a good thing since register_globals is going to be deprecated in the next major release of PHP. It is best to get your code usable with it off.

To that end, a cheap and dirty fix for this is to use extract() on the $_POST array. It is essentially the same as assigning values to post array indeces as vars, like what you normally see here:

Code: Select all

<?php
foreach ($_POST as $k => $v) {
  ${$k} = $v;
}
?>
Post Reply