Hiya,
I'm trying to work on a plugin currently and was wondering how to do this:
I've generated a submit button, such as below:
$buttons[] = $form->generate_submit_button("Try");
I need to create a db query when the form is submitted.
How could I do this?
Check beforehand to see if $mybb->request_method == "post" to see if it's been submitted and then create the query using $db->query().
Thanks Jammer
One other question, I'm using this following code:
$form_container->output_row("Comment", "", $form->generate_text_area('rec_comment', '', array('id' => 'rec_comment')), 'rec_comment');
to generate a text area, and the following code:
if ($mybb->request_method == "post") {
$db->query("INSERT INTO `table_name` VALUES (NULL, '{$mybb->settings['rec_uid']}', '{$mybb->settings['from_uid']}', '{$mybb->settings['rec_post']}', '{$mybb->settings['rec_amount']}', '{$mybb->settings['unix_time']}', '{$mybb->settings['rec_comment']}')");
}
to check if the form has been submitted, and if it has, create the db query.
I need to grab the result from the text area (rec_comment) and that will be the value rec_comment in the db query.
Cheers
$mybb->input['rec_comment'] will have the contents of it. I also suggest escaping all of the settings that you insert along with the comment to prevent sql injection. This can be done by doing $db->escape_string($mybb->input['rec_comment']), along with the other settings you insert. I would also use $form->generate_hidden_field("my_post_key", $mybb->post_code) in your form and also check for verify_post_check($mybb->input['my_post_key']) when you check the request method.
Edit:
You'll only have to do the post key thing if you're creating your own page and not extending one that already has the check.
Thanks Jammer
I now have this:
if ($mybb->request_method == "post") {
$db->query("INSERT INTO ".TABLE_PREFIX."reputation VALUES (NULL, '$mybb->input['rec_uid']', '$mybb->input['from_uid']', '$mybb->input['rec_post']', '$mybb->input['rec_amount']', '$mybb->input['unix_time']', '$mybb->input['rec_comment']')");
}
I'm now getting an SQL error when I submit the form:
MyBB has experienced an internal SQL error and cannot continue.
SQL Error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rec_uid']', 'Array['from_uid']', 'Array['rec_post']', 'Array['rec_amount']', 'Ar' at line 1
Query:
INSERT INTO mybb_reputation VALUES (NULL, 'Array['rec_uid']', 'Array['from_uid']', 'Array['rec_post']', 'Array['rec_amount']', 'Array['unix_time']', 'Array['rec_comment']')
Cheers
Make variables and make them added to database.
For example rather than adding $db->escape_string($mybb->input['rec_comment']), define it as variable like:
$rec_comment = $db->escape_string($mybb->input['rec_comment']);
and while adding it to table, just use $rec_comment variable, that should fix it.
Change above code to;
if ($mybb->request_method == "post")
{
$db->query("INSERT INTO ".TABLE_PREFIX."reputation VALUES (NULL, $mybb->input['rec_uid'], $mybb->input['from_uid'], $mybb->input['rec_post'], $mybb->input['rec_amount'], $mybb->input['unix_time'], $mybb->input['rec_comment'])");
}
Thanks guys - got it working perfectly now !
One other question (again
)
How do I add submenus like this:
http://i.imm.io/xyr1.png
Thanks again guys!
$sub_tabs['active_tab'] = array(
'title' => "Active Tab",
'link' => "index.php",
'description' => "Description"
);
$sub_tabs['other_tab'] = array(
'title' => "Other Tab",
'link' => "index.php"
);
$page->output_nav_tabs($sub_tabs, 'active_tab');
Add that after you output the header.
How and where would I create the actions?
Cheers again, +rep all who have helped