I have created a form through structure in backened
1. field_dimensions from physcial module.
2. then added image field field_image
3. through form_alter added ajax button. below mention
$form['getquote_fs']['getquote']['get-quote-btn'] = array(
'#type' => 'button',
'#name' => 'estimate',
'#button_type' => 'button',
'#executes_submit_handler' => false,
'#limit_validation_errors' => array(array('field_dimensions')),
'#size' => 5,
'#weight' => 100,
'#default_value' => '<span class="bold sd-red">get</span> <span class="normal sd-orange">estimate</span>',
'#maxlength' => 5,
'#description' => t('an industry estimate of how much would I pay.'),
'#ajax' => array(
'callback' => 'custom_startrack_ajax_cal',
'method' => 'replace',
'prevent'=> 'sumbit',
),
);
Now when inside the form if I browse image and press either of the other ajax button.Its directly uploading the image.How i can remove it from being uploading in folder
you can disable the behavior of buttons easily with jquery.
try one of these:
$("#your-button-id").submit(function(e){
return false;
});
or
$("#your-button-id").submit(function(e){
e.preventDefault();
});
Related
Does form controls in Forms API have id's? Below is my sample code:
function myid_user_page_form(){
$form = array();
$form['id'] = array(
'#type' => 'fieldset',
'#title' => t('ID Information'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['id']['myphoto_button'] = array(
'#type' => 'button',
'#value' => '...',
'#attributes' => array(
'onclick' => "myphoto_options();",),
);
return $form;
}
Sorry for this very simple beginner's question but how to identify my button's id in the sample above(e.g) $form['id']['myphoto_button']?
The #attributes property is used to set html attributes for the element. (Like, id, class, style, onclick, etc.)
I can see that you are using it to bind onclick handler. So, it order to give your button an id:
$form['id']['myphoto_button'] = array(
'#type' => 'button',
'#value' => '...',
'#attributes' => array(
'onclick' => "myphoto_options();",
'id' => 'YOUR-BUTTON-ID',
),
);
I am creating a custom module. I have a form that I would like to add some HTML to, but I don't know what the best way to do this is. In this example, I have a page with a textbox, dropdown list, and text area. I want to add a a div between the dropdown list and text area. But, I'm not sure how to add raw html to a web form. Here is what I have:
function myModule_add_form($form_state){
try{
$form = array();
$form['myModule_title'] = array(
'#title' => 'Title',
'#type' => 'textfield',
'#size' => '30',
'#weight'=>1,
);
$form['myModule_type_list']=array(
'#type'=>'select',
'#title' => 'Type List',
'#options' => $someArray,
'#multiple' => false,
'#attributes'=>array('size'=>1),
'#weight'=>2,
);
$form['myModule_description'] = array(
'#title' => 'Description',
'#type' => 'textarea',
'#size' => '255',
'#weight'=>3,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#weight'=>4,
);
return $form;
}catch(Exception $e){
$errrmsg = "Error with creating form: " .$e->getMessage();
throw New Exception($errrmsg);
}
}
Thanks
jason
Here is the reference for this:
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/6
I think the best option is the markup form type (This is the default type) so if you do not set the 'type' it will be markup, so you can simply insert something like this wherever you want:
$form['some_div'] = array(
'#value' => '<div>The div</div>',
);
The other option is to use prefix or suffix as also mentioned in the form API, e.g.:
$form['myModule_type_list']=array(
'#type'=>'select',
'#title' => 'Type List',
'#options' => $someArray,
'#multiple' => false,
'#attributes'=>array('size'=>1),
'#weight'=>2,
'#suffix' => '<div>The div</div>',
);
I added the div as a suffix of your list, or it could be a prefix of the text area.
Another way of doing the first option which you may prefer for readability is like this:
$form['some_div'] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#value' => 'The div content',
'#suffix' => '</div>',
);
I made a form module (only segment shown) and would like to add some text between questions in the form so I wrote the following, but no text 'text is here' appears.
... $form['name1'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
?><html><p>Text is here </p></html><?
$form['name2'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE, ...
One of the reasons why that will not work is because, in your function you are building a form array for Drupal to render not actually outputting the form yourself. so when you break out of the php and output the HTML, it will be output when Drupal runs your function.
What you'll want to do is add a form item of type markup. See the form API
Also, semi-unrelated the #collapsible and #collapsed properties only apply to fieldsets.
So your code would like something like this.
$form['name1'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
$form['betweenfields-html'] = array(
'#value' => '<p>Text is here </p>',
);
$form['name2'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
How can I add an onchange handler to a dropdown list in Drupal? The dropdown list is added using hook_form(). I have to perform a function depending on the onchange function.
Is there a way to do this?
You can add a form like this:
hook_form()
{
$form1["dropdown"] = array(
'#type' => 'select',
'#title' => t('Preview the page with themes available'),
'#options' => $ptions,
'#default_value' => 'defalut_value',
'#attributes' => array('onchange' => "form.submit('dropdown')"),
);
//Submit button:
$form1['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit '),
'#attributes' => array('style' => 'display: none;'),
);
Now you can add submit functionality using hook_submit().
Here is the simple example using '#ajax' property
$form['select'] = array(
'#type' => 'select',
'#title' => 'Option #1',
'#options' => $option,
'#ajax' => array(
// Call function that rebuilt other field
'callback' => 'ajax_load_field',
'method' => 'replace',
// div to be get replace by function output
'wrapper' => 'chart',
'effect' => 'fade'
),
);
If you generate another form field by using this drop down.
Then use AHAH for that.
$form['my_form_submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 1,
'#submit' => array('my_form_submit'),//none JS version
'#ahah' => array(
'event' => 'click',
'path' => 'mymodule/js', //Your ajax function path
'wrapper' => 'myform-wrapper',
'method' => 'replace',
'effect' => 'fade',
'progress' => array(
'type' => 'bar',
'message' => t('Loading...')
)
),
While I am sure that Nicholas is way past this issue by now, this may help some who are searching for a solution.
I'm using D7 and getting the single quotes around dropdown encoded to ' I suppose by >check_plain. >How do I avoid that? – Nicholas Tolley Cottrell Jun 2 at 16:03
I just found the "Drupal" way of doing this.
Step 1, set a variable to contain dropdown by using drupal_add_js:
drupal_add_js(array('mymodule' => array('varname' => 'dropdown')), 'setting');
Step 2, add the attributes line as
'#attributes' => array('onchange' => "form.submit(Drupal.settings.mymodule.varname)"),
When I submit the form without filling in one of the required fields( or any combination of required fields) there is no status message presented to let me know I have missed the required fields.
The second time I submit the form the status message shows me what fields are required.
The status message seems to be one step behind the form submission.
If after the first submit I change what fields are filled in and I submit again then the status message that should have show the previous time will show now.
When the form is filled in correctly it submits as normal.
The form is displayed using drupal_get_form( 'otherWaysToRequest' );
This is called in a template file in the theme.
Does anyone know why the status message is one step behind?
This is a sample of the code being used
function otherWaysToRequest(&$form_state)
{
global $base_url;
$pathToTheme = path_to_theme();
$form['top-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
);
$form['top-check']['gift'] = array(
'#title' => t('Included a gift'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['top-check']['contact'] = array(
'#title' => t('I would like to speak to you'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => false,
);
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => true,
);
$form['email'] = array(
'#title' => t('Email Address'),
'#type' => 'textfield',
'#required' => true,
);
$form['bottom-check'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => 'checkboxes'),
'#description' => t('<p class="Items">If you have ...:</p><p class="Items">I have included .....</p>')
);
$form['bottom-check']['share'] = array(
'#title' => t('A Share'),
'#type' => 'checkbox',
'#suffix' => '<br />',
'#required' => FALSE,
);
$form['submit'] = array(
'#type' => 'image_button',
'#src' => $pathToTheme.'/image.gif',
'#value' => t('Submit Form'),
);
}
function otherWaysToRequest_validate($form, &$form_state)
{
$mail_reg_ex = '/[-a-zA-Z0-9._]+[#]{1}[-a-zA-Z0-9.]+[.]{1}[a-zA-Z]{2,4}/';
if(!preg_match($mail_reg_ex, $form_state['values']['email']))
{
form_set_error('email', t('Invalid email address.'));
}
if( 0 == $form_state['values']['gift'] & 0 == $form_state['values']['contact'] )
{
form_set_error('gift', t('You must choose one of the first two options on the form'));
}
}
function otherWaysToRequest_submit($form, &$form_state)
{
//mail details
}
It's because by the time you're calling drupal_get_form in your template file the messages have already been committed for the current page; your validation messages will therefore show up the next time messages are displayed to the screen which is on the next page load.
You should build up the form in a custom module rather than the theme to get around this. The easiest way would be to create a block which you can assign to a region (using either hook_block in Drupal 6 or a combination of hook_block_info() and hook_block_view in Drupal 7).