0

I have an AJAX form that I've created into a block exposed on my current page. I will have an interface that will allow the users to select an indefinite amount of content to be used within the submit function of the form.

All the variables that will be passed to my form are node ID numbers, but there could be 1, there could be 50.

I have the Jquery action ready to update my ID numbers but I don't know how to get my ID number from Jquery into a variable, which can then be read when the AJAX form is submitted.

I've been looking at this - How to pass Javascript variable with form on submit?

and this - How to trigger a form and pass variables to $form_state with jQuery?

And I've been looking at the responses to both, but neither seem to give me access to the data I need, or are only speaking about a single variable, I need to pass a list or an array.

Would it be best to write something to trigger the form rebuild when I provide a new ID and allow the ID to be written into the form via that method?

  • The simple solution would be to add a new field to your form that is hidden (CSS display: none) and make jQuery update the field value. – No Sssweat Nov 27 '18 at 12:25
  • @NoSssweat I tried adding a hidden value to the form via jquery but the form didn't seem to pick them up. The issue is that I need the list in the order the user modifies it, not sequential. Eg if someone enters 5, 1, 20, 10 in that order, if the form returns 1, 5, 10, 20, then that'd be wrong – Andrew Morris Nov 27 '18 at 12:30
  • Well, you would make jQuery enter the values in the order the user modified it, so that's not really a deal braker. form didn't seem to pick them up should be like any regular field, did you look inside $form_state? – No Sssweat Nov 27 '18 at 12:41
  • @NoSssweat maybe I misunderstood, I thought you suggested putting something like a series of checkboxes that are hidden by CSS and javascript turns them on or off, do you mean hide a text box or something where it enters the value in as a string (or similar)? – Andrew Morris Nov 27 '18 at 12:44
  • Indeed, by using a textfield. – No Sssweat Nov 27 '18 at 12:46
0

For anyone looking to an answer to this, it really depends on whether or not you're working with an AJAX form or not. Ideally I wanted to avoid just populating a text area with my updated field to be submitted, so thankfully, since it was using the AJAX form, I was able to pass it information dynamically through Jquery.

In your javascript

// whatever your function is to get the information you want to pass
....
Drupal.ajax['edit-save'].options.data._my_vars = JSON.stringify(myList);

// Where 'edit-save' is the ID of my save button in my form
// you can console.log(Drupal.ajax) to see what options you have

Then in the callback for your AJAX request, the return request is stored in

$form_state['input']['_my_vars'] // or whatever variable name you passed above

Then you can check whether or not the response has comeback empty by using

 if (empty(json_decode($form_state['input']['_my_vars']))) {
   form_set_error('save'); // You can set the error to be any form element
 }

and in your callback code you can make your own error handling by doing

if (form_get_error() !== null) { 
  // fail code
} else {
  // success code
}

Hope this helps someone else down the line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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