sending form values $form_state['redirect'] Form API Drupal - forms

I want to send my form values using form API drupal. I have following value
$form['billing']["cardholders_name"] = array(
'#type' => 'textfield',
'#title' => t("Cardholder's Name"),
'#required' => TRUE,
'#prefix' => '<div class="field-wrapper-w1 card-name">'
);
I am writing following code in my form submit function
function test_form_submit($form, &$form_state) {
$form_state['redirect'] = 'www.test.com/page' . '?cname=' . $form_state['values']['billing[cardholders_name]'];
}
But it seems like it is not working. Please help

The following example appears in
http://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7
$form_state['redirect'] = array(
'node/123',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
),
);

Any value for form element will be in $form_state under values so in your function test_form_submit you can access cardholders_name
$form_state['values']['cardholders_name']
also you can do it in this way using drupal_goto()
drupal_goto('www.test.com/page' . '?cname=' . $form_state['values']['cardholders_name]);

Related

ReCaptcha with ZF3 wrong value

I use the newest Zend Framework available, and now i want to use ReCaptcha on my form. Together with some other elements, the ReCaptcha element is defined by:
$pubKey = 'replaced by the actual pubkey';
$privKey = 'replaced by the actual privkey';
$recaptcha = new \Zend\Captcha\ReCaptcha(['pubKey' => $pubKey, 'privKey' => $privKey]);
$this->add(array(
'attributes' => array (
'data-role' => 'none',
),
'name' => 'captcha',
'type' => 'captcha',
'options' => array(
'captcha' => $recaptcha,
),
));
This code validates the form in the controller:
public function contactAction () {
$contactForm = new ContactForm();
if($this->getRequest()->isPost()) {
$contactForm->setData($this->getRequest()->getPost());
if($contactForm->isValid()){
// send actual mail
return $this->redirect()->toRoute('page', ['lang' => $this->translator->getLocale(), 'page' => 'contact']);
}
}
$viewModel = new ViewModel ([
'contactForm' => $contactForm
]);
$viewModel->setTemplate('application/index/contact');
return $viewModel;
}
And finally, this is the view:
<?= $this->form($contactForm); ?>
To me, this code is pretty straightforward and should work. However, on sending the contact form, it displays the error 'Captcha value is wrong'. Any ideas?
You have to name the element according to the rules of Google. With this code, it works like a breeze
$pubKey = 'replaced by the actual pubkey';
$privKey = 'replaced by the actual privkey';
$recaptcha = new \Zend\Captcha\ReCaptcha(['pubKey' => $pubKey, 'privKey' => $privKey]);
$this->add(array(
'name' => 'g-recaptcha-response',
'type' => 'captcha',
'options' => [
'captcha' => $recaptcha,
]
));
Anyways, as always the ZF Docs are very short and lack examples.

Drupal custom form and autocomplete issue

Some how I managed to get it work. But still the result is not coming along with the autocomplete.
Posting my latest code now,
the textfield code
$form['town'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#autocomplete_path' => 'hfind/town/autocomplete',
);
menu function code
function hfind_menu() {
$items = array();
$items['hfind/town/autocomplete'] = array (
'title' => 'Autocomplete for cities',
'page callback' => 'hfind_town_autocomplete',
'access arguments' => array('use autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
the callback function code
function hfind_town_autocomplete($string){
$matches = array();
$result = db_select('towns', 't')
->fields('t', array('town_name'))
->condition('town_name', '%' . db_like($string) . '%', 'LIKE')
->execute();
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
drupal_json_output($matches);
}
I hope this may the final edit.
The current situation is, autocomplete is working
The url is hfind/town/autocomplete/mtw
but it is not able to find any data from the database. I found why and unable to fix it.
It is because in the last function I've added above the $string needs to be the 'search query' but it is always querying the database as 'autocomplete'. I mean the $string variable always having the value 'autocomplete' instead of user typed value.
One more problem is, even after providing the permission to all types of user to access search autocomplete on the forms, guests users are not able to use the feature.
Please please someone help me..
`drupal_json_output()` instead of `drupal_to_js` and remove `print` .
<code>
hook_menu() {
$items['cnetusers/autocomplete'] = array(
'title' => 'Auto complete path',
'page callback' => 'cnetusers_employees_autocomplete',
'page arguments' => array(2, 3, 4, 5),
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
);
return $item;
}
// my autocomplete function is like this
function cnetusers_employees_autocomplete() {
// write your sql query
$matches["$record->ename $record->elname [id: $record->uid]"] = $value;
}
if (empty($matches)) {
$matches[''] = t('No matching records found.');
}
drupal_json_output($matches);
}
$form['disc_info']['approval'] = array(
'#type' => 'textfield',
'#title' => t('Approval By'),
'#autocomplete_path' => 'cnetusers/autocomplete',
);
</code>

#value not being changed in custom drupal form

I have created a custom drupal 7 form with a submit button. when submit is pressed the values in $form_state['values'] are not changed. see the code below:
$form[$tag] = array(
'#title' => t($tag),
'#type' => 'textfield',
'#default_value' => !empty($form_state['values'][$tag]) ? $form_state['values'][$tag] : $value,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 5,
'#submit' => array('xml_form_builder_edit_datastream_form_submit'),
);
Then the submit function is:
function xml_form_builder_edit_datastream_form_submit($form, &$form_state){
dsm('SUBMITTED');
dpm($form);
dpm($form_state);
dsm('SUBEND');
}
the $form_id is 'xml_form_builder_edit_datastream_form'.
Ive been trying to figure this out for a good few hours.
Anyone got any ideas?
Try $form_state['input'][$tag] , that should have the user input value.

Passing URL parameters to a Drupal form created in a menu item

I am using Drupal 7 and have a custom module where I am creating a menu item which will display a Drupal form. The code is setup like this:
$items['job-seekers/update-resume/%'] = array(
'title' => 'Update Resume',
'page callback' => 'drupal_get_form',
'page arguments' => 'oa_onboard_update_resume_form',
'access arguments' => array('access content'),
'page arguments' => array(2),
);
So my menu item will display a form, but I need to take the parameter passed in the URL and have it stored in a hidden form field that will end up getting passed to the form submission method for processing. My form code looks like this:
/**
* Form callback for updating resume
*/
function oa_onboard_update_resume_form($form_state) {
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
So it's a very simple form that is just a file upload field, however I can't figure out how to gain access to the ID that is passed in the URL. Originally I was thinking I needed to account for it in the form method like so:
function oa_onboard_update_resume_form($candidateId) {
But I get an error when I try to do that:
Notice: Undefined index: 20249278 in drupal_retrieve_form() (line 763 of C:\xampp\htdocs\mysite\includes\form.inc).
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function '20249278' not found or invalid function name in drupal_retrieve_form() (line 798 of C:\xampp\htdocs\mysite\includes\form.inc).
I think I might be going about this the wrong way. The bottom line is I just need to create a URL that I can pass an ID to that will display a form and pass that ID to the submit method along with the file upload.
Thanks in advance.
EDIT
Here is the new callback function that renders the form after fixing the menu item per the suggestion below:
function oa_onboard_update_resume_form($candidateId) {
$form['candidateId'] = array(
'#type' => 'hidden',
'#value' => $candidateId,
);
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
In the hook_menu function you have twice set 'page arguments' and you should use the 'page callback' with the function oa_onboard_update_resume_form.
$items['job-seekers/update-resume/%'] = array(
'title' => 'Update Resume',
'page callback' => 'oa_onboard_update_resume_form',
'access arguments' => array('access content'),
'page arguments' => array(2),
);
OK so this is what ended up working:
$items['job-seekers/update-resume/%'] = array(
'title' => 'Update Resume',
'page callback' => 'drupal_get_form',
'page arguments' => array('oa_onboard_update_resume_form', 2),
'access arguments' => array('access content'),
);
function oa_onboard_update_resume_form($form, &$form_state, $candidateId) {
// I can use candidateId here now
}

drupal questions - how to print a form from another module?

I'm trying to include a module form in a panel and I've tried using drupal_get_form(), but not sure I'm using it correctly.
In the organic groups module, there's a function to render an og_broadcast_form. It's called within a page_callback in og.module:
// Broadcast tab on group node.
$items['node/%node/broadcast'] = array(
'title' => 'Broadcast',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_broadcast_form', 1),
'access callback' => 'og_broadcast_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'file' => 'og.pages.inc',
'weight' => 7
);
And in og.pages.inc, the function is:
function og_broadcast_form($form_state, $node) {
drupal_set_title(t('Send message to %group', array('%group' => $node->title)));
if (!empty($form_state['post'])) {
drupal_set_message(t('Your message will be sent to all members of this group.'));
}
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 70,
'#maxlength' => 250,
'#description' => t('Enter a subject for your message.'),
'#required' => TRUE,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#rows' => 5,
'#cols' => 90,
'#description' => t('Enter a body for your message.'),
'#required' => TRUE
);
$form['send'] = array('#type' => 'submit', '#value' => t('Send message'));
$form['gid'] = array('#type' => 'value', '#value' => $node->nid);
return $form;
}
I've tried a number of variations of drupal_get_form:
print drupal_get_form('og_broadcast_form', NULL, arg(1)); //where arg 1 is the node id from the url
print drupal_get_form('og_broadcast_form');
print drupal_get_form('og_broadcast_form', &$form_state, arg(1));
print drupal_get_form('og_broadcast_form', $n); //where $n is node_load(arg(1));
print drupal_get_form('og_broadcast_form', &$form_state, $n);
etc., etc... Is there a way to accomplish what I'm trying to do here?
FYI.. your problem is that you're attempting to load a form located in another modules include file.
The function is located in og.pages.inc and you'll need to make a call to:
module_load_include('inc', 'og', 'og.pages');
This must be done before you can grab the form.
If drupal_get_form is given the name of a function as it's first argument, that will be both the form_id and a function to be called to generate the $form array.
On line 3 of the function code, we have $args = func_get_args();, this is used by drupal_get_form to collect any or all additional arguments you may want to pass to your form-building function.
You should be using drupal_get_form('og_broadcast_form', node_load(arg(1)));.
Are you sure you should be using print and not return? I have recently learned they do very different things in the theming system. I have used drupal_get_form in this way to populate the contents of a block successfully, but at no point did I print to the screen myself.
EDIT: The full node object and not the nid because %node in a menu path uses a wildcard loader to pass the node_load(arg(1)) on to whatever function is being called.
drupal_get_form gets the form for you. have you tried print drupal_render(drupal_get_form('whatever'))) ?
drupal_get_form() only accepts one argument, the $form_id.
http://api.drupal.org/api/function/drupal_get_form/6
Run a hook_form_alter() and var_dump($form_id). That will give you the $form_id, and when you pass that to drupal_get_form(), it should return the rendered form.