Please include a detailed question with properly formatted forum styles (
Code: Select all
,[img]etc) to encourage members to answer ;)
Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+
Thanks!
You kindly helped with an issue recently. I have now added (Successfully) a 'subject' box in my contact form. Can you help me turn this into a drop down list with some options? Thanks for any assistance.
I added to Catalog/controllor/information/contact.php
if (isset($this->request->post['reason'])) {
$data['reason'] = $this->request->post['reason'];
} else {
$data['reason'] = '';
}
and also added
if ((utf8_strlen($this->request->post['reason']) < 1)) {
$this->error['reason'] = $this->language->get('error_reason');
}
and also ammended
$mail->setText($this->request->post['enquiry'] . to
$mail->setText($this->request->post['enquiry'] . $mail->newline . 'reason: ' . $this->request->post['reason']);
$mail->send();
and also added
$data['entry_reason'] = $this->language->get('entry_reason');
and also added
if (isset($this->error['reason'])) {
$data['error_reason'] = $this->error['reason'];
} else {
$data['error_reason'] = '';
if (isset($this->request->post['reason'])) {
$data['reason'] = $this->request->post['reason'];
} else {
$data['reason'] = '';
}
and also added
if ((utf8_strlen($this->request->post['reason']) < 1)) {
$this->error['reason'] = $this->language->get('error_reason');
}
In the catalog/language/Englis/Information/contact.php i added
$_['entry_reason'] = 'Subject'; under Text
$_['entry_reason'] = 'Subject'; under Entry
$_['error_reason'] = 'Please choose a subject!';
In the catalog/view/theme/default/template/information/contact.tpl I added
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-reason"><?php echo $entry_reason; ?></label>
<div class="col-sm-10">
<input type="text" name="reason" value="<?php echo $reason; ?>" id="input-reason" class="form-control" />
<?php if ($error_reason) { ?>
<div class="text-danger"><?php echo $error_reason; ?></div>
I stress I am not a coder but worked this out and it has done the job well with a static box. However, I would like a drop down list to make reason for contact clearer. Is that possible and what would I alter in the code?
Thanks
Martin
SubatWest
Posts: 30
Joined: Thu Oct 30, 2014 6:29 pm
$_['reason1']='I need a quote';
$_['reason2']='I need an offer';
$_['reason3']='I need more information';
$_['error_reason'] = 'please give us a reason';
controller:
create an array with your choices. I made them multi-lingual
Code: Select all
$data['reasons'] = array(
$this->language->get('reason1'),
$this->language->get('reason2'),
$this->language->get('reason3'),
);
Code: Select all
$data['reason'] = isset($this->request->post['reason']) ? $this->request->post['reason'] : '';
Code: Select all
protected function validate() {
if ($this->request->post['reason'] == '') {
$this->_error['reason'] = $this->language->get('error_reason');
}
///etc
}
Code: Select all
<div class="form-group">
<div class="input-group">
<select title="---select---" class="form-control" name="reason">
<option value="">reasons</option>
<!--here you are looping the array-->
<?php foreach ($reasons as $option) { ?>
<option value="<?php echo $option ?>"
<!-- if someone made a choice and hit save but there was an error, put the posted var back as chosen-->
<?php if ($option === $reason) {
echo ' selected="selected"';
} ?>>
<?php echo $option ?>
</option>
<?php } ?>
</select>
</div>
<!-- this will activate upon error-->
<?php if ($error_reason) { ?>
<div class="label label-danger"><?php echo $error_reason; ?></div>
<?php } ?>
</div>
...and next time please take the time to post a readable query...
Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+
Thanks!
Have made all thge suggested code entries and get the following error:
Parse error: syntax error, unexpected 'protected' (T_PROTECTED) in C:\xampp\htdocs\kevinkirby\vqmod\vqcache\vq2-catalog_controller_information_contact.php on line 211
Have tried to make sure the code is entered at the correct places and rearranged some blocks but no change.
Advice?
Regards and thanks and I do note you comments for future.
Martin
I wrote out of habit _error for a private var. That's probably it.
Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+
Thanks!
Using your starting point there were a number of issues when I finally got it to appear.
This has been the sequence of code I used.
In the catalog/controller/information/contact.php:
Code: Select all
$data['text_reason'] = $this->language->get('text_reason');
$data['entry_reason'] = $this->language->get('entry_reason');
$data['reasons'] = array(
$this->language->get('reason1'),
$this->language->get('reason2'),
$this->language->get('reason3'),
$this->language->get('reason4'),
);
if (isset($this->error['reason'])) {
$data['error_reason'] = $this->error['reason'];
} else {
$data['error_reason'] = '';
}
$data['reason'] = isset($this->request->post['reason']) ? $this->request->post['reason'] : '';
Code: Select all
// Text
$_['text_reason'] = 'Reason for contact';
//Entry
$_['entry_reason'] = 'Reason for enquiry';
//Errors
$_['error_reason'] = 'Please give a reason for contact';
Code: Select all
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-reason"><?php echo $entry_reason; ?></label>
<div class="col-sm-10">
<div class="input-group">
<select title="---select---" class="form-control" name="reason">
<option value="">reasons</option>
<!--here you are looping the array-->
<?php foreach ($reasons as $option) { ?>
<option value="<?php echo $option ?>"
<!-- if someone made a choice and hit save but there was an error, put the posted var back as chosen-->
<?php if ($option === $reason) {
echo ' selected="selected"';
} ?>>
<?php echo $option ?>
</option>
<?php } ?>
</select>
</div>
<!-- this will activate upon error-->
<?php if ($error_reason) { ?>
<div class="label label-danger"><?php echo $error_reason; ?></div>
<?php } ?>
</div>
</div>
Code: Select all
protected function validate() {
if ($this->request->post['reason'] == '') {
$this->_error['reason'] = $this->language->get('error_reason');
}
///etc
}
When the submit button is pressed the 'success' appears and the email is sent and received. However, the reason is not included in the email.
Can I have your commenst artcore as to anything I need to do. Certainly helping me learn a lot of coding!
Thanks
forgot one piece of code in Language:
Code: Select all
$_['entry_reason'] = 'Reason for enquiry';
$_['reason1']='General';
$_['reason2']='Sales';
$_['reason3']='Delivery';
$_['reason4']='Hire';
I only gave an example for it, hence the 'etc...',
You can look at how the original validation() is coded and add the check I provided in that example.
I did forget to initialise the error message, so well done there
PS can't make out the error you got from the docx, my tablet only shows half a blue line...
Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+
Thanks!
Thanks for response.
There is no error in the screenshot the form has the box and drop down. I do not understand your comment as I am just learning. What do you mean by 'Are you calling the validation method from if POST && $this->validation() ?'
Is that why the reason is not appearing in the email that is sent?
Thanks
You can add the 'reason' to the array that's sent to the mail template but you also have to add something to it in order to display it in the actual mail sent.
I think the template is located at catalog/view/theme/default/template/mail/contact.tpl ? or just check the controller which tpl is used. See how it echoes the enquiry variable? Do something similar to $reason.
Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+
Thanks!
OK I have now looked at other coding in the controller and have changed the isset and this has made the check correct. So every thing is working on the form but it is now only the email not including the reason. There is no mail contact.tpl but in the controller there is this:
Code: Select all
$mail->setTo($this->config->get('config_email'));
$mail->setFrom($this->request->post['email']);
$mail->setSender(html_entity_decode($this->request->post['name'], ENT_QUOTES, 'UTF-8'));
$mail->setSubject(html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8'));
$mail->setText($this->request->post['reason']);
$mail->setText($this->request->post['enquiry']);
$mail->send();
So build out a $text var and use this for the setText method
Code: Select all
$text = 'Reason: ' . $this->request->post['reason'] . PHP_EOL;//end of line. Can also use "\r\n" in double quotes!
$text .= $this->request->post['enquiry'];
optional extra texts, you decide:
$text .= 'Thank you for containg us';
...
$mail->setText($text);
$mail->setHTML($text);
But you have to create a valid hml page inculding headers!
Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+
Thanks!
Users browsing this forum: Bing [Bot] and 12 guests