1

I have a Drupal form like this:

    function myblock_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function my_module_form() {

      return drupal_get_form('my_module_my_form');
 }

 function my_module_my_form($form_state) {
      $form['name']['price'] = array(
        '#type' => 'textfield',
        '#title' => t('price:'),
        '#value' =>t('100'),
        '#size' => 6,
        '#field_suffix' => t('$'),
        '#disabled' => TRUE,
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
      );
      return $form;
 }

And i have submit function:

function my_module_my_form_submit($form, &$form_state) {

 $form_state['redirect'] = 'result.php';
}

My problem: how can i get $form_state['values']['price'] inside result.php? or in another way: how should i sent my data to a normal php file? I studied several similar questions but didn't get my answer!
one suggestion was storing data in database which is not proper for my module! another suggestion was using sessions which i couldn't manage it(between module and php file)!

1

If all you are doing in your submit handler is redirecting to result.php then you should instead make your form submit direct to there, so that all the form post values will be available in the $_POST global variable in result.php.

To do so, in your my_module_my_form() function add this:

$form['#action'] = '/result.php'

Edit:

As Clive has pointed out, you cannot post that field value as you currently have it because it is a disabled field.

To make that field post you either have to make it not disabled, or if you don't want the user to be able to edit it you can use a different field type.

You can use type value to pass a value straight through, however the user won't be able to see it before they submit as it is a type of hidden value.

If you want the user to be able to view the value but not change it you could have 2 fields, one disabled (or just an item field) one that the user can see but doesn't submit, and a second value type field that submits the value.

  • thank u for looking into my question. i did what you said. then in result.php I have this code:print_r ($_POST); ... All i get out of that is this(my form data is not among them): Array ( [op] => Submit [form_build_id] => form-K1w83FqZ4FjzPOBzacsT9Lh_n_aKZDlpmJzkTr72Tvs [form_token] => rBT-p0wlLH17-z3LJTrmIaxT9LFQyq6EgmZSZoRIf6A [form_id] => my_module_my_form ) – PersianMan Mar 26 '14 at 7:14
  • 1
    @PersianMan The reason you're missing $_POST['name'] is that disabled form elements cannot be part of the form submission. This is by design, and a security feature of HTML. – Clive Mar 26 '14 at 8:46
  • 1
    I added some info to my answer. – rooby Mar 26 '14 at 14:09
  • Thanks a million! the problem solved but another one raised! i have to pass a price to bank gate by post method! i dont want my users to edit the price!!! if i use hidden element they can change it by inspect element! as far as i know i should have something like : $form['price'] = array('#type' => 'value'.... user can not see and edit that but it is not passed in post method as well :( i guess i have to use form Validation. am i right? is that the proper and safe way? – PersianMan Mar 26 '14 at 19:27
  • value fields should post through. Another way is using a text field and adding the readonly attribute using the form api #attributes. value type should be safest. If a user is smart enough to know what is being posted it is possible they can get around any of this though. That is why drupal also uses form tokens to help stop forged requests. Because you are posting to a custom php page you won't be getting any of that benefit. To get that benefit I would not use a custom php file but instead do whatever you are doing in that file in your form submit handler. That's a separate issue though. – rooby Mar 26 '14 at 23:58
1

the answers of rooby and clive were right but just solved that by using session as well and want to share it here: inside your module add this:

if (isset($_SESSION['myvalue'])){
    unset($_SESSION['myvalue']);
}
$_SESSION['myvalue'] = t('your data.....');

and inside your php file:

<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

print_r($_SESSION['myvalue'] );

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.