How to pass variable as $_POST key in PHP?

后端 未结 4 1395
北恋
北恋 2020-12-31 22:24

How can you pass a variable as the $_POST array key value in PHP? Or is it not possible?

$test = \"test\";
echo $_POST[$test];

Thanks

4条回答
  •  醉梦人生
    2020-12-31 22:57

    If I get you right, you want to pass a variable from one php-file to another via post. This sure is possible in several ways.

    1. With an HTML-form

    if you click on the submit-button, $_POST['key'] in target.php will contain 'foo'.

    2. Directly from PHP

    $context = stream_context_create(array(
        'http' => array(
          'method'  => 'POST',
          'header'  => "Content-type: text/html\r\n",
          'content' => http_build_query(array('key' => 'foo'))
        ),
      ));
    $return = file_get_contents('target.php', false, $context); 
    

    Same thing as in 1., and $return will contain all the output produced by target.php.

    3. Via AJAX (jQuery (JavaScript))

    
    

    Same thing as in 2., but now data contains the output from target.php.

提交回复
热议问题