Page 1 of 1

Calling a php script from javascript onclick

Posted: Thu Nov 01, 2012 11:18 am
by shile
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

[text]$(".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");
}
});

});[/text]

php script

Code: Select all

$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
    }  

Re: Calling a php script from javascript onclick

Posted: Thu Nov 01, 2012 4:03 pm
by jraede
What do you see in the console when you click the button?

Re: Calling a php script from javascript onclick

Posted: Fri Nov 02, 2012 7:45 am
by social_experiment
You might also want to check that there isn't any problems on the php page (syntax errors, etc) as this will stop the script from working even if it is called

Re: Calling a php script from javascript onclick

Posted: Fri Nov 02, 2012 1:05 pm
by shile
the php script is working perfectly on its own,but i cant call it with ajax...i dont know what is wrong and why its not working...

Re: Calling a php script from javascript onclick

Posted: Sun Nov 04, 2012 3:53 pm
by social_experiment
You could check the web console (if you use FireFox) or check for any errors generated by the javascript.