Title: Calling PHP Snippet from on-page Ajax
Last modified: July 3, 2021

---

# Calling PHP Snippet from on-page Ajax

 *  [cryptoframegames](https://wordpress.org/support/users/cryptoframegames/)
 * (@cryptoframegames)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/calling-php-snippet-from-on-page-javascript/)
 * Hi, I’ve been unable to find an example of how to call a PHP Snippet from an 
   on-page Ajax function. Doesn’t look like shortcodes are an option yet. Could 
   anyone point me in the right direction? Any helps appreciated.
 *     ```
       $('#CryptoQR')
           .ajaxForm({
               url : 'myscript.php',
               dataType : 'json',
               success : function (response) {
       		document.getElementById("QRCode").src = "data:image/png;base64," + response; //your ByteArray as Base64
               }
           });
       </script>
       ```
   
    -  This topic was modified 4 years, 11 months ago by [cryptoframegames](https://wordpress.org/support/users/cryptoframegames/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcalling-php-snippet-from-on-page-javascript%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [cryptoframegames](https://wordpress.org/support/users/cryptoframegames/)
 * (@cryptoframegames)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/calling-php-snippet-from-on-page-javascript/#post-14621678)
 * Think I may be able to get somewhere with [this link here](https://wordpress.stackexchange.com/questions/313643/how-to-call-a-php-function-with-ajax-when-the-user-clicks-a-button),
   though I’m still not sure the correct path to use for the PHP Snippet.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/calling-php-snippet-from-on-page-javascript/#post-14748351)
 * You would want to create a function hooked to `wp_ajax_$ACTION` and `wp_ajax_nopriv_
   $ACTION` to handle the request, and then set the correct parameters within the
   jQuery call:
 *     ```
       $handler = function () {
       	$result = [
       		'qrcode' => 'somedata',
       	];
   
       	wp_send_json_success( $result );
       };
   
       add_action( 'wp_ajax_generate_qr_code',  $handler );
       add_action ('wp_ajax_nopriv_generate_qr_code', $handler );
   
       add_action( 'wp_head', function () { ?>
       <script>
       $('#CryptoQR').ajaxForm({
       	url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>',
       	dataType: 'json',
       	type: 'POST',
       	data: {
       		action: 'generate_qr_code'
       	},
       	success: function (response) {
       		document.getElementById("QRCode").src = "data:image/png;base64," + response.qrcode; //your ByteArray as Base64
               }
       });
       </script>
       <?php } );
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Calling PHP Snippet from on-page Ajax’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 2 replies
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [4 years, 10 months ago](https://wordpress.org/support/topic/calling-php-snippet-from-on-page-javascript/#post-14748351)
 * Status: not a support question