Custom form posting "Array" rather than user input values

365 views Asked by At

We are using the Aheadworks Helpdesk Module and are trying to create a second form to capture specific information and use the form to create a ticket where all of the form content gets posted to the "content" section of Helpdesk.

The problem is, if I use the name="content", what gets posted into the "content" section is simply "Array"

The form code is quite simple:

<form id="helpdesk-ticket-form" action="../helpdeskultimate/customer/new/" method="post" enctype="multipart/form-data"><input name="title" value="WHOLESALE SETUP REQUEST" type="hidden">
<div><label for="title_field">Name<span class="required">*</span></label><br> <input id="title" class="input-text required-entry" style="width: 250px;" name="" value="" type="text"></div>
<div>&nbsp;</div>
<div><label for="title_field">Company Name<span class="required">*</span></label>    <br><input id="content_field" class="input-text " title="Company" name="content" value="" type="text"></div>
<input name="department_id" value="2" type="hidden">
<div>&nbsp;</div>
<div><label for="content_field">Message<span class="required">*</span></label><br>     <textarea id="content_field" class="required-entry" style="width: 450px;" name="content" rows="10" cols="53"></textarea></div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><label for="filename">Attach Reseller Permit (2Mb limit)</label><br> <input id="filename" class="input-file" style="width: 450px;" name="filename" type="file"></div>
<div class="button-set">&nbsp;</div>
<div class="button-set"><span><span><br></span></span></div>
<div class="button-set"><button class="button right form-button" type="submit"><span>  <span>Submit ticket</span></span> </button></div>
</form>

I have tried using name="content[]" but it also returned "Array"

The module looks to be using this post method:

public function newAction()
{
    if (!$this->_getCustomerSession()->getCustomerId()) {
        $this->_getCustomerSession()->authenticate($this);
        return;
    }
    $session = Mage::getSingleton('core/session');
    $customer = $this->_getCustomerSession()->getCustomer();

    $Proto = Mage::getModel('helpdeskultimate/proto');
    $postData = $this->getRequest()->getPost();

    if (isset($postData['department_id'])) {
        $Proto->setDepartmentId($postData['department_id']);
    }
    try {

        $Proto
                ->setSubject(@$postData['title'])
                ->setContent(@$postData['content'])
                ->setPriority(@$postData['priority'])
                ->setStoreId(Mage::app()->getStore()->getId())
                ->setFrom(Mage::getSingleton('customer/customer')->getId())
                ->setSource('web');

The insert into the message field seems to come from here:

/* Insert */
    try {
        $message->setContent(trim($data['content']));
        $validateResult = $message->validate();

The full controller file can be downloaded from http://www.gingabox.com/CustomerController.zip

I am not sure how to actually use a foreach statement with the @$postData['content'], or if there is a better solution.

I would happily ask AheadWorks, but have been told by them that they are not accepting customization inquiries at this time (too busy)...

Any help would be greatly appreciated!

2

There are 2 answers

4
Mike Firesheets On

The word "Array" is what you get when you convert a PHP array into a string; since array values can contain anything, PHP doesn't bother trying to figure out how to convert an array and just returns the string "Array". This is exactly what happens in the line:

// The trim() function casts $data as a string => string(5) "Array"
$message->setContent(trim($data['content']));

There are functions that do return a string representation of array contents, such as print_r(). This will spit out the array in a multiline string, so incorporating that in your code would be:

$message->setContent(print_r($data['content'], TRUE));

If you wanted the contents of the array as a single-line string you should probably use a foreach() statement like you mentioned in your question. Here's a quick example:

$contentString = 'Second Form values:' . PHP_EOL;
foreach($data['content'] as $key => $value) {
    $contentString .= PHP_EOL . '    ' . $key . ': ' . $value;
}

Then you would be able to use $contentString as the message instead of accessing the $data array value directly. I don't know what the validate() method is doing in your example, but it is definitely a good idea to ensure that you are properly escaping the values within this second form before you use them as the body of an email.

0
Patryk On

If you can change the form definition in HTML then maybe you will be able to receive an array as content, please have a look at the following example:

<form id="helpdesk-ticket-form" action="tescik.php" method="post" enctype="multipart/form-data"><input name="title" value="WHOLESALE SETUP REQUEST" type="hidden">
<div><label for="title_field">Name<span class="required">*</span></label><br> <input id="title" class="input-text required-entry" style="width: 250px;" name="content[name]" value="" type="text"></div>
<div>&nbsp;</div>
<div><label for="title_field">Company Name<span class="required">*</span></label>    <br><input id="content_field" class="input-text " title="Company" name="content[company]" value="" type="text"></div>
<input name="department_id" value="2" type="hidden">
<div>&nbsp;</div>
<div><label for="content_field">Message<span class="required">*</span></label><br>     <textarea id="content_field" class="required-entry" style="width: 450px;" name="content[message]" rows="10" cols="53"></textarea></div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><label for="filename">Attach Reseller Permit (2Mb limit)</label><br> <input id="filename" class="input-file" style="width: 450px;" name="filename" type="file"></div>
<div class="button-set">&nbsp;</div>
<div class="button-set"><span><span><br></span></span></div>
<div class="button-set"><button class="button right form-button" type="submit"><span>  <span>Submit ticket</span></span> </button></div>
</form>

Please note the changed form names like content[message], content[company] etc. This should resolve to an Array of values.