MyBB Community Forums

Full Version: Creating a Shoutbox Plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I'm creating my own Shoutbox plugin. I haven't done much so far and would like to use this as a learning process. I am stuck at one point and aren't too sure where I've gone wrong.

Here is my code
<?php
/*
myBBChangebook
by: cupOcappuccino
http://www.timechangers.com/changebook
Copyright 2011  ChangeBook
*/

if (!defined("IN_MYBB"))
{
  die("You Cannot Access This File Directly.");
}

$plugins->add_hook('pre_output_page', 'mybbchangebook');
//$plugins->add_hook("forumdisplay_start", "mybbchangebook");

function mybbchangebook_info()
{
	return array(
		"name"		      	=> "myBBChangebook",
		"description"		=> "Adds Changebook to the forum",
		"website"		    => "http://www.timechangers.com/changebook",
		"author"		    => "cupOcappuccino",
		"authorsite"		=> "http://www.timechangers.com/changebook",
		"version"		    => "1.0",
		"guid"        		=> "",
		"compatibility"		=> "16*"
		);
		
}


function mybbchangebook_install()
{
	global $db;

	$db->write_query("
		CREATE TABLE ".TABLE_PREFIX."changebook (
			`id` int(10) unsigned NOT NULL auto_increment,
			`message` text NOT NULL,
			`time` varchar(100) NOT NULL,
			`userid` int(10) NOT NULL,
			PRIMARY KEY (id)
		) Type=MyISAM;
	");
}

function mybbchangebook_is_installed()
{
	global $db;

	if($db->table_exists('changebook'))
	{
		return true;
	}

	return false;
}

function mybbchangebook_uninstall()
{
	global $db;
	
	$db->drop_table('changebook');
}

function mybbchangebook_activate()
{
  global $db;
  
  $mybbchangebook_group = array(
      'gid'    			=> 'NULL',
      'name'  			=> 'mybbchangebook',
      'title'      		=> 'myBBChangebook',
      'description'    	=> 'Settings For myBBChangeBook',
      'disporder'    	=> "1",
      'isdefault'  		=> 'no',
  );
    
  $db->insert_query('settinggroups', $mybbchangebook_group);
  $gid = $db->insert_id(); 
  
  $mybbchangebook_setting = array(
      'sid'             => 'NULL',
      'name'            => 'enabled_mybbchangebook',
      'title'           => 'Do you want myBBChangebook Enabled?',
      'description'     => 'If Yes People Can Use myBBChangebook, If No People Cannot Use myBBChangebook.',
      'optionscode'     => 'yesno',
      'value'           => '1',
      'disporder'       => 1,
      'gid'             => intval($gid),
  ); 
  
  $db->insert_query('settings', $mybbchangebook_setting);
  rebuild_settings();
   
}

function mybbchangebook_deactivate()
  {
  global $db;
  
  //Delete the settings
  $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('enabled_mybbchangebook')");
  $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='mybbchangebook'");
  
  rebuild_settings();

	//Delete templates
	$deletetemplates = array('mybbchangebook_body');

	foreach($deletetemplates as $title)
	{
		$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='".$title."'");
	}
	
}

function mybbchangebook(){
  global $db, $mybb, $templates, $lang, $theme, $collapsed, $collapsedimg;
  
  if ($mybb->settings['enabled_mybbchangebook'] == 1){


$template_posts = "
<table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder\">
<thead>
<tr>
<td class=\"thead\">
<strong>Changebook</strong>
</td>
</tr>
</thead>
</table>
<br />";

$template_body = "<tr>
<td class=\"tcat\">
</td>
</tr>
<tr>
<td class=\"trow1\">
<strong>Response:</strong> <input type=\"text\" name=\"mybbchangebook_message\" id=\"mybbchangebook_message\" size=\"40\" maxlength=\"200\" onKeyPress=\"mybbchangebook_add_enter(event);\" /> <input type=\"submit\" name=\"submit\" onclick=\"mybbchangebook_add()\" value=\"Shout\" />
</td>
</tr>";

	echo $template_posts;
	echo $template_body;
  
  }
}

function mybbchangebook_add()
{
	if($mybb->input['action'] == "mybbchangebook_add") {
	$insert_shout = array(
		'post' => addslashes($mybb->input['mybbchangebook_message']),
		'time' => time(),
		'userid' => $mybb->user['uid']
	);
	
	$db->insert_query(TABLE_PREFIX."changebook", $insert_shout);
}
}

?>

What my problem, now, is that my "Shout" button is doing nothing. I can only assume that my
onclick=\"mybbchangebook_add()\"
isn't really doing what I want it to do. What should go there in the place of the mess I've made?

What have I done wrong? Can anyone please help?
You're trying to call a PHP function with an onclick call, it doesn't work like that Undecided You put javascript in onclick, so you'd call a javascript function, which would contain an AJAX request, which would then do the PHP you want to do.
(2011-02-02, 02:16 PM)MattRogowski Wrote: [ -> ]You're trying to call a PHP function with an onclick call, it doesn't work like that Undecided You put javascript in onclick, so you'd call a javascript function, which would contain an AJAX request, which would then do the PHP you want to do.

Oh. That was stupid. lol
Thanks for pointing that out for me I'll let you know how I go.

I've lost the plot