View Full Version : Calling a php script from javascript onclick

11-01-2012, 06:13 PM
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...


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!");
// callback handler that will be called on error
error: function(jqXHR, textStatus, errorThrown){
// log the error to the console
"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


And here is the php script i want to call

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

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,
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"; }

$res = json_decode($json, true);

var_dump($res); // do something with $res

11-01-2012, 07:48 PM
the php script is working so its not thatTry the JavaScript forum

11-01-2012, 07:56 PM
console.log() kills things for me in normal web page. Think that's cause I'm not running this in firebug.

Two things in the php is the final } does not belong
Could not resolve host: urlblblbll; Host not found

11-03-2012, 10:08 PM
dont you need to put onClick not sclick in the first line of javascript