Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Loop to catch Post vars???

    Hi all, I just wanted to know if something was possible in php and if so how? What I want to do is have a loop to get post variables and put them into variables. Basically reduce this code below into a few lines. I hate having to have so many almost identical lines when I am coding from what I have found is that there is almost always a way to use a loop to reduce such things.

    Code:
    $returnNumber = isset($_REQUEST["returnNumber"]) ? $_REQUEST["returnNumber"] : "";
    $quoted = isset($_REQUEST["quoted"]) ? $_REQUEST["quoted"] : "";
    $quotedBy = isset($_REQUEST["quotedBy"]) ? $_REQUEST["quotedBy"] : "";
    $company = isset($_REQUEST["company"]) ? $_REQUEST["company"] : "";
    $firstName = isset($_REQUEST["firstName"]) ? $_REQUEST["firstName"] : "";
    $lastName = isset($_REQUEST["lastName"]) ? $_REQUEST["lastName"] : "";
    $address = isset($_REQUEST["address"]) ? $_REQUEST["address"] : "";
    $suburb = isset($_REQUEST["suburb"]) ? $_REQUEST["suburb"] : "";
    $state = isset($_REQUEST["state"]) ? $_REQUEST["state"] : "";
    $postCode = isset($_REQUEST["postCode"]) ? $_REQUEST["postCode"] : "";
    $phone = isset($_REQUEST["phone"]) ? $_REQUEST["phone"] : "";
    $received = isset($_REQUEST["received"]) ? $_REQUEST["received"] : "";
    $returned = isset($_REQUEST["returned"]) ? $_REQUEST["returned"] : "";
    $courier = isset($_REQUEST["courier"]) ? $_REQUEST["courier"] : "";
    $connote = isset($_REQUEST["connote"]) ? $_REQUEST["connote"] : "";
    $notes = isset($_REQUEST["notes"]) ? $_REQUEST["notes"] : "";
    $product = isset($_REQUEST["product"]) ? $_REQUEST["product"] : "";
    $serial = isset($_REQUEST["serial"]) ? $_REQUEST["serial"] : "";
    $reason = isset($_REQUEST["reason"]) ? $_REQUEST["reason"] : "";
    $faultyRepaired = isset($_REQUEST["faultyRepaired"]) ? $_REQUEST["faultyRepaired"] : "";
    $sentToManf = isset($_REQUEST["sentToManf"]) ? $_REQUEST["sentToManf"] : "";
    $testedOk = isset($_REQUEST["testedOk"]) ? $_REQUEST["testedOk"] : "";
    $footNotes = isset($_REQUEST["footNotes"]) ? $_REQUEST["footNotes"] : "";

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    extract

    Edit:
    Oh yeah, make sure you use EXTR_SKIP. No and's, if's or but's.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Also have a look at import_request_variables.

    The only "bad" thing I could see happening when using these two functions straight up would be, for instance, not having the variable $footNotes created if $_REQUEST['footNotes'] is not set.
    Last edited by kbluhm; 03-06-2009 at 12:28 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by kbluhm View Post
    Also have a look at {url=http://www.php.net/import_request_variables}import_request_variables[/url].

    The only "bad" thing I could see happening when using these two functions straight up would be, for instance, not having the variable $footNotes created if $_REQUEST['footNotes'] is not set.
    Agreed. And to add any type of security on this, you'd need to use the skip methods to prevent evil register_globals from restructuring you're code. If you were to predefine you're variables, the skip would leave you with empty variables lol.
    Or... maybe this would work:
    PHP Code:
    if (isset($_POST))
    {
        foreach (
    $_POST AS $key => &$val)
        {
            
    $GLOBALS[$key] = $val;
        }

    Extract to globals. That may work.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •