MyBB Community Forums

Full Version: Jquery / Ajax in plugin ACP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
should be a simple problem but I'm struggling to find an example of what to do...

I have a plugin i am working on to prevent spammy signups on our little message board (based on chkproxy and security log, but significantly modified) -  it parses the API data from Stop Forum Spam and ipintel and then does some calculations to give a pass fail for the signup.  That side is all working fine...

It also applies the same tests to existing user accounts to find the spammers.  

In the ACP i have a form which summarises a list of the user accounts that have failed the test from there I have a couple of actions I would like to implement.  I suspect both will use much the same code

1.  Send a report to SFS just by clicking on the text link ( perhaps with a 'Are you sure?" ) onclick
2.  trigger an "Ignore" function for users deemed to be actually OK , which will simply add a value to a database table

Both of these I would like to use jquery to do the leg work, without having to do a form or reload the page if possible...
I am struggling to work out where the jquery function might be placed - I am familiar with PHP and jquery in this respect, but applying it within the plugin ACP form has got me...
I see that jquery is already loaded in the headers

An example of the form - SFS reporting on the far right,  Ignore button in the Action field.  

[attachment=46040]

Doesn't need to alter the form in anyway( removing the entry etc) - I simply want to make a call to a php file on the server with a click on the text link using jquery or similar, without reloading the page - after that I should be good

Appreciate any guidance you can give.

Cheers
Just echo out your Javascript immediately prior to your call to $page->output_footer();, something like this:

echo <<<EOF
<script type="text/javascript">
// <![CDATA[
$(function() {
    /* TODO: set the `click` handler for the element in which we're interested, and in that handler perform our funky ajax call. */
});
// ]]>
</script>
EOF;

$page->output_footer();

So, yep, you were right - it really is simple!

ETA: Looking in the DefaultPage class, it seems that an alternative is to set (append to) its $extra_header property prior to your call to its output_header() method. So, the above code would instead look like this and occur higher up in your script:

$page->extra_header .= <<<EOF
<script type="text/javascript">
// <![CDATA[
$(function() {
    /* TODO: set the `click` handler for the element in which we're interested, and in that handler perform our funky ajax call. */
});
// ]]>
</script>
EOF;

$page->output_header('TODO: Your page title goes here');
thanks for that, I will check that out tomorrow - it certainly looks like the answer to my little problem. Cheers

Thanks for the help, the first option is doing the job perfectly.
Further to this problem

the jquery is working and I can pass the id numbers etc through to the target php file and echo them back, but the php has no access to the $db class or MYBB stuff - error I get in console is this 
Handler for `click` called.ign::12480
index.php?module=tools-sentinel&action=review:202 <br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function update_query() on null in C:\xampp\htdocs\tmmb\inc\plugins\sentinel\sentinel_actions.php:41
Stack trace:
#0 {main}
  thrown in <b>C:\xampp\htdocs\tmmb\inc\plugins\sentinel\sentinel_actions.php</b> on line <b>41</b><br />


presently the file i'm calling from jquery is in
 inc/plugins/sentinel/sentinel_actions.php
but it is being called from within the ACP because I seem to access it in the admin folder

admin plugin  file is  inc/plugins/sentinel.php



I have added a 
global $db,$lang, $session, $mybb;

but this seems to make no difference.

I'm guessin there is some form of headers or initialisation that needs to happen for the file to be recognised either by mvbb or as part of the plugin or both...  - the  $db->update_query is where it is failing

this is the sentinel_actions file
// This kind of seems pointless ?
define('IN_MYBB', 1);

// Don't allow direct initialization.
if (! defined('IN_MYBB')) {
	die('Nope.');
}


global $db,$lang, $session, $mybb;



if(isset($_POST['theid']) && !empty($_POST['theid'])) { $theid = $_POST['theid'];}
	
if(isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action'];}
   
	
if ($theid  && $action){	
    switch($action) {
        case 'ign' : 
			
			// UPDATE sentinel_users tocheck field if more IPs exist to check for user - 1=more 0=no more - user IPs remaining
			
			$db->update_query('sentinel_users', array("lastchecked" => TIME_NOW, "reviewedok" =>1 ), "`userid` = " . $theid );
			echo "IGN--" . $theid; 

			
			
			break;
        case 'sfs' : 
			echo "SFS--" . $theid;
			// SFS MUST ALLOW FOR 
			// username and evidence encoded to utf-8
			// all fields url encoded 
			// MUST have been email verified account
			/*
				$ch = curl_init('https://www.stopforumspam.com/add');
					if ($ch) {
					  // if your curl version does not support SNI and you must use HTTPS then you may have to disable verify with 
					  // curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
					  curl_setopt ($ch, CURLOPT_POST, 1);
					  curl_setopt ($ch, CURLOPT_POSTFIELDS, "ip_addr=IPADDRESS&evidence=XXXXXXXXXX&email=EMAILADDRESS&api_key=ZZZZZZZZZZZZZZZ");
					  curl_exec ($ch);
					  curl_close ($ch);
					}

*/
			break;
        // ...etc...
    }
} else {
	echo "Sorry AJAX has failed you!";
}

Appreciate any help
(2023-06-14, 03:37 AM)king1 Wrote: [ -> ]presently the file i'm calling from jquery is in
inc/plugins/sentinel/sentinel_actions.php

Usually in this scenario you'd create an admin module file in admin/modules/tools/sentinel.php, and make MyBB aware of that module (file) via code in your inc/plugins/sentinel.php file, by hooking in to admin_tools_action_handler and admin_tools_permissions.

Then, your call from jQuery would be to that module with an appropriate query parameter to indicate that it's an AJAX call that should be handled appropriately - based on the code you supplied, maybe something like:

admin/index.php?module=tools-sentinel&action=ign

and

admin/index.php?module=tools-sentinel&action=sfs

In that case, MyBB core handles the initialisation of the global variables like $db.
(2023-06-14, 04:27 AM)Laird Wrote: [ -> ]
(2023-06-14, 03:37 AM)king1 Wrote: [ -> ]presently the file i'm calling from jquery is in
inc/plugins/sentinel/sentinel_actions.php

Usually in this scenario you'd create an admin module file in admin/modules/tools/sentinel.php, and make MyBB aware of that module (file) via code in your inc/plugins/sentinel.php file, by hooking in to admin_tools_action_handler and admin_tools_permissions.

Then, your call from jQuery would be to that module with an appropriate query parameter to indicate that it's an AJAX call that should be handled appropriately - based on the code you supplied, maybe something like:

admin/index.php?module=tools-sentinel&action=ign

and

admin/index.php?module=tools-sentinel&action=sfs

In that case, MyBB core handles the initialisation of the global variables like $db.

right so if i'm reading that correctly...

I already have admin_tools_action_handler and admin_tools_permissions hooks and a functioning admin area...

Rather than having a seperate PHP file for this i should just modify the existing  admin\modules\tools\sentinel.php, break down the switch statement to individual  sections as are the displayed tabs, but without all the displayed tables etc...

Something like this? 

if($mybb->input['action']== 'sfs') {
}
if($mybb->input['action']== 'ign') {
}
Yes - at least, that's the way I've done it for one of the plugins I wrote, so I know it works. There might be other approaches, but that one will get the job done for you.