• 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>

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter cryptoframegames

    (@cryptoframegames)

    Think I may be able to get somewhere with this link here, though I’m still not sure the correct path to use for the PHP Snippet.

    Plugin Author Shea Bunge

    (@bungeshea)

    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.