2012-11-22 4 views
0

자바 스크립트로 생성 된 버튼을 클릭 할 때 PHP 스크립트를 실행하고 싶습니다. jquery 아약스로 시도했지만 아무 일도 일어나지 않았습니다 ... 도움이 필요하십니까? PHP 스크립트는자바 스크립트에서 PHP 스크립트 호출하기 onclick

Ajax 호출 ...이 아니 즉, 내가이 아약스 호출에서 뭔가를 놓친 거지 같아 그렇게하고있다

$(".formBtn").click(function(){ 

    $.ajax({ 
     url: "script to call", 
     type: "post", 

     // callback handler that will be called on success 
     success: function(response, textStatus, jqXHR){ 
      // log a message to the console 
      console.log("Hooray, it worked!"); 
      alert("Working!"); 
     }, 
     // callback handler that will be called on error 
     error: function(jqXHR, textStatus, errorThrown){ 
      // log the error to the console 
      console.log(
       "The following error occured: "+ 
       textStatus, errorThrown 
      ); 
     }, 
     // callback handler that will be called on completion 
     // which means, either on success or error 
     complete: function(){ 
      // enable the inputs 
      $inputs.removeAttr("disabled"); 
     } 
    }); 

}); 

PHP의 srcipt

$api_key = 'apikey'; 
    $project_id = 'projectid'; 
    $phone_id = 'phoneid';  
    $to_number = 'number'; 
    $content = 'content'; 


    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, 
     "urlblblbll"); 
    curl_setopt($curl, CURLOPT_USERPWD, "{$api_key}:"); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
     'content' => $content, 
     'phone_id' => $phone_id, 
     'to_number' => $to_number, 

    )));   



    $json = curl_exec($curl);  
    if ($err = curl_error($curl)) { echo "$err\n"; }  
    curl_close($curl);  

    $res = json_decode($json, true);   

    var_dump($res); // do something with $res 
    } 
+0

콘솔에 오류가 있습니까? –

+0

아니요 빈 페이지로 돌아 가기 –

+0

'curl_exec'의 반환 값은 true/false입니다. 그래서 나는 그 문제가 그 때문이라고 생각합니다. –

답변

0
$result = curl_exec($curl); 
if($result) 
{ 
    $json['result'] = 'true'; 
} 
else 
{ 
    $json['result'] = 'false'; 
} 
if ($err = curl_error($curl)) { echo "$err\n"; }  
curl_close($curl);  

$res = json_decode($json, true); 

시도해 볼 수 있습니까?

0

아약스의 데이터는 PHP 파일의 출력을 기반으로하기 때문에 PHP 페이지에 뭔가를 표시해야한다고 생각합니다.

관련 문제