I was looking in implement a form that collect a customers name and email information by writing the information into a CSV file.
I'm completely new to Magento and got help with the following code via my previous question.
I've written a form for the frontend (second code below) but I'm have issues with the post data and how to handle it. I don't think the form is working because I'm not handling the information correctly.
As you can see I tried creating a CSV file named 'getfirmwarecustomerinfo.csv' in the module's controller (wasn't sure whether I was supposed to use $this->getRequest()->getPost()
or $this->getRequest()->getParams()
.
The person who helped mentioned adding information mentioned I need code to validate post data and to save post data to CSV file.
app/code/local/Myname/Mymodule/controllers/IndexController.php
<?php
class Myname_Mymodule_IndexController extends Mage_Core_Controller_Front_Action {
public function saveAction() {
$postData = $this->getRequest()->getPost();
//$params = $this->getRequest()->getParams();
$file = fopen('getfirmwarecustomerinfo.csv', 'w');
$info = array($postData['firstname'], $postData['lastname'], $postData['useremail']);
fputcsv($file, $info);
fclose($file);
header("Content-type: text/csv");
header("Content-disposition: attachment; filename = getfirmwarecustomerinfo.csv");
readfile("getfirmwarecustomerinfo.csv");
// afterwards, show a nice message & redirect the user to the previous page
$session = Mage::getSingleton('core/session');
if($success) {
$session->addSuccess(Mage::helper('myname_mymodule')->__("IT WORKED"));
} else {
$session->addError(Mage::helper('myname_mymodule')->__("Something went wrong."));
}
$this->_redirectReferer();
}
}
For the frontend, I found this piece of code from Inchoo. Most of the code is the same, I just added the <?php echo $this->getUrl('myname_mymodule/index/save', array('_secure'=>true)) ?>
but I'm still not sure how the two files are connected?
app/design/frontend/base/default/templates/myname_mymodule/form.phtml
<form name="test-form" id="my-custom-form" action="<?php echo $this->getUrl('myname_mymodule/index/save', array('_secure'=>true)) ?>" method="post">
<label for="firstname"><?php echo $this->__('First name') ?> <span class="required">*</span></label><br />
<input id="firstname" name="firstname" class="input-text required-entry" /><br />
<label for="lastname"><?php echo $this->__('Last name') ?> <span class="required">*</span></label><br />
<input id="lastname" name="lastname" class="input-text required-entry" /><br />
<label for="useremail"><?php echo $this->__('Email') ?> <span class="required">*</span></label><br />
<input type="text" name="useremail" id="useremail" class="input-text required-entry validate-email" /><br />
<input type="submit" name="submit" value="<?php echo Mage::helper('contacts')->__('Submit') ?>" />
</form>
<script type="text/javascript">
//< ![CDATA[
var customForm = new VarienForm('my-custom-form');
//]]>
</script>
Any explanation of what I'm doing wrong would be appreciated!