Programing

POST로 전송 된 모든 변수를 가져 오시겠습니까?

lottogame 2020. 10. 28. 07:40
반응형

POST로 전송 된 모든 변수를 가져 오시겠습니까?


게시물과 함께 보낸 모든 변수를 삽입해야합니다. 각 변수는 사용자를 나타내는 확인란이었습니다.

GET을 사용하면 다음과 같은 결과가 나타납니다.

?19=on&25=on&30=on

데이터베이스에 변수를 삽입해야합니다.

POST로 모든 변수를 보내려면 어떻게해야합니까? 쉼표로 구분 된 배열 또는 값으로?


변수 $_POST가 자동으로 채워집니다.

var_dump($_POST);내용을 보십시오 .

다음과 같은 개별 값에 액세스 할 수 있습니다. echo $_POST["name"];

물론 이것은 귀하의 양식이 일반적인 양식 인코딩 (예 : enctype=”multipart/form-data”

게시물 데이터가 다른 형식 (예 : JSON 또는 XML) 인 경우 다음과 같이 할 수 있습니다.

$post = file_get_contents('php://input');

$post원시 데이터가 포함됩니다.

표준 $_POST변수를 사용한다고 가정하면 다음 과 같이 확인란이 선택되어 있는지 테스트 할 수 있습니다.

if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
     ...
}

체크 박스 배열이있는 경우 (예 :

<form action="myscript.php" method="post">
  <input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
  <input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
  <input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
  <input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
  <input type="checkbox" name="myCheckbox[]" value="E" />val5
  <input type="submit" name="Submit" value="Submit" />
</form>

[ ]확인란 이름에를 사용 하면 선택한 값이 PHP 스크립트에서 배열로 액세스됨을 나타냅니다. 이 경우 $_POST['myCheckbox']단일 문자열을 반환하지 않지만 선택된 확인란의 모든 값으로 구성된 배열을 반환합니다.

예를 들어, 모든 상자를 선택하면 다음 $_POST['myCheckbox']으로 구성된 배열이 {A, B, C, D, E}됩니다. 다음은 값 배열을 검색하고 표시하는 방법의 예입니다.

  $myboxes = $_POST['myCheckbox'];
  if(empty($myboxes))
  {
    echo("You didn't select any boxes.");
  }
  else
  {
    $i = count($myboxes);
    echo("You selected $i box(es): <br>");
    for($j = 0; $j < $i; $j++)
    {
      echo $myboxes[$j] . "<br>";
    }
  }

$_POST변수 에서 액세스 할 수 있어야 합니다.

foreach ($_POST as $param_name => $param_val) {
    echo "Param: $param_name; Value: $param_val<br />\n";
}

더 이상 사용되지 않으며 superglobals에 직접 액세스하고 싶지 않습니다 (내가 생각하는 PHP 5.5 이후?)

모든 최신 IDE는 다음을 알려줍니다.

수퍼 글로벌에 직접 액세스하지 마십시오. (예 : 일부 필터 기능을 사용 filter_input)

솔루션의 경우 모든 요청 매개 변수를 가져 오려면 다음 메소드를 사용해야합니다. filter_input_array

To get all params from a input method use this:

$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...

Now you can use it in var_dump or your foreach-Loops

What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULL and that is correct.

If you need to get all Input params, comming over different methods, just merge them like in the following method:

function askForPostAndGetParams(){
    return array_merge ( 
        filter_input_array(INPUT_POST), 
        filter_input_array(INPUT_GET) 
    );
}

Edit: extended Version of this method (works also when one of the request methods are not set):

function askForRequestedArguments(){
    $getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
    $postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
    $allRequests = array_merge($getArray, $postArray);
    return $allRequests;
}

So, something like the $_POST array?

You can use http_build_query($_POST) to get them in a var=xxx&var2=yyy string again. Or just print_r($_POST) to see what's there.


Why not this, it's easy:

extract($_POST);

Using this u can get all post variable

print_r($_POST)

참고URL : https://stackoverflow.com/questions/8207488/get-all-variables-sent-with-post

반응형