I want a button that runs a callback function using the AJAX method included in Form API.  The button I have still submits the whole node edit form. I don't want it to do that - just pass a couple variables to the callback function and display the results. How can I do that?  This is my FAPI code for that button:      $form['rid_import']['rid_get'] = array(                 '#type' =&gt; 'button',                 '#value' =&gt; t('Import'),                 '#attributes' =&gt; array(                     'id' =&gt; array('fetch_rid'),                     'class' =&gt; array('col-sm-3', 'col-xs-4')                 ),                 '#parents' =&gt; array('rid_import'),                 '#limit_validation_errors' =&gt; array(),                 '#executes_submit_callback' =&gt; false,                 "#submit" =&gt; array('sql_no_submit'),                 '#ajax' =&gt; array(                     "callback" =&gt; 'sql_fetch_restid',                        "wrapper" =&gt; 'rid_sql_query',                     "event" =&gt; 'click'                 )           );   EDIT  You're right, I was being unclear. Basically what I wanted is something that works like your "Add More" or "Remove" buttons when you have multiple-value fields, but just to run my function without reloading the page – grab some data from the DB and show it there on the node edit form. I have a multivalue field collection that should be populated with those values and then saved together with the rest of the node. That's why I don't want it to go ahead and save the node. However, if it isn't possible I guess I will have to find another way to do what I need.  And tried to add a submit function, still reloads the whole page.
122548
I have a submit button in my form and I want to implement it using ajax:  function myid_user_page_form(){      drupal_add_library('system', 'drupal.ajax');     $form = array();     $form['id'] = array(        '#type' =&gt; 'fieldset',        '#title' =&gt; t('ID Information'),        '#prefix' =&gt; '&lt;div id="myid_search_id_number_wrapper"&gt;',        '#suffix' =&gt; '&lt;/div&gt;'    );       $form['submit'] = array(       '#type' =&gt; 'button',       '#value' =&gt; 'Save',          '#ajax' =&gt; array(          'event' =&gt; 'click',          'callback' =&gt; 'myid_user_page_form_ajax_submit',          'wrapper' =&gt; 'myid_search_id_number_wrapper'       ),        '#attributes' =&gt; array(         'id' =&gt; 'myid_save',                    ), );  function myid_user_page_form_ajax_submit($form, &amp;$form_state) {      watchdog('Wow','Wow'); }   After executing the code above, the page still has it's refresh after it's clickfunctionality. The ajax doesnt work and no Wow message log in my watchdog table from my database, which means the ajax callback function isn't called. Where am I missing?
52808
I have a form with some checkboxes and inputboxes selectboxes and it shows what the user wants via an ajax call. The problem is that when the user click on the item and the detail page is shown and then decide to go back to the previous page he needs to click and select his previous choice again.  I would like to make WP to store all the options in the session when the button to the detail page is clicked and save actual info in the session and then when he visits the page again the  values will be chcecked in sessions and set if any are found.  Could that be done in WP?  If yes, how?  Let's oversimplify that and say that we have something like this in our form:  &lt;input class="car_color" type="checkbox" name="car_color" value="1" /&gt; &lt;input class="car_color" type="checkbox" name="car_color" value="2" /&gt; &lt;input class="car_color" type="checkbox" name="car_color" value="8" /&gt; &lt;input class="car_color" type="checkbox" name="car_color" value="4" /&gt; &lt;input class="car_color" type="checkbox" name="car_color" value="6" /&gt;   I don't use submit button in my form, it's handled via AJAX on input change.  And in my results get via ajax I have a link to the detail page:  &lt;a class="detail-info-link" href="&lt;?php echo $url ?&gt;"&gt;   Any idea how can I store my values in Session and call them when revisiting/reloading/back button in the browser?  I need to be able to read the session stored stuff and use it via ?Javascript? and trigger my search fuction via ajax which is already working well.  I just need to store (probably before going to $link in href of by detail button and read and send session variables (if they exist).
21618
I'm trying to add AJAX callback to form element that was previously rendered by AJAX but with no luck. Code below.   A bit of description. There are three main methods here:   buildForm - base form extraField - AJAX callback attached to ['month'] choosePerson - AJAX callback attached to: ['week_day']['hour']   In the buildForm method I create base form with AJAX callback on 'month' select element. In the AJAX callback you can find an extraField method. This method contains form element called 'hour' ($form['week_day']['hour']) and this element renders well. But this element also contains AJAX callback, which doesn't work, it does not generate 'person_container' element and the 'person' select.   Any idea how to ajaxify elements that previously were rendered by ajax?  class ReservationForm extends FormBase {    public function getFormId() {     return 'reservation_form';   }    public function buildForm(array $form, FormStateInterface $form_state) {      $form['month'] = array(       '#type' =&gt; 'select',       '#title' =&gt; $this-&gt;t('Select month'),       '#options' =&gt; array($this-&gt;t('January'), $this-&gt;t('February'), $this-&gt;t('March')),       '#ajax' =&gt; array(         'callback' =&gt; array($this, 'extraField'),         'event' =&gt; 'change',         'wrapper' =&gt; 'week-day',       ),     );      // Disable caching on this form.     $form_state-&gt;setCached(FALSE);      $form['week_day'] = [       '#type' =&gt; 'container',       '#attributes' =&gt; ['id' =&gt; 'week-day'],     ];   }    public function extraField(array &amp;$form, FormStateInterface $form_state) {     $month = $form_state-&gt;getValue('month');      $hour_options = array();      if ($month == '0') {       $hour_options[0] = $this-&gt;t('08:00');       $hour_options[1] = $this-&gt;t('09:00');       $hour_options[2] = $this-&gt;t('10:00');     }      if ($month == '1') {       $hour_options[0] = $this-&gt;t('12:00');       $hour_options[1] = $this-&gt;t('13:00');       $hour_options[2] = $this-&gt;t('14:00');     }      if ($month == '2') {       $hour_options[0] = $this-&gt;t('18:00');       $hour_options[1] = $this-&gt;t('19:00');       $hour_options[2] = $this-&gt;t('20:00');     }      $form['week_day']['hour'] = array(       '#type' =&gt; 'select',       '#title' =&gt; $this-&gt;t('Choose hours @month', array('@month' =&gt; $month)),       '#options' =&gt; $hour_options,       '#ajax' =&gt; array(         // tried with that callback too: [ $this, 'choosePerson']         'callback' =&gt; array($this, 'choosePerson'),         'event' =&gt; 'change',         'wrapper' =&gt; 'person',       ),     );      $form['week_day']['person_container'] = array(       '#type' =&gt; 'container',       '#attributes' =&gt; ['id' =&gt; 'person-container'],     );      $form['week_day']['person_container']['person'] = array(       '#type' =&gt; 'select',       '#title' =&gt; $this-&gt;t('Choose person'),       '#options' =&gt; ['John', 'Sarah', 'Peter'],     );      return $form['week_day'];   }    public function choosePerson(array &amp;$form, FormStateInterface $form_state) {     $form['week_day']['person_container']['person'] = array(       '#type' =&gt; 'select',       '#title' =&gt; $this-&gt;t('Choose person'),       '#options' =&gt; ['John', 'Sarah', 'Peter'],     );     return $form['week_day']['person_container'];   } }
112689
I'm trying to add an "add more" field to a form in my custom module. I've been looking at the examples module, with no luck.   Here's what I have so far:  $form['#tree'] = TRUE;   $form['names_fieldset'] = array(   '#type' =&gt; 'fieldset',   '#title' =&gt; t('People coming to the picnic'),   // Set up the wrapper so that AJAX will be able to replace the fieldset.   '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',   '#suffix' =&gt; '&lt;/div&gt;', );  if (empty($form_state['num_names'])) {   $form_state['num_names'] = 1; }  for ($i = 0; $i &lt; $form_state['num_names']; $i++) {   $form['names_fieldset']['name'][$i] = array(     '#type' =&gt; 'textfield',     '#title' =&gt; t('Name'),   ); }  $form['names_fieldset']['add_name'] = array(   '#type' =&gt; 'submit',   '#value' =&gt; t('Add one more'),   '#submit' =&gt; array('spower_add_more_add_one'),   // See the examples in ajax_example.module for more details on the   // properties of #ajax.   '#ajax' =&gt; array(     'callback' =&gt; 'spower_add_more_callback',     'wrapper' =&gt; 'names-fieldset-wrapper',   ), );  if ($form_state['num_names'] &gt; 1) {   $form['names_fieldset']['remove_name'] = array(     '#type' =&gt; 'submit',     '#value' =&gt; t('Remove one'),     '#submit' =&gt; array('spower_add_more_remove_one'),     '#ajax' =&gt; array(       'callback' =&gt; 'spower_add_more_callback',       'wrapper' =&gt; 'names-fieldset-wrapper',     ),   ); }     function spower_theme($existing, $type, $theme, $path) {   $base = array(     'render element' =&gt; 'form',   );    return array(     'spower_my_form' =&gt; $base + array(       'template' =&gt; 'spower',     ),   ); }  function spower_add_more_callback($form, $form_state) {   return $form['names_fieldset']; }  /**  * Submit handler for the "add-one-more" button.  *  * Increments the max counter and causes a rebuild.  */ function spower_add_more_add_one($form, &amp;$form_state) {   $form_state['num_names']++;   $form_state['rebuild'] = TRUE; }  /**  * Submit handler for the "remove one" button.  *  * Decrements the max counter and causes a form rebuild.  */ function spower_add_more_remove_one($form, &amp;$form_state) {   if ($form_state['num_names'] &gt; 1) {     $form_state['num_names']--;   }   $form_state['rebuild'] = TRUE; }   The problem is that when I click "add more", the entire form is submitted.   If the above isn't clear, I've added the module at  and the spower_elements.inc at .  Thanks for the help.
53883
Drupal doesn't seem to prevent multiple form submits, even on node pages. This  suggests fixing it on the server side.  How could I disable the submit button after clicking?   Any other solutions?
8344
I'm working with a bit of html and Javascript code that I've taken over from someone else. The page reloads a table of data (via an asynchronous request) every ten seconds, and then re-builds the table using some DOM code. The code in question looks something like this:  var blah = xmlres.getElementsByTagName('blah'); for(var i = 0; i &lt; blah.length; i++) {     var td = document.createElement('td');     var select = document.createElement('select');     select.setAttribute("...", "...");     select.onchange = function() {         onStatusChanged(select, callid, anotherid);     };     td.appendChild(select); }   When the onchange event is fired for a &lt;select&gt; element however, it seems like the same values are being passed to the onStatusChanged() method for every &lt;select&gt; in the table (I've verified that in each iteration of the loop, callid and anotherid are being given new, distinct values).   I suspect this is occuring because of the nature of how I am setting the event handler, with the select.onchange = function() syntax. If I understand how this is working correctly, this syntax sets a closure for the onchange event to be a function which refers to these two references, which end up having a final value of whatever they are set to on the last iteration of the loop. When the event fires, the value referenced by callid and anotherid is the value set in the last iteration, not the value set at the individual iteration.  Is there a way that I can copy the value of the parameters I am passing to onStatusChanged()?  I've changed the title to better reflect the question and the accepted answer.
108591
I have a form which allows the user to create extra "rows" using JQuery (using .clone) so that they can decide how many of the same information they need to submit. My issue is that I cannot work out how to access these form items within my controller.  the form that is being submitted may look like this  &lt;input type="text" name="Amount" id="Amount"&gt;    &lt;select name="Item"&gt;        &lt;option value="1"&gt;Item 1"&lt;/option&gt;        &lt;option value="2"&gt;Item 2"&lt;/option&gt;        &lt;option value="3"&gt;Item 3"&lt;/option&gt;    &lt;/select&gt; &lt;input type="text" name="Amount" id="Amount"&gt;    &lt;select name="Item"&gt;        &lt;option value="1"&gt;Item 1"&lt;/option&gt;        &lt;option value="2"&gt;Item 2"&lt;/option&gt;        &lt;option value="3"&gt;Item 3"&lt;/option&gt;    &lt;/select&gt; &lt;input type="text" name="Amount" id="Amount"&gt;    &lt;select name="Item"&gt;        &lt;option value="1"&gt;Item 1"&lt;/option&gt;        &lt;option value="2"&gt;Item 2"&lt;/option&gt;        &lt;option value="3"&gt;Item 3"&lt;/option&gt;    &lt;/select&gt;   Basically, the block between input and the select could be repeated an infinite number of times. When I submit to the controller I am then using FormCollection form to access the form elements. from there I am unsure how I can access the items that have been submitted. I thought of using a for loop and then accessing them via something like form["Amount"][i] but obviously that is not going to work.  Am I going about this the right way and if so, does anyone have any suggestions about how this might work?  Thanks in advance.
9942
Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?  I thought that setting it to null would do it, but apparently it does not work.
24403
By default, the web form is displayed with &lt;?php print drupal_render($form['submitted']); ?&gt; .  I need to make a small edit and add class="btn" to the submit button.  I can change its value with $form['actions']['submit']['#value'] = "click to become a member";  But I couldnt find a way to add the class.  Please help!  Thank you
46022
I just wanted to know how to add "create node form" in a block  when i  copy the source code and make a block out of that code, only public could post from that. when i am posting as a user it says timeout and tells us to refresh the page. plz someone give me solution.  SO i tried to add the following code in page.tpl.php  $form = node_add('nodetype'); print drupal_render($form);
24342
I have a system where I send an Ajax command, which returns a script block with a function in it. After this data is correctly inserted in the DIV, I want to be able to call this function to perform the required actions.  Is this possible?
16554
When I create a form using the form API, the resulting html includes all sorts of unwanted divs. For example, if I use:  $form = array(     'myFieldset' =&gt; array(         '#type' =&gt; 'fieldset'     ) );   The resulting html looks like this:   &lt;form action="/drupal-7.15/?q=chercheur" method="post" id="form-chercheur" accept-charset="UTF-8"&gt;     &lt;div&gt;         &lt;fieldset class="form-wrapper" id="edit-myfieldset"&gt;             &lt;div class="fieldset-wrapper"&gt;             &lt;/div&gt;         &lt;/fieldset&gt;         &lt;input type="hidden" name="form_build_id" value="form-87Dv5ezVpaLI5mD345ofMkLgPhsv_dawudUp73ePQTc" /&gt;         &lt;input type="hidden" name="form_token" value="sXXINWr7rmParm0tRJoEb6QKGIH_QH-ffRSOtnvxYY0" /&gt;         &lt;input type="hidden" name="form_id" value="form_Chercheur" /&gt;     &lt;/div&gt; &lt;/form&gt;   As you can see, two extraneous divs are added here. They are making the javascript I'm writing uselessly complicated, so I was wondering if there was a way to remove the extraneous divs. Ideally, I would like to obtain something like this:  &lt;form action="/drupal-7.15/?q=chercheur" method="post" id="form-chercheur" accept-charset="UTF-8"&gt;     &lt;fieldset class="form-wrapper" id="edit-myfieldset"&gt;     &lt;/fieldset&gt;     &lt;input type="hidden" name="form_build_id" value="form-87Dv5ezVpaLI5mD345ofMkLgPhsv_dawudUp73ePQTc" /&gt;     &lt;input type="hidden" name="form_token" value="sXXINWr7rmParm0tRJoEb6QKGIH_QH-ffRSOtnvxYY0" /&gt;     &lt;input type="hidden" name="form_id" value="form_Chercheur" /&gt; &lt;/form&gt;   Is this possible?
7265
I am writing a WP plugin. I have created a button for the user to click in thw WP dashboard. I need to run a function when a user clicks the button, shown with a red arrow. The code snippet for the button is:&lt;p&gt;&lt;button class="button button-primary"&gt;Update Media Titles and ALT Text&lt;/button&gt;&lt;/p&gt;. I have already created the function in my class like so:  public function kh_update_media_seo() {      //update media files title and alt tags here }   I can handle the code that goes in the function alone, I only need help making the button in the WP dashboard fire off this specific function when clicked.  Pardon me if this sounds dump or straight forward. It's my first time doing this.  My plugin is a one file plugin if that helps.
45620
I'm using drupal_get_form('event_signup_form') to embed my custom form inside a block. The form is being rendered as intended, but submitting the form triggers nothing.  The form function itself is called event_signup_form($form, &amp;$form_state) and the submit function is called event_signup_form_submit($form, &amp;$form_state) but it's not being called.  I am using  to show the form:  function event_block_view($delta = '') { $blocks = array(); if ($delta ...) { $blocks['content'] = drupal_render(drupal_get_form('event_signup_form')); } }   I managed to get this working - here is how (stub):  function hook_block_view($delta = '') { $blocks['content'] = drupal_render(drupal_get_form('event_signup_form')); }  function event_signup_form() { // Register form submit handler, same goes for validation handler $form['#submit'][] = 'event_signup_form_submit';  return $form; }  function event_signup_form_submit($form, &amp;$form_state) { // This is now triggered }
32416
I edited the form for a list item in InfoPath, and when I print, I just want to print the form itself.  It seems when I use the browser's print function, it still pulls up some elements from the page and it cuts off the content if it's longer than the popup window. Also, some of the elements of the form are hidden when I print, so that is not a good option.  I have read that others have attempted to create a custom action that resides in the InfoPath Ribbon, but I have yet to find one that works?  Here is an answer for this but we are unfamiliar with Java Script.  Maybe it will give you some direction: what we did in this case was to add a simple Print button (we used Content web part, but you can do whatever). The print button uses JS to cut out only the relevant portion (based on DOM/CSS) and show it in a new window - basically we open the form in a new window and pass a QS parameter, the JS picks it up and cleans the form. Worked well and was very simple to do, just a few lines of javascript  Does anyone have the Java Script code for this?
76167
I'm not sure if I have done this correctly.  As I understand it:  if I have a class foo and a static method bar I can register that as the callback by passing the array array("foo","bar") as the function name.  If I have an instance of a class in $foo and want to call the method bar I pass the array array($foo,'bar').  If I need to register an action inside the class itself would it work with array($this,'bar')?
8406
In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:  Original URL:         Resulting URL:     &amp;enabled=true   Looking for a JavaScript function which parses the URL looking at each parameter, then adds the new parameter or updates the value if one already exists.