I'd like to call a form that is created in the .module file. It means that in a AJAX function located in a HTML page of drupal, I'd like to append a form that is defined in the module file with a DIV presents in my HTML file.
Do you have a solution to have only the code HTML generated by drupal for a form? But the call to Drupal would be in an AJAX Function.
Actually, I can create a path '?myProject/form' where the form is generated. However, I cannot append the full content of the html page (header, footer of drupal). I only want to append the HTML code of the form.
I hope that I'm understandable
Thank you very much
Bat
Maybe you have a 'page callback' with a function like this in your hook_menu:
$items['myProject/form/%'] = array(
'title' => t('Get a form'),
'description' => 'Get form',
'access callback' => 'user_access',
'page callback' => '_get_form',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
And maybe you have this function defined:
function _get_form($form_id){
return "Hello World with " . $form_id; //returns the whole page with this text
}
If you change return for print, Drupal only return this text, like this:
function _get_form($form_id){
print "Hello World with " . $form_id; //returns only this text
}
I hope it helps
Related
have a Magento module that takes an input and creates a new item off that input. Everything works fine, and I wanted to add a field that would input an image with the item. I made sure to add the corresponding database column, and made sure to make the change every where it would be needed, but when i upload an image through the new input the image field does not send in my post data. I still get an object with all the fields and values from my form but the File field is always omitted. Using $_FILE to try to access it yields nothing also.
Here is the form:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/edit', array('embroidery_id' => $this->getRequest()->getParam('embroidery_id'))),
'method' => 'post',
'enctype' => 'multipart/form-data'
));
$form->setUseContainer(true);
$this->setForm($form);
$fieldset = $form->addFieldset(
'general',
array(
'legend' => $this->__('Details')
)
);
And here are the fields:
$this->_addFieldsToFieldset($fieldset, array(
'name' => array(
'label' => $this->__('Name'),
'input' => 'text',
'required' => true,
),
'file_t' => array(
'label' => $this->__('File path'),
'required' => false,
'class' => 'disable',
'input' => 'file',
'name' => 'file_t',
'value' => 'file_t',
),
));
Any help or guidance would be appreciated. i already did some googling and looked at all the top results for adding in a file upload field, and I copied some of the code exactly but the post field for the file input is still empty.
For Clarification:
The _addFieldsToFieldset function just loops through the fields array and either fills them in with data or adds the field onto the $fieldset variable. I also tried adding the field straight onto the variable with the following code but it did not work also.
$fieldset->addField('file_t', 'file', array(
'label' => $this->__('File path'),
'required' => false,
'name' => 'file_t',
));
Here is the function that handles the saving. This function does work, it has been tried and tested multiple times when I submitted other forms, and all the fields were successfully saved, and everything was sent through, but whenever I add the file field on, the post data looks exactly the same, and everything still works, it just does not send the file field through the post.
if(isset($_FILES['file_t']['name'])){
echo 'TRUE';
}
var_dump($this->getRequest()->getPost()); exit();
$em->addData($postData);
$em->save();
$this->_getSession()->addSuccess(
$this->__('The embroidery has been saved.')
);
// redirect to remove $_POST data from the request
return $this->_redirect(
'mu_em_admin/embroidery/edit',
array('embroidery_id' => $em->getID())
);
I simplified alot of the code down to only include the important parts. I also have the var_dump function there to troubleshoot the code. The results of var_dump were the whole post array that was sent, except for the file field.
please try to put this form before body end and then submit.
what web browser do you use, and which version of php?
I want to get the selected schoolFilter form value and place in [SELECTED VALUE HERE].
$data = $this->Js->get('#SchoolFilter')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#SchoolSchoolId')->event(
'change', $this->Js->request(
array('action' => 'assign_school_ads/', [SELECTED VALUE HERE], array(
'update' => '#results',
'data' => $data,
'async' => true,
'dataExpression' => true,
'method' => 'POST'
)
)
);
// School Filter
$schoolFilter = $this->Form->create('School', array('id' => 'SchoolFilter');
$schoolFilter .= $this->Form->input('school_id', array('label'=>'Schools', 'empty'=>'- select -');
$schoolFilter .= $this->Form->end();
I have seen variations on this question but without any clear answer, except to just forget using JS Helper. Is it possible within the context of JS Helper? And if not, can I get the value using regular JQuery, then inject it into JS Helper.
Use following code :-
$this->Js->get('#SchoolFilter');
$this->Js->event('change', $this->Js->request(array('controller'=>'put-ur-controller-ame-here','action' => 'assign_school_ads'),array('async' => true,'update' => '#results','method' => 'post','dataExpression'=>true,'data'=> $this->Js->serializeForm(array('isForm' => true,'inline' => true)))));
the serialize() function sends the form data to the php action so we can see which option was selected and decide what to update in the ajax call.
the form data will be found in $this->data in the action (just like after a form has been submited).
Don't forget to add $this->Js->writeBuffer(); in your layout just before the body closing tag. Otherwise all the ajax code will not be added to your page.
I am using hook_menu to setup a link which I'm calling via JQUERY from an AJAX application.
My hook implementation is as follows:
function staff_filter_menu(){
$items = array();
$items['staff/filtering/results/%'] = array(
'page callback' => 'staff_filter_function',
'page arguments' => array(3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'delivery callback' => 'staff_filter_deliver',
);
return $items;
}
This link is the point of contact for my ajax app which is a simple textfield where users type in some input. *staff_filter_function* searches the database and returns some matching data. Ajax code is as follows:
$.ajax({
url: Drupal.settings.CALL_BACK + '/' + $(this).val(),
success: function($data){
.... more stuff done here
}
});
It all works perfectly well but for one small detail.
I want to allow a shortcut which will allow all results to display. This shortcut is simply typing a single space.
But, because hook_menu is expecting an argument at the end of the url, my ajax request fails whenever I type in a space. It works perfectly when I type in real content.
This is just for completeness and it a nice to have feature ... if anyone can share some ideas, I'll be very great greatful.
Maybe you don't need to add an argument at the end of the URL.
Just make change you code to the following:
function staff_filter_menu(){
$items = array();
$items['staff/filtering/results'] = array(
'page callback' => 'staff_filter_function',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'delivery callback' => 'staff_filter_deliver',
);
return $items;
and make a slight change to your page callback function
function staff_filter_function($my_arg = "")
{
// your code goes here
}
and you can treat $my_arg as your page argument.
Hope it works,
Muhammad
I had created several modules extending the UI elements of the FormAPI. This works fine when I render forms in a normal way (not AJAX). But if the rendered form is delivered though the AJAX request, JS script simply inserts the code via the $.html function. So I can't access elements of the document by their ID from the script evaluated by the $.html.
Is there any solution to pass JavaScript code through the Form/Ajax API?
I think jQuery("#new-id") (not $(..)) should take the newest dom elements.
You can use either the 'ajax' property of form api
$form['myitem'] = array(
'#type'=>'textfield',
'#ajax' => array(
'callback' => 'my_callback',
'wrapper' => 'new-id',
'method' => 'html',
'effect' => 'fade',
),
);
$form['myitem2'] = array(
'#type'=>'markup',
'#value'=>"<div id='new-id'> I'm #new-id </div>"
);
function my_callback(&$form, &$form_state){
$commands = array();
$commands[] = ajax_command_invoke('#new-id', 'html', array('<div id="new-id2">#new.id2 here!</div>'));
$commands[] = ajax_command_invoke('#new-id2', 'addClass', array('error'));
return array('#type' => 'ajax', '#commands' => $commands);
}
D7 ajax_commands:
http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax_commands/7
hope this help
I'm trying to get an AJAX-submitted (AHAH) form working to display in a block on the sidebar. For testing purposes, I'm using an example module called "Poof" from the Pro Drupal Development book: http://books.google.com/books?id=VmZrdGuBZCMC&lpg=PA269&ots=cnHiYG6kXn&dq=pro%20drupal%20development%20poof&pg=PA269#v=onepage&q=pro%20drupal%20development%20poof&f=false
The only thing I've added to the example so far is an implementation of hook_block, which looks like this:
function poof_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('poof');
return $blocks;
case 'view':
$block['content'] = drupal_get_form('poof_form');
return $block;
}
}
The AJAX module works fine when displaying on its own page (mydrupalsite.com/poof) but when I call the form with module_invoke('poof', 'block' ...) in a template file, the form submits as normal (sans AJAX) and refreshes the page.
I can't find a definitive answer for why this happens, though a found something tangentially related that suggests that maybe AHAH doesn't work within blocks. If that's so, why? Or better yet, what's a work-around. Do I have to put the on its own blank page and bring it in with an iframe? That sounds unnecessarily messy.
UPDATED:
Here's more code for reference (again, it's from the Pro Drupal book)
function poof_form() {
$form['target'] = array(
'#type' => 'markup',
'#prefix' => '<div id="target">',
'#value' => t('Click the button below.'),
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'button',
'#value' => t('Click Me'),
'#submit'=>false,
'#ahah' => array(
'event' => 'click',
'path' => 'poof/message_js',
'wrapper' => 'target',
'effect' => 'fade',
),
);
return $form;
}
function poof_message_js() {
$output = t('POOF!');
drupal_json(array('status' => TRUE, 'data' => $output));
}
Try adding
$blocks[0]['cache'] = BLOCK_NO_CACHE;
to your hook_block implementation.
Rendering a form with ahah causes a call to drupal_add_js to add the ahah javascript, but while the output of the block is cached, the javascript that gets added to the page doesn't.