Jump to content

form with submit, reset, add, delete button


KrazyKanuk

Recommended Posts

I am new to php and I thought it best to start by using a CMS and creating small functions to extend it's functionality. I will start by explaining the situation around what I want to do. I am trying to create a module for the drupal CMS. The module I am trying to create I am creating for my girlfriends son (for when it is complete he will be the one maintaining it through the web interface). He wants a game server (WOW) and a web site that once they create an account on the web site it will create an account on the game server as well. If I understand the way this works correctly for this game server to work you need a logon database and then a character and world database for each realm you have. With that basically what happens is you go to the drupal web site and click create account, input a username, email address and password and agree to terms of service. Then drupal does some checks and writes the information to the drupal database and with the module I creating it will write to the logon database as well. I am working on the admin forms so you can edit how many realms the server has the sql statement to login to the game and add and delete realms. I have been looking at an example of a form for drupal here http://drupal.org/node/717746 but have converted it to my purpose and it brings up some questions here is what I have so far:

function wowcp_gameserver_settings($form_state) {
    $form['server'] = array(
        '#type' => 'fieldset',
        '#title' => t('WOW Game Server Core'),
    );
    $form['server']['decision'] = array(
        '#type' => 'select',
        '#title' => 'Select the Server Core that your WOW server runs on',
        '#options' => array('TrinityCore2', 'ArcEMU', 'Aspire', 'Mangos'),
        '#default_value' => TrinityCore2,
     );
    $form['server']['sql_command'] = array(
        '#type' => 'textfield',
        '#title' => 'Select the SQL command for your server core',
        '#size' => 150,
    );
    $form['server']['max_realms'] = array(
        '#type' => 'textfield',
        '#title' => t('Select the maximum realms allowed'),
        '#size' => 2,
    );
    $form['logon'] = array(
        '#type' => 'fieldset',
        '#title' => t('WOW Game Server LOGON database setup')
    );
    $form['logon']['logon_db'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the name of the LOGON database'),
        '#size' => 25,
    );
    $form['logon']['logon_username'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the username for the LOGON database'),
        '#size' => 25,
    );
    $form['logon']['logon_password'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the password for the LOGON database'),
        '#size' => 25,
    );
    $form['realm']['character'] = array(
        '#type' => 'fieldset',
        '#title' => t('WOW Game Server first realm'),
    );
    $form['realm']['character']['character_db'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the name of the CHARACTER database'),
        '#default_value' => $form_state['values']['character_db'],
        '#size' => 25,
    );
    $form['realm']['character']['character_username'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the username for the CHARACTER database'),
        '#default_value' => $form_state['values']['character_username'],
        '#size' => 25,
    );
    $form['realm']['character']['character_password'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the password for the CHARACTER database'),
        '#default_value' => $form_state['values']['character_password'],
        '#size' => 25,
    );
    $form['realm']['world']['world_db'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the name of the WORLD database'),
        '#defaule_value' => $form_state['values']['world_db'],
        '#size' => 25,
    );
    $form['realm']['world']['world_username'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the username for the WORLD database'),
        '#defaule_value' => $form_state['values']['world_username'],
        '#size' => 25,
    );
    $form['realm']['world']['world_password'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the password for the WORLD database'),
        '#defaule_value' => $form_state['values']['world_password'],
        '#size' => 25,
    );
    if (isset($form_state['storage']['new_name'])) {
        $form['character2'] = array(
            '#type' => 'fieldset',
            '#title' => t('WOW Game Server CHARACTER database setup for realm2'),
        );
        $form['character2']['character_db2'] = array(
            '#type' => 'textfield',
            '#title' => t('Enter the name of the CHARACTER database for relam2'),
            '#default_value' => $form_state['values']['character_db2'],
            '#size' => 25,
        );
        $form['character2']['character_username2'] = array(
            '#type' => 'textfield',
            '#title' => t('Enter the username for the CHARACTER database for realm2'),
            '#default_value' => $form_state['values']['character_username2'],
            '#size' => 25,
        );
    $form['character2']['character_password2'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the password for the CHARACTER database for realm2'),
        '#default_value' => $form_state['values']['character_password2'],
        '#size' => 25,
    );
    $form['world2'] = array(
        '#type' => 'fieldset',
        '#title' => t('WOW ame Server WORLD database setup for realm2'),
    );
        $form['world2']['world_db2'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the name of the WORLD database for realm2'),
        '#defaule_value' => $form_state['values']['world_db2'],
        '#size' => 25,
    );
    $form['world2']['world_username2'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the username for the WORLD database for realm2'),
        '#defaule_value' => $form_state['values']['world_username2'],
        '#size' => 25,
    );
    $form['world2']['world_password2'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the password for the WORLD database for realm2'),
        '#defaule_value' => $form_state['values']['world_password2'],
        '#size' => 25,
    );
    }
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    $form['clear'] = array(
        '#type' => 'submit',
        '#value' => 'Reset',
        '#validate' => array('wowcp_form_clear'),
    );
    if (empty($form_state['storage']['new_name'])) {
        $form['new_name'] = array(
            '#type' => 'submit',
            '#value' => 'Add Realm',
            '#validate' => array('wowcp_form_new_name'),
        );
        $form['delete'] = array(
            '#type' => 'submit',
            '#value' => 'Delete Realm',
            '#validate' => array('wowcp_form_delete'),
        );
    }
    return $form;
}

If max_realms is set to a large number the way I see it you would have to hard code everything into the php file. Instead of that could you not remove from the if(isset) and have it that when they click on the add realm button check to make sure the textfield contains a number if it doesn't return an error if it does contain a number make sure it is not greater then max_realms and then use a start count and adding that number to all the arrays for that realm and validate all the information is what you expect. I think this would work for adding realms, I think where the problem would come in is in deleting realms (or it could be me over thinking it). For example if you have max_realms set to 4 you could have 5 the one default one that can't be deleted plus 4 others and the ones that can be deleted would be called realm1 - realm4 if you delete the first 2 you would minus count by 2 so if you clicked add realm again it would try and create realm3 which already exists. Is there another way on doing this that you could create the realms and delete them and it not try to add realms with the same array name?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.