从javascript onclick调用php脚本

时间:2012-11-22 04:24:40

标签: php ajax

当我点击javascript生成的html中的按钮时,我想执行一个PHP脚本,我正在尝试使用jquery ajax,我这样做但没有任何反应...任何帮助请问?php脚本正在工作所以它不是那个,我想我在这个ajax电话中缺少一些东西......

调用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
    } 

2 个答案:

答案 0 :(得分:0)

我相信你需要在php页面上回应一些东西,因为ajax的数据是基于php文件的输出。

答案 1 :(得分: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);  

你能试试吗?