Skip to:
Content
Pages
Categories
Search
Top
Bottom

Problem using a form in a plugin


  • Null
    Member

    @null

    Hi,

    I have a form to update a plugin’s settings. The problem is, it seems to update on it’s own as well. Seems randomly and appears mostly when surfing through the admin pages. Whats wrong with my form and how to fix it?

    The form:

    // Update portal
    function update_bbportal() {
    global $bbdb;

    $bbdb->query("UPDATE <code>$bbdb->portal</code> SET pforum_id = '". $_POST['forum_id'] ."', number_of_topics = '". $_POST['number_of_topics'] ."'");
    }

    // Show form
    function bbportal_form() {
    ?>
    <h2><?php _e('Portal Management'); ?></h2>
    <h3><?php _e('Portal settings'); ?></h3>
    <form action="" method="post">
    <table>
    <tr><th scope="row"><label for="forum_id"><?php _e('Where do you want to pull the topics from?'); ?></th>
    <td><?php forum_dropdown(); ?></label></td>
    </tr>
    <tr><th scope="row"><?php _e('Number of topics on the portal:'); ?></th>
    <td><input type="text" name="number_of_topics" id="number_of_topics" /></td>
    </tr>
    </table>
    <p class="submit alignleft"><input type="submit" onclick="<?php update_bbportal(); ?>" value="Submit" /></p>
    </form>
    <?php
    }

    I also no longer want to use the created and used database table “portal”, but use the “bb_update_option” and “bb_get_option”. How to do this?

    Thx

Viewing 5 replies - 1 through 5 (of 5 total)

  • Sam Bauers
    Participant

    @sambauers

    I think you are misunderstanding how PHP is used to process forms. You can’t call a PHP function using an “onclick” event.

    PHP is a server side pre-processor, not a client side scripting language. The form needs to be posted, then you need to somehow trigger the php function on the recipient page. In bbPress, this is done by hooking into the API.

    Read this… http://www.php.net/manual/en/tutorial.forms.php

    Then I suggest you download and copy the methods used in another plugin that does this. No one will mind you copying their methods. You can feel free to adapt from any of my plugins that do this. “LDAP authentication” does it, so does “Restrict registration”. The code for handling admin pages is at the bottom of both plugins. These will also give you a clue as to how to implement the bb_*_option functions.


    Null
    Member

    @null

    Ok thx Sam, going to take a look at that


    Null
    Member

    @null

    Well got the bb_get_option working, but not my form. Did take a look at yours and several others, but I can’t seem to see the logic in them. I’ve made some changes but it aint updating at all anymore.

    This is what i have now:

    // Show form
    function bbportal_form() {
    ?>
    <h2><?php _e('Portal Management'); ?></h2>
    <h3><?php _e('Portal settings'); ?></h3>
    <form action="" method="post">
    <table>
    <tr><th scope="row"><label for="forum_id"><?php _e('Where do you want to pull the topics from?'); ?></th>
    <td><?php forum_dropdown(); ?></label></td>
    </tr>
    <tr><th scope="row"><?php _e('Number of topics on the portal:'); ?></th>
    <td><input type="text" name="number_of_topics" id="number_of_topics" /></td>
    </tr>
    </table>
    <p class="submit alignleft"><input name="submit" type="submit" value="<?php _e('Submit'); ?>" /></p>
    </form>
    <?php
    }

    // Update portal
    function update_bbportal() {
    if (isset($_POST['submit'])) {
    bb_update_option( 'pforum_id', $_POST['forum_id'] );
    bb_update_option( 'number_of_topics', $_POST['number_of_topics'] );
    }
    }

    So what do i miss?

    Thx


    Sam Bauers
    Participant

    @sambauers

    You are missing the API hooks, and you have to be a bit more specific about your forms submit name to distinguish it from other plugins.

    The code below is at least a step closer, notice the change to the name of the submit button and to the if conditional in the update_bbportal function. The specified action allows that function to be called when the admin header is loaded.

    You will also have to pull the values you insert into the options back out again to re-populate the form on the same page.

    // Add action for the admin area to process the form
    add_action('bb_admin-header.php','update_bbportal');

    // Show form
    function bbportal_form() {
    ?>
    <h2><?php _e('Portal Management'); ?></h2>
    <h3><?php _e('Portal settings'); ?></h3>
    <form action="" method="post">
    <table>
    <tr><th scope="row"><label for="forum_id"><?php _e('Where do you want to pull the topics from?'); ?></th>
    <td><?php forum_dropdown(); ?></label></td>
    </tr>
    <tr><th scope="row"><?php _e('Number of topics on the portal:'); ?></th>
    <td><input type="text" name="number_of_topics" id="number_of_topics" /></td>
    </tr>
    </table>
    <p class="submit alignleft"><input name="submitPortal" type="submit" value="<?php _e('Submit'); ?>" />

    </form>
    <?php
    }

    // Update portal
    function update_bbportal() {
    if (isset($_POST['submitPortal'])) {
    bb_update_option( 'pforum_id', $_POST['forum_id'] );
    bb_update_option( 'number_of_topics', $_POST['number_of_topics'] );
    }
    }

    Well, at least that is one way to do it.


    Null
    Member

    @null

    Ah going to try that tonight. Don’t know why, but I never understood forms :S (perhaps cause there are many different ways to do this…) I am going to print this one and mine and compare them. Thx for the help, learned something today :)

    Quote:

    and you have to be a bit more specific about your forms submit name to distinguish it from other plugins

    Well that could explain why it updated on its own :D

    Thx

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.