INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

execute php on button click

execute php on button click

(OP)
I am VERY new to WordPress but am a fast learner.

In the past I have used AJAX to execute php code on a button click. As you probably know, that requires extra php and js code as well as a lengthy path to success. It their an easier way to do this in WordPress? Some sort of hook?

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer

RE: execute php on button click

sure.

1. output your javascript in an admin_head hook
2. then hook your receiving script in to wp_ajax_xxxxx where xxx is the name of the action. 3. the second argument to the hook is the name of the php function that executes.
parameter that you pass in the ajax data (ie. in the url action=xxxx&...)
4. point your ajax request at ajaxurl (on the admin side, you will have to code this yourself on front-facing pages)
5. for front-facing ajax use wp_ajax_nopriv_xxxx hook.

for more info check out the codex : http://codex.wordpress.org/AJAX_in_Plugins

RE: execute php on button click

(OP)
I understand most of that. The codex page you mentioned cleared some things up but I still don't understand how to link the action to an element event.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer

RE: execute php on button click

use jQuery.

CODE

$('document').ready(function(){ $('id of button element').on('click', function (e){ e.preventDefault(); var data = { action: 'myAjaxAction', othervariable: 'othervalue' };//.. $.getJSON(ajaxurl, data, function (d){ //do something with the return values }); }); )};

RE: execute php on button click

(OP)
Makes sense. Thanks, jpardie

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close