0

Is what i am trying to do is on this page it will display all items from the database that currently exist. Then at the top of this list will be a small form that will allow someone to add an item to the list. After someone submits the data and posts the form I want the list below to automatically populate with the updated data. I am having a really hard time trying to figure this out.

function clay_mod_blacklist_form($form, &$form_state) {
drupal_add_css(drupal_get_path('module', 'clay_mod') . '/css/clay_mod.min.css');
drupal_add_js(drupal_get_path('module', 'clay_mod') . '/js/jquery.maskedinput.js');
drupal_add_js(drupal_get_path('module', 'clay_mod') . '/js/clay_mod.js');


$form['company'] = array(
    '#type' => 'textfield',
    '#title' => t('Company Name'),
    '#required' => TRUE
);
$form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Contact Name'),
    '#required' => TRUE
);
$form['phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Phone'),
    '#required' => TRUE
);
$form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE
);
$form['submit_button'] = array(
    '#type' => 'button',
    '#submit' => array('clay_mod_blacklist_form_submit'),
    '#ajax' => array(
        'callback' => 'clay_mod_blacklist_form_ajax',
        'wrapper' => 'box',
        'method' => 'after'
    ),
    '#value' => t('Add To Blacklist'),
    '#id' => 'ajax-save-form-values'
);

$form['results'] = array(
    '#type' => 'container',
    '#prefix' => '<div class="customShit" id="box">',
    '#suffix' => '</div>'
);

if(isset($form_state['values'])) {
    $form['results'][] = $form_state['values']['results'];
}
// This is where I was display info from database before the form was submitted
//    if(!empty($form_state['values']['results'])) {
//        $form['results'] = $form_state['values']['results'];
//    } else {
//        $rows = db_select('blacklist', 't')
//            ->fields('t')
//            ->execute()
//            ->fetchAll();
//        foreach($rows as $k => $v) {
//            $form['results'][$k]['#markup'] = "<div    class=\"vvvvvvvvv\">x{$v->name}y</div>";
//        }
//    }



return $form;
}
function clay_mod_blacklist_form_ajax($form, $form_state) {
    return $form['results'];
}
function clay_mod_blacklist_form_submit($form, $form_state) {
   ddl('ajax submit');
   $form_state['rebuild'] = TRUE;
   clay_mod_blacklist_form_save($form, $form_state);

}
function  clay_mod_blacklist_form_save(&$form, &$form_state) {
$companyName = $form_state['values']['company'];
$name = $form_state['values']['name'];
$phone = $form_state['values']['phone'];
$email = $form_state['values']['email'];
$query = db_insert('blacklist')
    ->fields(array(
        'company_name' => $companyName,
        'name' => $name,
        'phone' => $phone,
        'email' => $email,
    ))->execute();
}

1 Answer 1

0

Add a wrapper around the entire form, and return the entire form from your ajax callback. Then just build the rows reguarly in your form definition - The newly added rows will be shown since all the data is pulled from the database when the from is generated.

function clay_mod_blacklist_form($form, &$form_state) {
  $form['#prefix'] = '<div id="clay_mod_blacklist_form_wrapper">;
  $form['#suffix'] = '</div>';

  // Add the rest of your form definition here

  $form['submit_button'] = array(
    '#type' => 'button',
    '#submit' => array('clay_mod_blacklist_form_submit'),
    '#ajax' => array(
      'callback' => 'clay_mod_blacklist_form_ajax',
      'wrapper' => 'clay_mod_blacklist_form_wrapper',
      'method' => 'after'
    ),
    '#value' => t('Add To Blacklist'),
  );

  $rows = db_select('blacklist', 't')
    ->fields('t')
    ->execute()
    ->fetchAll();
  foreach($rows as $k => $v) {
    $form['results'][$k]['#markup'] = "<div    class=\"vvvvvvvvv\">x{$v->name}y</div>";
}

function clay_mod_blacklist_form_ajax(&$form, &$form_state)
{
  return $form;
}
5
  • I think this is really close. Now it seems though that it returns the whole results again twice. I need this to show the results with only the newest line added not every item listed again.
    – Atrium
    Commented Dec 30, 2016 at 21:03
  • Also inside function clay_mod_blacklist_form_ajax($form, $form_state) { return $form; } It returns the entire form not only the added item.
    – Atrium
    Commented Dec 30, 2016 at 21:05
  • Yes, I did that on purpose. You can alter it as necessary.
    – Jaypan
    Commented Dec 30, 2016 at 23:17
  • Well, I don't understand how. I can't get it to show all items from database along with new entry. I can get it to return a single item or the whole entire form again.
    – Atrium
    Commented Jan 2, 2017 at 4:56
  • You query all results from the database, and you load the results both on initial load, as well as in your ajax load. As the new item is added to the database, when you load all results with ajax, the new element will also be loaded.
    – Jaypan
    Commented Jan 2, 2017 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.