0

I'm trying to send data from a html form to another drupal page.

The Form (in JavaScript):

var data = { // JS Object with data to send
            priceUnit: '131.97', 
            priceTotal: '131.97', 
            priceCount: '1', 
            articleNumber: 'XYZ'
        }; 
        // Form creation
        var form = document.createElement("form");
        form.style.display = 'none';
        // link to drupal site
        form.action = "../configurator/process_order";
        form.method = "post";
        form.target = "_blank";

        // Create form Content from JS Object data
        for (var key in data)
        {
            var input = document.createElement("input");
            input.name = key;
            input.value = data[key];
            form.appendChild(input);
        }
        document.body.appendChild(form);
        form.submit();   

On the other page, I am trying to access php $_POST but there is no output:

<div>
     <?php
         echo $_POST['priceUnit']; 
     ?>
</div>

But, if I add var_dump($_POST); in my module: configurator.module:

function configurator_process_order()
{
    var_dump($_POST);


    $modulePath = drupal_get_path('module', 'configurator');

    $content = file_get_contents($modulePath . "/Trader/PHPWebPages/processOrder.php");
    return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

I get the following output:

array (size=4)
  'priceUnit' => string '131.97' (length=6)
  'priceTotal' => string '131.97' (length=6)
  'priceCount' => string '1' (length=1)
  'articleNumber' => string 'XYZ' (length=3)

So the form has submitted it's data but I cannot access it on my drupal page.
What am I doing wrong?

0

Your code:

<div>
     <?php
         echo $_POST['priceUnit']; 
     ?>
</div>

is probably in one of the tpl.php files. They are cached. You are not passing any data from any module to your template file, so Drupal cannot know it is not supposed to be cached. That's all.

Access post data in your module file, and pass it to your template properly, the "Drupal way" you can see in Examples, and you'll be good.

Also, I strongly suggest to remember that POST data is user-agent provided and you should never trust it. Especially not for things like price! There is a Drupal Form API to automate tests, but what you really should pass around is item's ID, and it's price should always come from your database, not from your user.

Price supplied by client is a recipe for a legal disaster.

  • Thank you for your answer. This part of the code is in a external php web page which gets loaded into drupal in the module: $content = file_get_contents($modulePath . "/Trader/PHPWebPages/processOrder.php"); How do I pass data to this page the "Drupal way"? I can't find any useful examples. Can please give me a hint? Thanks in Advance – Jbartmann May 12 '14 at 12:52
  • @Jbartmann I have no idea what exactly you are trying to accomplish, so I honestly can't point you in the right direction. I refuse to help with passing price in POST, in any way. And total redesign of your application is too broad for this site. – Mołot May 12 '14 at 12:55

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.