|
 |
|
Codewalkers Forums
> PHP Related
> PHP Coding
|
Calling a php script from javascript onclick
Discuss Calling a php script from javascript onclick in the PHP Coding forum on Codewalkers. Calling a php script from javascript onclick Having problems with a PHP script you are coding? This is the place to get help!
|
|
|
|
 |
|
|
|
|

Codewalkers Forums Sponsor:
|
|
|

November 1st, 2012, 11:15 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 12
Time spent in forums: 3 h 8 m 24 sec
Reputation Power: 0
|
|
|
Calling a php script from javascript onclick
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..
Code:
$(".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 this is the php script
PHP Code:
$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 }
|

November 2nd, 2012, 10:10 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,362

Time spent in forums: 4 Weeks 1 Day 23 h 41 m 31 sec
Reputation Power: 8
|
|
|
Are you getting any errors? have you tried running the php on its own?
__________________
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.
|

November 2nd, 2012, 01:03 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 12
Time spent in forums: 3 h 8 m 24 sec
Reputation Power: 0
|
|
|
yes i have,and it works fine...
|

November 3rd, 2012, 07:22 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,362

Time spent in forums: 4 Weeks 1 Day 23 h 41 m 31 sec
Reputation Power: 8
|
|
|
you need to provide a bit more information, ie exactly what you are doing, what you are expecting... posting 2 bits of code formatted terribly is hard to trouble shoot.
|

November 3rd, 2012, 07:47 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 12
Time spent in forums: 3 h 8 m 24 sec
Reputation Power: 0
|
|
|
this php script sends sms message and i want when a button is clicked to send that message,but the problem is that button is already linked with a submit form so i have to do this with ajax...
|

November 4th, 2012, 01:31 PM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,362

Time spent in forums: 4 Weeks 1 Day 23 h 41 m 31 sec
Reputation Power: 8
|
|
|
In your ajax, i dont see where you passing the post variables?
|

November 4th, 2012, 02:38 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 6 m 20 sec
Reputation Power: 0
|
|
|
Only one click allowed!
If the button is type = "submit" that is all you will get! only one click event procedure will be called and that will be the submit.
You should not need a submit form and an ajax script... (actually you can't have both) handle the whole thing with ajax!
|

November 5th, 2012, 04:39 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Galway
Posts: 1,362

Time spent in forums: 4 Weeks 1 Day 23 h 41 m 31 sec
Reputation Power: 8
|
|
Quote: | Originally Posted by Sleeping Troll If the button is type = "submit" that is all you will get! only one click event procedure will be called and that will be the submit.
You should not need a submit form and an ajax script... (actually you can't have both) handle the whole thing with ajax! |
if there is no need for a form submission, type="button" would be better as it will not submit the form.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|