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 07:28:12 EDT 2006


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