Calling a php script from javascript onclick

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shile
Forum Newbie
Posts: 15
Joined: Thu Oct 04, 2012 6:45 pm

Calling a php script from javascript onclick

Post 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
    }  
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Calling a php script from javascript onclick

Post by jraede »

What do you see in the console when you click the button?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Calling a php script from javascript onclick

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
shile
Forum Newbie
Posts: 15
Joined: Thu Oct 04, 2012 6:45 pm

Re: Calling a php script from javascript onclick

Post 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...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Calling a php script from javascript onclick

Post by social_experiment »

You could check the web console (if you use FireFox) or check for any errors generated by the javascript.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply