Site icon Hip-Hop Website Design and Development

Call a PHP Function with Multiple Parameters that is outside the AJAX Handler

Q: What is the correct way to call a PHP Function with multiple parameters from the AJAX Handler?
Q: Am I calling the php function correctly?
Q: Is there an easier way to do this?


Page Element
The user clicks a button.

Action:
The click will call php code to connect to an external server using the following params:

  1. User Name 2. IP Address 3. Port 4. Public Key 5. Service

Purpose:
Send an vote string (from host A) to an external Gaming server (host B).
I do not need to update any WordPress content. Note: I own both of these servers.

WordPress Version: 5.4.1 PHP Version on Server 7.3

Intially, I had my custom php code in a separate file “/wp-contents/plugins/my-plugin/votifier.php” I was trying to call my functions in this file from the AJAX Handler.

  1. MyPlugin.php
  2. sendvote.js
  3. votifier.php (I ended up moving the code from here into 1.MyPlugin.php above)

I made sure the custom plugin is activated.

I have WordPress in Debug mode. i.e. Debug mode is true.

The Button

<div id="frm_field_61_container">
<button type="button">Try it</button>
</div>

WordPress JQuery with AJAX (sendvote.js)

NOTE: This code is in a separate file in my plugins folder.
Filename sendvote.js

jQuery(document).ready( function($) {
 $("#frm_field_61_container").click(function(){
       // e.preventDefault();  // used to cancel forms submit action.


    jQuery.ajax({

        url : myAjax.ajaxurl,

        type : 'POST',

        dataType : 'json',

        data : {
             'action'   :'my_vote_count'
            ,'username' :$.trim($('#field_9gma9').val())
            ,'key'      :$.trim($('#field_yjr62').val())
            ,'ip'       :$.trim($('#field_973sr').val())
            ,'port'     :$.trim($('#field_q9ajo').val())
            ,'service'  :'Votifier'
            ,'nonce'    :'votifier_nonce_key'
        },

        success: function(data) {

                    console.log(data);
                },

        error: function(errorThrown){
                    console.log(errorThrown);
                }

      });   
   });
});

/* ===== WP_ENQUEUE SCRIPTS ==== */
NOTE: This code is in a separate file with the file-name MyPlugin.php.

add_action( 'wp_enqueue_scripts', 'my_script_enqueuer' );
function my_script_enqueuer() {

   wp_enqueue_script( 'my_voter_script',  plugins_url('sendvote.js', __FILE__), array( 'jquery'),'1.0', true);

   wp_localize_script( 'my_voter_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

}

/* ===== My AJAX Handler ==== */
NOTE: This code is also in the “MyPlugin.php” file.

add_action( 'wp_ajax_nopriv_my_vote_count', 'my_ajax_handler' );  
add_action( 'wp_ajax_my_vote_count', 'my_ajax_handler');
function my_ajax_handler() {

//check_ajax_referer('votifier_nonce_key'); 

$user = (isset($_POST['username'])) ? $_POST['username'] : 'Missing User';
$key = (isset($_POST['key'])) ? $_POST['key'] : 'Missing Key';
$ip = (isset($_POST['ip'])) ? $_POST['ip'] : 'Missing ip Address';
$port = (isset($_POST['port'])) ? $_POST['port'] : 25566;
$service = (isset($_POST['service'])) ? $_POST['service'] : 'Missing Service';

define( 'PUBLIC_KEY_FORMAT', "-----BEGIN PUBLIC KEY-----n%sn-----END PUBLIC KEY-----" );

       /* XXXX Custom Code Here XXXX */
       // The function sendVote() was in a separate file. 
       // The file votifier.php contained the sendVote() function.  
       // note:  The functions were good and working outside WordPress
       // i.e. I fully tested them before trying to call the function from here.

       // I gave up trying and just 
       // put all the PHP code here and removed all functions.
       // I removed -- echo sendVote($p1,$p2,$p3,$p4,$p5); -- from this area.
       // when I did that the everything started to work.

       //  I know WordPress functions work here.
       //  Why can't I call my own custom function from a separate php file?

       //  Do I need to add an include statement?

wp_send_json_success($user . $fkey . $ip . $port . $service . $crypted);
wp_die(); // All ajax handlers die when finished

}

Its working now
One answer is …

put all the PHP code in the AJAX handler instead of trying to call an external PHP function.