Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want to execute a php script when i click on a button in html generated by javascript,i was trying with jquery ajax and i did this but nothing happens...any help pls?the php script is working so its not that,i guess i am missing something in this ajax call...

Ajax call

$(".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
    } 
share|improve this question
Is there any error in the console? – Nandakumar V Nov 22 '12 at 4:30
No , Return with a blank page – user1838449 Nov 22 '12 at 4:32
The return value of curl_exec is true/false. So i think issue is because of that. – Nandakumar V Nov 22 '12 at 4:41
Not true - he has added the RETURN_TRANSFER parameter, so the return should be the result of the request – shadow Nov 22 '12 at 5:30
Why are you var_dumping res? You should probably be echoing it so that the browser can read the return. – shadow Nov 22 '12 at 5:43

2 Answers

up vote 0 down vote accepted
 $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);  

Can you try this.

share|improve this answer

I believe you need to echo something on the php page, because ajax's data is based on the output from the php file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.