...

View Full Version : Calling a php script from javascript onclick



shile
11-01-2012, 05: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...




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

});

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,
"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
}

>ssp-cdr<
11-01-2012, 06:48 PM
the php script is working so its not thatTry the JavaScript forum

sunfighter
11-01-2012, 06: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
and
Could not resolve host: urlblblbll; Host not found

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum