I am newbie into Drupal.   I want to add 2 form elements dynamically to existing form via AJAX based on particular condition. But in the examples I saw till now the AJAX callback defined returns only one element to form. I tried to achieve this by adding these two fields in fieldset and returning fieldset as a single item. But this replaces my container div completely. This works only at first time and then the contents returned by callback replaces the container.   function new_registration($form, &amp;$form_state) {     $form['select-reason'] = array(         '#type' =&gt; 'select',         '#title' =&gt; t('Reason'),         '#options' =&gt; array('' =&gt; 'Select') + _global_reasons(),         '#description' =&gt; t('Reason.'),         '#ajax' =&gt; array(           'event' =&gt; 'change',           'callback' =&gt; 'is_true',           'wrapper' =&gt; 'options',           'method' =&gt; 'replace'         ),       );      $form['options'] = array(         '#type' =&gt; 'container',         '#prefix' =&gt; '&lt;div id="options"&gt;',         '#suffix' =&gt; '&lt;/div&gt;'       );     }  function is_true(&amp;$form, &amp;$form_state) {       $product_quality = $form_state['values']['select-reason'];       if(_belongs_check($product_quality)) {           $form['a-add'] = array(               '#type' =&gt; 'fieldset',               '#collapsible' =&gt; FALSE,               '#collapsed' =&gt; FALSE,           );           $form['a-add']['detail'] = array(               '#type' =&gt; 'textfield',               '#title' =&gt; t ( 'Details' ),               '#description' =&gt; t ( 'Enter product name.' ),               '#maxlength' =&gt; 400,               '#size' =&gt; 100,               '#required' =&gt; TRUE,           );           $form['a-add']['num'] = array(               '#type' =&gt; 'textfield',               '#title' =&gt; t ( 'Number' ),               '#description' =&gt; t ( 'Enter number.' ),               '#maxlength' =&gt; 400,               '#size' =&gt; 100,               '#required' =&gt; TRUE,           );       } else {             //remove 2 fields added in IF block       }       return $form['a-add'];     }   I also tried to return HTML structure of these two and place it in #markup type of form element. But I am not convinced with this way as theming might be complicated. Can anybody help? Thanks in advance.
121190
I'm trying to use ajax to update two different parts of a drupal form from just one form input blur.  I have the standard ajax stuff on my input:  $my_form['my_input']['#ajax'] = array(   'callback' =&gt; 'my_callback',   'wrapper' =&gt; 'my_target_div',   'event' =&gt; 'blur', );   This is working well, swapping out my div when the input is updated ... however I also want to update another form input elsewhere in the form with different code as a result of the blur on my original input.  Is this possible, any ideas?  Edit: For clarity, here's my real world example:   Content type 'movie' Added field 'primary_title' When 'primary_title' is updated my ajax callback checks for similar strings and returns html. The html from the callback is inserted into an empty div.  That part is working great!   I'm trying to also modify the standard node 'title' input, giving it the value of 'primary_title' after I have done some regex to tidy up the string (removing "The" or "A" from the start etc) The result will be two title fields, one with the complete title 'primary_title' and one trimmed 'title' that will be useful for sorting and displaying records.
116319
I am trying to add jQuery to my page (it is not being added by default). The file is adding to the page but after drupal.js causing the error:  SCRIPT5009: 'jQuery' is undefined  drupal.js, line 5 character 1    The code that adds drupal.js to the page in common.inc  ... 'misc/drupal.js' =&gt; array(           'data' =&gt; 'misc/drupal.js',           'type' =&gt; 'file',           'scope' =&gt; 'header',           'group' =&gt; JS_LIBRARY,           'every_page' =&gt; TRUE,           'weight' =&gt; -1,           'preprocess' =&gt; TRUE,           'cache' =&gt; TRUE,           'defer' =&gt; FALSE,         ), ...   In my module hook_init I have:    drupal_add_js('misc/jquery.js', array(     'group' =&gt; JS_LIBRARY,     'weight' =&gt; -20,     'cache' =&gt; TRUE,   ));   I would think with a lower weight it would load before drupal.js?  The header of the page contains the following scripts:  &lt;script type="text/javascript" src="http://mysite.com/misc/drupal.js?mqdxrf"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://mysite.com/misc/jquery.js?mqdxrf"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://mysite.com/misc/jquery.once.js?mqdxrf"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://mysite.com/sites/default/files/languages/nl_c9aC92_wtuULa79-hTFcGPM53oqk1yKwciOa_Q3yO28.js?mqdxrf"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://mysite.com/sites/all/modules/lang_dropdown/msdropdown/jquery.dd.js?mqdxrf"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://mysite.com/sites/all/modules/lang_dropdown/lang_dropdown.js?mqdxrf"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://mysite.com/sites/all/modules/devel/devel_krumo_path.js?mqdxrf"&gt;&lt;/script&gt;
56223
Any idea why the piece of code below does not add the script element to the DOM?  var code = "&lt;script&gt;&lt;/script&gt;"; $("#someElement").append(code);
29416
I have set theme handler for a form in Drupal and this handler has a template file. What is the easiest way to send a value from PHP to JavaScript in that include file?
74715
I have several input and option elements on my page, each (well almost) have an event attached to update some text on the page once they change. I use jQuery which is really really cool :)   I also use Microsofts  framework, utilizing the UpdatePanel. The reason why I do that is that certain elements are created on the page based on some server-side logic. I don't really want to explain why I use the UpdatePanel - even if it could (it can with quite some effort) be rewritten to use only  I still want that UpdatePanel.  You probably guessed it - once I have a postback on the UpdatePanel the jQuery events stops working. I actually was expecting this, since the "postback" is not really a new postback so my code in document.ready that binds the events won't be fired again. I also confirmed my suspicion by reading up on it in the jQuery help libraries.  Anyway I'm left with the problem of rebinding my controls after the UpdatePanel is done updating the . I preferably need a solution that does not require adding more .js files (jQuery plug-ins) to the page but something as simple as being able to catch the UpdatePanel's 'afterupdating' where I can just call my method to rebind all the form elements.
18317
I'm running into a problem where a user can submit any form built by the Form API multiple times (fast clicking resulting in multiple requests).  I've put in the basic client side (javascript) solution of disabling the button, but I'm curious what the best approach to preventing this situation is on the server side.  Is there a recommended way to use Drupal's form token system to handle this?  Especially a global form solution (ie, adding a custom validator to every form using hook_form_alter()).  My approach so far has been something like this:  function mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {   $form['#validate'][] = 'mymodule_form_validate'; }  function mymodule_form_validate(&amp;$form, &amp;$form_state){   //initialize form array   if (!isset($_SESSION['submitted_forms'])){     $_SESSION['submitted_forms'] = array();   }    $form_token = $form_state['values']['form_token'];   if ( isset($_SESSION['submitted_forms'][$form_token]) &amp;&amp; $_SESSION['submitted_forms'][$form_token] = TRUE ){     form_set_error('name]', 'This form has already been submitted');   }   else{     $_SESSION['submitted_forms'][$form_token] = TRUE;   } }   I'm running into trouble where the form_token isn't unique to the form - it seems to stay the same no matter what happens.  I'm probably misunderstanding what the token is in the grand scheme of the form api.  Any insight is appreciated!
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);
38310
Given the following classes and controller action method:  public School {   public Int32 ID { get; set; }   publig String Name { get; set; }   public Address Address { get; set; } }  public class Address {   public string Street1 { get; set; }   public string City { get; set; }   public String ZipCode { get; set; }   public String State { get; set; }   public String Country { get; set; } }  [Authorize(Roles = "SchoolEditor")] [AcceptVerbs(HttpVerbs.Post)] public SchoolResponse Edit(Int32 id, FormCollection form) {   School school = GetSchoolFromRepository(id);    UpdateModel(school, form);    return new SchoolResponse() { School = school }; }   And the following form:  &lt;form method="post"&gt;   School: &lt;%= Html.TextBox("Name") %&gt;&lt;br /&gt;   Street: &lt;%= Html.TextBox("Address.Street") %&gt;&lt;br /&gt;   City:  &lt;%= Html.TextBox("Address.City") %&gt;&lt;br /&gt;   Zip Code: &lt;%= Html.TextBox("Address.ZipCode") %&gt;&lt;br /&gt;   Sate: &lt;select id="Address.State"&gt;&lt;/select&gt;&lt;br /&gt;   Country: &lt;select id="Address.Country"&gt;&lt;/select&gt;&lt;br /&gt; &lt;/form&gt;   I am able to update both the School instance and the Address member of the school.  This is quite nice!  Thank you ASP.NET MVC team!  However, how do I use jQuery to select the drop down list so that I can pre-fill it?  I realize that I could do this server side but there will be other dynamic elements on the page that affect the list.  The following is what I have so far, and it does not work as the selectors don't seem to match the IDs:  $(function() {   $.getJSON("/Location/GetCountryList", null, function(data) {     $("#Address.Country").fillSelect(data);   });   $("#Address.Country").change(function() {     $.getJSON("/Location/GetRegionsForCountry", { country: $(this).val() }, function(data) {       $("#Address.State").fillSelect(data);     });   }); });
92599
Should I just paste the code in the main template file, or is there any other recommended way of attaching new Jquery plugins in Drupal 7?
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?
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?
109677
I have two lists:   List A: Products List B: Requirements   I'd like to have a nice Create Requirement page launched from the Product Listing page.  I have a DVWP/Multi-Item View displaying all Products. Next to each product, there is a Create Requirement link. The hyperlink generates the Query String URL /Pages/Create-Requirement.aspx?Product=Product  On the Create-Requirement.aspx page, I have a DVWP/New Item Form. The form includes a lookup field back to the Products list. I would like the Query String parameter to populate that lookup field and for the field to be hidden.  I've seen different methods of using Javascript or jQuery to pass to a field on a Custom New Item Form - but not necessarily a DVWP/New Item Form. And it's easy to pass to a text field but not a lookup field. I even tried passing to a text field that would then populate the lookup by workflow on item creation but SPD won't do that (unless someone can think of workaround logic).  Hiding the field should be easy enough - I just can't make the lookup happy. (Lookups are never happy.)
107032
In Drupal 8, how do I validate a ConfigEntity? I tried adding constraints, but then I found out this only works for ContentEntity, not ConfigEntity.  My ConfigEntity has 2 fields, and the options of the second field should be limited based on what's selected in the first.   Through Drupals #ajax mechanism I can limit the items in the second dropdown - that all works. However, with Javascript disabled or when creating the ConfigEntity programmatically, someone can still select a wrong value for the second dropdown, that doesn't work with the first dropdown selection. I checked ConfigEntityBase but couldn't find anything related to validation.
98687
If the user selects an option in a dropdown box, there must be added a label and a textbox. Using appendChild, these elements get added to the end of the container.  var newFreeformLabel = document.createElement('label'); newFreeformLabel.innerHTML = 'Omschrijving:';  var newFreeformField = document.createElement('input'); newFreeformField.className = 'textfield'; newFreeformField.name = 'factuur_orderregel[]';  var newFreeformSpacer = document.createElement('div'); newFreeformSpacer.className = 'spacer';  container.appendChild(newFreeformLabel); container.appendChild(newFreeformField); container.appendChild(newFreeformSpacer);   The issue is that these elements should be inserted at the beginning of container, not at the end.  Is there a solution to this in PrototypeJS?
29087
/**  * Implements hook_form_alter().  */ function MODULE_NAME_form_alter(   &amp;$form,   \Drupal\Core\Form\FormStateInterface $form_state,   $form_id ) {    if ($form_id == 'node_article_form') {     $form["body"]["widget"][0]['#allowed_formats'] = ['full_html']; // Your default format     $form["body"]["widget"][0]['#after_build'][] = 'MODULE_NAME_hide_format';   } }  function MODULE_NAME_hide_format(   &amp;$element,   FormStateInterface $form_state ) {   if (isset($element["format"]["format"])) {     $element["format"]["format"]["#access"] = FALSE;   }    return $element; }
30655
This is the code in my template.php file.  function studiga_theme_theme($existing, $type, $theme, $path) {         $items['add_property_form'] = array(         'render element' =&gt; 'form',         'template' =&gt; 'node--14',         'path' =&gt; drupal_get_path('theme', 'studiga_theme') . '/templates/node',     );      return $items; }    function custom_add_property_form($form, &amp;$form_state) {     $form['first_name'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('First name')),     );     $form['last_name'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Last name')),     );     $form['address'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Address')),     );     $form['city'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('City')),     );     $form['state'] = array(         '#type' =&gt; 'select',         '#options' =&gt; array(             'default' =&gt; 'State',             '...' =&gt; '...',         ),     );     $form['zip'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Zip')),     );     $form['email'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Email')),     );     $form['phone'] = array(         '#type' =&gt; 'textfield',         '#attributes' =&gt; array('placeholder' =&gt; t('Phone')),     );     $form['submit'] = array(         '#type' =&gt; 'submit',         '#value' =&gt; 'Submit',     );      return $form; }   This is the code in node--14.tpl.php.                                          Contact Information                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  &lt;div class="row"&gt;     &lt;div class="small-12 medium-12 large-8 large-offset-2 columns"&gt;         &lt;?php print render($form['submit']); ?&gt;     &lt;/div&gt; &lt;/div&gt;     But no fields are appearing - what am I doing wrong? You can't have a straight forward  in a tpl.php file can you?
4670
I have an element that already has a class: &lt;div class=&quot;someclass&quot;&gt;     &lt;img ... id=&quot;image1&quot; name=&quot;image1&quot; /&gt; &lt;/div&gt;  Now, I want to create a JavaScript function that will add a class to the div (not replace, but add). How can I do that?