2014-07-25 2 views
2

CodeIgniter를 사용하고 있으며 내 js 코드가 "보기"에 있으며 컨트롤러에 값을 전달하려고합니다.코드 이그니 터에서 php에 자바 스크립트 배열 전달

var storage =[]; 

function something() 
{ 

storage.push('the value'); 

} 

이제 저장소 배열을 PHP로 전달하는 더 좋은 방법을 원합니다. 미리 감사드립니다. 이 코드는 별도의 폴더에 있기 때문에 작동하지 않습니다.

$.ajax({ 
    url: 'yourPHPFile.php', 
    type: 'POST', 
    data: {data:storage.toString()}, 
    success: function(result) { 
      // handle your success 
    }, 
    error: function() { 
     // alert("error"); 
    } 
}); 
+0

이 목적으로 JSON을 사용할 수 있습니다. –

+1

''스토리지 배열을 my php "에 넣을 수 있습니까? 다소 광범위하고 모호합니다. – Darren

+0

그걸 알고 있지만 문법을 모르겠다. – CodeSlayer

답변

3

사용 JQuery와 데이터

$.ajax({ type: "POST", 
      url: "Filename.php", 
      data: storage,//no need to call JSON.stringify etc... jQ does this for you 
      success: function(resopnse) 
      {//check response: it's always good to check server output when developing... 
       }}); 

그리고 당신의 PHP의를 게시 ... 자바 스크립트

JSON.stringify(array) 

를 작성하고 사용하여 검색 할 파일

$array=json_decode($_POST['jsondata']); 
3

가장 쉬운 방법은 jQuery입니다.

당신은

$data = $_POST['data']; 
echo $data; 

$datathe value를 포함, 당신의 PHP 코드 수행에, 서버 측에서 $.ajax

$.ajax({ 
    url: 'yourPHPFile.php', 
    type: 'POST', 
    data: {data:storage.toString()}, 
    success: function(result) { 
      // handle your success 
    }, 
    error: function() { 
     // alert("error"); 
    } 
}); 

사용하여 server에 데이터를 전송하여 그것을 할 수 있습니다.

1

를 사용하여 JSON은

$array=json_decode($_POST['jsondata']); 
관련 문제