NYCPHP Meetup

NYPHP.org

[nycphp-talk] How to pass on variables with POST without using a form

Dan Cech dcech at phpwerx.net
Thu Sep 21 11:44:09 EDT 2006


Baer, Jon wrote:
> It's also available as a PEAR PHP-Compact function ...
> 
> http://pear.php.net/manual/en/package.php.php-compat.components.php 
> 
> - Jon

Yeah, I was going to mention that, but in general I've been underwhelmed
by PHP_Compat.  In this case, it seems to be unnecessarily complicated
and doesn't accept the third $arg_separator argument.

Dan

> -----Original Message-----
> From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org]
> On Behalf Of Dan Cech
> Sent: Thursday, September 21, 2006 7:28 AM
> To: NYPHP Talk
> Subject: Re: [nycphp-talk] How to pass on variables with POST without
> using a form
> 
> Cliff Hirsch wrote:
>>> Why not just use the built-in function http_build_query?
> 
> Maybe because it's php5-only?
> 
> If you want a php4 version, try this function I just cooked up which
> duplicates all the documented functionality of the php5 function.
> 
> function http_build_query($formdata,$numeric_prefix = '',$arg_separator
> = null,$_parent = null) {
>     if (empty($arg_separator)) {
>         $arg_separator = ini_get('arg_separator.output');
>     }
> 
>     $postdata = array();
>     foreach ($formdata as $k => $v) {
>         if (!empty($_parent)) {
>             $k = $_parent .'['. urlencode($k) .']';
>         } else {
>             if (is_numeric($k)) {
>                 $k = $numeric_prefix . $k;
>             }
>             $k = urlencode($k);
>         }
>         if (is_array($v)) {
>             $postdata[] =
> http_build_query($v,$numeric_prefix,$arg_separator,$k);
>         } else {
>             $postdata[] = $k .'=' . urlencode($v);
>         }
>     }
> 
>     return implode($arg_separator,$postdata);
> }
> 
> Dan
> 
>>> -------
>>> The easiest way I've found to do this is as follows:
>>>
>>> $myvars = array(
>>>   'item1' => 'value1',
>>>   'item2' => 'value2',
>>> );
>>>
>>> $postdata = array();
>>> foreach ($myvars as $k => $v) {
>>>   $postdata[] = urlencode($k) .'=' . urlencode($v); } $postdata = 
>>> implode('&',$postdata);




More information about the talk mailing list