Page 1 of 1

Avoiding the $_POST

Posted: Sun Nov 30, 2008 7:34 pm
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.

Re: Avoiding the $_POST

Posted: Sun Nov 30, 2008 9:09 pm
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];

Re: Avoiding the $_POST

Posted: Sun Nov 30, 2008 11:27 pm
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;
}
?>