How to get values of multiple select in contact form magento? - magento

I have a code like this on my form.phtml, its a custom multiple select.
<select name="projecttype[]" multiple="multiple">
<option value="" selected>Select...</option>
<option value="Kitchen remodeling">Kitchen</option>
<option value="Refacing">Reface</option>
<option value="Counter tops">Counters</option>
<option value="Bathroom remodeling">Bathrooms</option>
<option value="Flooring">Floors</option>
<option value="Lighting">Lights</option>
<option value="Other">Others</option>
</select>
Now my problem is, how could I get those values if lets say I select "Floors and Lights" in my contact form. Because in normal select option, I could have a Project Type: {{var data.projecttype}} in custom form Email Template. But this one is a multiple select. This is my first time to encounter a problem like this but I can't find a good source as my guide on internet that something a good guide for this. Hope someone could help and guide me with this.

If you need to get value of multiselect box, you can use jQuery for that. See the below sample code.
<select id="multipleSelect" name="projecttype[]" multiple="multiple">
<option value="" selected>Select...</option>
<option value="Kitchen remodeling">Kitchen</option>
<option value="Refacing">Reface</option>
<option value="Counter tops">Counters</option>
<option value="Bathroom remodeling">Bathrooms</option>
<option value="Flooring">Floors</option>
<option value="Lighting">Lights</option>
<option value="Other">Others</option>
</select>
<div onclick="javascript:getValues();">TEST ME</div>
<script type="text/javascript">
function getValues(){
var selectedValues = jQuery('#multipleSelect').val();
alert(selectedValues);//alert if you want to
}
</script>
When you click on TEST ME <div>, you can see the values selected.
The above is a demo code. You can change it as per your needs.

Did you tried in this way...
foreach ($_GET['projecttype'] as $selectedOption)
echo $selectedOption."\n";

I came up to my mind to create a new phtml, and i just make my own mail and add this code
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$hear = $_POST['hear'];
$projecttype = implode(', ', $_POST['projecttype']);
$comment = $_POST['comment'];
$to = 'webdev2all#gmail.com';
$subject = 'Contact Form';
$message = 'Name: ' . $name . "\r\n" .
'E-mail: ' . $email . "\r\n" .
'Telephone: ' . $telephone . "\r\n" .
'How did you hear about us: ' . $hear . "\r\n" .
'Project Type: ' . $projecttype . "\r\n\r\n" .
'Comment: ' . $comment;
$headers = 'From: '. $name .' '. $email . "\r\n" .
'Reply-To: webdev2all#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
I also change my form action to
action="<?php echo $actual_link; ?>"
now my contact page form is working as I needed...
thanks for the replies...

Related

How to prepopulate a cascaded drop down and attachment on validation failure

I have 2 drop downs, just for simplicity let's say they are Country / States.
<select id="country_id" name = "country_id">
<option value="">Select...</option>
<?php foreach ($countries as $country): ?>
<option value="<?php echo $country->id; ?>" <?php if (null !== set_value("country_id") && set_value("country_id") == $country->id) : echo ' selected '; ?>>$country->name;
<?php endforeach?>
</select>
<select id="state_id" name = "state_id">
<option value="">Select...</option>
</select>
<input type="file" name="attachment_id" value="" />
What I do is when the country is selected I populate the states. This I do using AJAX and ".change" and this works well
The problem I'm having is when I do form_validation and
$this->form_validation->run() == FALSE
Then I reload all controls using set_value, which does work for the first drop down (the countries) just fine, however the second drop down stays empty( as there was no ".change" triggered on the "country_id" to populate "state_id".
Similarly I am asked to re-attach the file
So what I need and is not working is after form_validation fail to
Load up second drop down with data => RESOLVED see below with .trigger event
Select the previous selection int he second drop down => ALMOST RESOLVED however only if I use alert :)
The attachment should still stay attached until form is dismissed or committed successfully => NOT RESOLVED
I feel like I need to manually trigger the data change to get (1) but in my case I have a foreach (see above) where I just add lines and if the selection is the same I simply set is as "selected" but nothing happens after.
Here is the AJAX part
<script>
$(document).ready(function(){
$("#country_id").change(function(){
$.ajax({
url:"<?php echo base_url(); ?>location/getCountries",
data: {country_id: $(this).val()},
type: "POST",
success: function(data){
$("#state_id").html(data);
$("#state_id").val(null);
}
});
});
<?php if (!empty(set_value('country_id'))) : ?>
var value = "<?php echo set_value('country_id'); ?>";
$('#country_id').val(value);
$('#country_id').trigger('change');
<?php endif; ?>
<?php if (!empty(set_value('state_id'))) : ?>
var value = "<?php echo set_value('state_id'); ?>";
//without this alert the selection doesn't work
alert(value);
$('#state_id').val(value);
$('#state_id').trigger('change');
<?php endif; ?>
});
</script>
So I am getting the values saved only if I use an alert. This seems to be an asynchronous execution where the selection of second drop down is done before it gets populated, so the last thing to do is to figure out how to wait.
Still to do the attachment part
Thanks in advance!
Add this code at the end of view page
For country dropdown
<?php if (!empty(set_value('country_id'))) : ?>
<script type="text/javascript">
var value = "<?php echo set_value('country_id'); ?>";
$('select[name="country_id"]').find('option[value="'+value+'"]').attr("selected",true);
</script>
<?php endif; ?>
For state dropdown
<?php if (!empty(set_value('state_id'))) : ?>
<script type="text/javascript">
var value = "<?php echo set_value('state_id'); ?>";
$('select[name="state_id"]').find('option[value="'+value+'"]').attr("selected",true);
</script>
<?php endif; ?>
Follow the below points
1) For select tag, use set_select(), like this
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
2) If the form validation failed, call your ajax function to set second drop down.
3) For attachment, Whatever result of the form validation, first upload the image and keep the image path in session variable. After successful form validation, save the data into database and destroy the session variable.
edited >>>
**html**
<?php
if (isset($edit) && !empty($edit)) { //while editing
$StateID = $edit['StateID'];
$CountryID = $edit['CountryID'];
} else { // while adding
$StateID = set_value('StateID');
$CountryID = set_value('CountryID');
} ?>
<div class="col-3">
<div class="form-group">
<label class="mb-0">Country</label>
<select class="form-control Country" id="country" name="CountryID" data_state_value="<?php echo $StateID; ?>" >
<option value="">Select Country</option>
<?php
foreach ($country as $row) {
$selected = ($row->id == $CountryID) ? 'selected' : '';
echo '<option value="' . $row->id . '"' . $selected . '>' . $row->country_name . '</option>';
}
?>
</select>
<span class="text-danger font9"><?php echo form_error('CountryID'); ?></span>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label class="mb-0">State</label>
<select class="form-control State" id="State" name="StateID">
</select>
<span class="text-danger font9"><?php echo form_error('StateID'); ?></span>
</div>
</div>
**script**
$(document).on('change', '.Country', function () {
var country_id = $(this).val();
var state_id = $(this).attr('data_state_value');
url = $("body").attr('b_url');
$.ajax({
url: url + "Education/fetch_state",
method: "POST",
data: {
country_id: country_id,
state_id: state_id
},
success: function (res) {
var response = $.parseJSON(res);
$('#State').html('<option value="">Select State</option>' + response.view);
$('.State').trigger('change');
}
});
});
$('.Country').trigger('change');
**controller**
public function fetch_state() {
$data = array();
if (isset($_POST['country_id']) && !empty($_POST['country_id'])) {
$states = $this->Location_model->fetch_state($this->input->post('country_id')); // array of states
$view = array();
if (!empty($states)) {
foreach ($states as $val) {
$selected = (isset($_POST['state_id']) && !empty($_POST['state_id']) && $_POST['state_id'] == $val['id']) ? 'selected' : '';
$view[] = "<option value='" . $val['id'] . "'" . $selected . ">" . $val['states_name'] . "</option>";
}
$data['view'] = $view;
$data['status'] = 0;
} else {
$data['status'] = 0;
$data['view'] = '<option value="">No State Found</option>';
}
} else {
$data['status'] = 0;
$data['view'] = '<option value="">Select State</option>';
}
echo json_encode($data);
}

WordPress : How to change Ordering 'Ajax Load More' Plugin

'Ajax Load More' Plugin has installed on my website, i want to change Ordering
there is spacialfield in wp_posts Table On DB
But I can't how to set in on my posts
<option value="spacial" selected="selected">VIP (default)</option>
<option value="date">Date</option>
changed on :
plugins / ajax-load-more / admin / shortcode-builder / shortcode-builder.php
So it is :
<div class="inner half">
<label class="full"><?php _e('Order By', 'ajax-load-more'); ?>:</label>
<option value="spacial" selected="selected">VIP (default)</option>
<select class="alm_element" name="post-orderby" id="post-orderby">
<option value="date">Date</option>
<option value="title">Title</option>
<option value="name">Name (slug)</option>
<option value="menu_order">Menu Order</option>
<option value="author">Author</option>
<option value="ID">ID</option>
<option value="comment_count">Comment Count</option>
<option value="modified">Modified</option>
<option value="meta_value_num">meta_value_num</option>
</select>
</div>
But Nothing has changed and posts don't sort as i want
Shortcode Output on Admin Panel > Ajax Load More > Shortcode Builde :
[ajax_load_more post_type="post" orderby="spacial"]
Where should i put 'shortcode-builder' Code ?
------------------- Updata -------------------
You mean Admin Panel > Apearacne > Editor > main Page (Home page)
I can found :
echo do_shortcode('[ajax_load_more category__not_in=1 '.$query.' button_label="More Posts" orderby="title" order="DESC" posts_per_page="20" ]');
When i change orderby To spacial it doesn't works
Also I try :
<?php
$cat = get_category( get_query_var( 'cat' ) );
$category = $cat->slug;
if (!empty ($category))
$query .= ' category="'.$category.'" ';
if (isset ($_GET["province"]) && !empty($_GET["province"]))
$query .= ' meta_key="province" meta_value="'.$_GET["province"].'" ';
if (isset ($_GET["city"]) && !empty($_GET["city"]))
$query .= ' meta_key="city" meta_value="'.$_GET["city"].'" ';
$query .= ' ORDER BY SPACIAL DESC';
echo do_shortcode('[ajax_load_more category__not_in=1 '.$query.' button_label="More Posts" posts_per_page="20" ]');
?>
You can only order by custom field value not a field in the wp.posts table (I believe).
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
If spacial was a custom field.
[ajax_load_more post_type="post" orderby="meta_value" meta_key="spacial"]

Get subject heading in Prestashop contact form email template

I have a drop downselect for several subjects in my Prestashop contact form.
<select id="id_contact" name="id_contact" class="form-control" onchange="showElemFromSelect('id_contact', 'desc_contact')">
<option value="0">-- Please chose --</option>
<option value="1">Cancel order</option>
<option value="3">Check order</option>
<option value="2">Return a product</option>
</select>
Is there any possibility to create or to have the value of the subject chosen in the confirmation email received?:
If you added this subjects as contacts names in backoffice, then in controllers/front/ContactController method postProcess() find block:
$var_list = array(
'{order_name}' => '-',
'{attached_file}' => '-',
'{message}' => Tools::nl2br(stripslashes($message)),
'{email}' => $from,
'{product_name}' => '',
);
and add $contact->name e.g. to {message}
'{message}' => '<br />Subject: '. $contact->name . '<br />' .Tools::nl2br(stripslashes($message)),

Codeigniter: Interdependent dropdown lists. Completed code

I have a series of interdependent select lists which work correctly outside CI. When I tried to implement it in Codeigniter, I am not coming on as the first selectlist is not populated, either the echoing is not properly code or I dont know what. This question here will refer to just the first select list, that is, you will not see any jquery, because the first gets populated directly from the database without any "function change".
so here are the modules:
VIEW
<?php echo form_open('control_form/add_all'); ?>
<label for="f_state">State<span class="red">*</span></label>
<select id="f_state" name="f_state">
<option value=""></option>
<?php
foreach($result as $row)
{
echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>';
}
?>
</select>
<label for="f_city">City<span class="red">*</span></label>
<!--this will be filled based on the tree selection above-->
<select id="f_city" name="f_city" id="f_city_label">
<option value=""></option>
</select>
<label for="f_membername">Member Name<span class="red">*</span></label>
<input type="text" name="f_membername"/>
<?php echo form_close(); ?>
CONTROL
public function add_all()
{
#Validate entry form information
$this->load->model('model_form','', TRUE);
$this->form_validation->set_rules('f_state', 'State', 'required');
$this->form_validation->set_rules('f_city', 'City', 'required');
$this->form_validation->set_rules('f_membername', 'Member Name', 'required');
$data['city'] = $this->model_form->get_state(); //gets the available groups for the dropdown
if ($this->form_validation->run() == FALSE)
{
$this->load->view('view_form_all', $data); # parece ser que vuelve a meter los mismos datos que tenia la Form
}
else
{
#Add Member to Database
$this->model_form->add_all();
$this->load->view('view_form_success');
}
}
MODEL
<?php
class Model_form extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_state()
{
$query = $this->db->query('SELECT pais_id, pais_name FROM pais');
return $query->result();
}
function add_all()
{
$v_state = $this->input->post('f_state');
$v_membername = $this->input->post('f_membername');
$data = array(
'pais_id' => NULL,
'pais_name' => $v_state
);
$this->db->insert('members', $data);
}
}
You are not sending $result to the view, you need something like:
$data['result'] = ...//get some data
$this->load->view('view_form_all',$data);
If the list you want to display is the cities list, then you need to change in your view:
foreach($city as $row)
{
echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>';
}
since in your controller you are doing:
$data['city'] = $this->model_form->get_state();

DOB select boxes on Magento registration form

I'm currently using Magento 1.5.0.1
When a customer reigisters on the registration page (/account/create), he has to insert a date of birth (dob).
By default, the date of birth consists of 3 text-boxes. I have modified the file at /app/design/frontend/base/default/template/customer/widget.
So, instead of text-boxes there are now 3 select-boxes where the user can select a date
However, when all information is filled in correctly and you click on the "submit" button nothing happens (no errors, messages or anything).
Any idea on how to make the "submit" button work again?
Thanks
Edit:
The id's of the are the same as the one's used by the "". I've posted the code that I have changed
I've made 2 functions that print the options's element:
function getOptions($minValue, $maxValue)
{
$options = "";
for ($count = $minValue; $count <= $maxValue; $count++)
{
$options = $options . '<option value="' . $count . '">' . $count . '</option>';
}
return $options;
}
function getYears()
{
$years = "";
$currentYear = date('Y');
for ($count = 1900; $count <= $currentYear; $count++)
{
$years = $years . '<option value="' . $count;
if ($count == '1980')
{
$years = $years . '" selected="selected"';
}
$years = $years . '">' . $count . '</option>';
}
return $years;
}
And i've changed the input type=text to select
$this->setDateInput('d',
'<div class="dob-day">
<label for="' . $this->getFieldId('day') . '">' . $this->__('DD') . '</label>
<select id="' . $this->getFieldId('day') . '" name="' . $this->getFieldName('day') . '" title="' . $this->__('Day') . '" class="validate-select" ' . $this->getFieldParams() . '>' . getOptions(1,31) . '
</select>
</div>
<br />');
$this->setDateInput('m',
'<div class="dob-month">
<label for="' . $this->getFieldId('month') . '">' . $this->__('MM') . '</label>
<select id="' . $this->getFieldId('month') . '" name="' . $this->getFieldName('month') . '" title="' . $this->__('Month') . '" class="validate-select"' . $this->getFieldParams() . '>' . getOptions(1,12) . '
</select>
</div>
<br />');
$this->setDateInput('y',
'<div class="dob-year">
<label for="' . $this->getFieldId('year') . '">' . $this->__('YYYY') . '</label>
<select id="' . $this->getFieldId('year') . '" name="' . $this->getFieldName('year') . '" title="' . $this->__('Year') . '" class="validate-select"' . $this->getFieldParams() . '>' . getYears() . '
</div>'
);
Replace/create dob.phtml file located in app/design/frontend/[package-name]/default/template/customer/widget/
https://gist.github.com/ncla/7794502 (tested on Magento 1.8 community edition)
Accepted answer might work, but hiding inputs seems unacceptable. Also setDateInput, getSortedDateInputs methods are unnecessary, as you pass them HTML just to get them back.
If you try converting dob.phtml from input boxes to drop down boxes, Magento form validation will fail (throw JS error) because it can't find "day" input. So I just override Varien.DOB initialize function to select correct elements.
Plz replace the dob.phtml in
app/design/frontend/base/default/template/customer/widget/
to follwing code and override new field to old with the help of css
/////////////////////////////
<?php echo $this->getLayout()->createBlock('customer/widget_dob')
->setDate($this->getCustomer()->getDob())
->toHtml() ?>
// For checkout/onepage/billing.phtml:
<?php echo $this->getLayout()->createBlock('customer/widget_dob')
->setDate($this->getCustomer()->getDob())
->setFieldIdFormat('billing:%s')
->setFieldNameFormat('billing[%s]')
->toHtml() ?>
/* NOTE: Regarding styles - if we leave it this way, we'll move it to boxes.css
Alternatively we could calculate widths automatically using block input parameters.
*/
/**
* #see Mage_Customer_Block_Widget_Dob
*/
<label for="<?php echo $this->getFieldId('month')?>"<?php if ($this->isRequired()) echo ' class="required"' ?>><?php if ($this->isRequired()) echo '<em>*</em>' ?><?php echo $this->__('Date of Birth') ?></label>
<div class="input-box customer-dob">
<?php
$this->setDateInput('d',
'<div class="dob-day">
<input type="text" id="' . $this->getFieldId('day') . '" name="' . $this->getFieldName('day') . '" value="' . $this->getDay() . '" title="' . $this->__('Day') . '" class="input-text validate-custom" ' . $this->getFieldParams() . ' />
<label for="' . $this->getFieldId('day') . '">' . $this->__('DD') . '</label>
</div>'
);?>
<select name="nday" id="nday" onchange="getElementById('<?php echo $this->getFieldId('day');?>').value=this.value" style="width:90px;">
<option value="">Select</option>
<?php
for($i=1;$i<=31;$i++)
{
echo "<option value='".$i."'>".$i."</option>";
}
?>
</select>
<select name="nmon" id="nmon" onchange="getElementById('<?php echo $this->getFieldId('month');?>').value=this.value" style="width:90px;">
<option value=''>Select</option>
<?php echo '<option value="">'.$this->__("Select").'</option>'.'<option value="01">'.$this->__("January").'</option>'.'<option value="02">'.$this->__("February").'</option>'.'<option value="03">'.$this->__("March").'</option>'.'<option value="04">'.$this->__("April").'</option>'.'<option value="05">'.$this->__("May").'</option>'.'<option value="06">'.$this->__("June").'</option>'.'<option value="07">'.$this->__("July").'</option>'.'<option value="08">'.$this->__("August").'</option>'.'<option value="09">'.$this->__("September").'</option>'.'<option value="10">'.$this->__("October").'</option>'.'<option value="11">'.$this->__("November").'</option>'.'<option value="12">'.$this->__("December").'</option>'; ?>
</select>
<select name="nyear" id="nyear" onchange="getElementById('<?php echo $this->getFieldId('year');?>').value=this.value" style="width:90px;">
<option value="">Select</option>
<?php
for($j=date('Y')-80;$j<=date('Y');$j++)
{
echo "<option value='".$j."'>".$j."</option>";
}
?>
</select>
</p>
<?php
$this->setDateInput('m',
'<div class="dob-month">
<input type="text" id="' . $this->getFieldId('month') . '" name="' . $this->getFieldName('month') . '" value="' . $this->getMonth() . '" title="' . $this->__('Month') . '" class="input-text validate-custom" ' . $this->getFieldParams() . '/>
<label for="' . $this->getFieldId('month') . '">' . $this->__('MM') . '</label>
</div>'
);
$this->setDateInput('y',
'<div class="dob-year">
<input type="text" id="' . $this->getFieldId('year') . '" name="' . $this->getFieldName('year') . '" value="' . $this->getYear() . '" title="' . $this->__('Year') . '" class="input-text validate-custom" ' . $this->getFieldParams() . ' />
<label for="' . $this->getFieldId('year') . '">' . $this->__('YYYY') . '</label>
</div>'
);
?>
<?php echo $this->getSortedDateInputs() ?>
<div class="dob-full" style="display:none;">
<input type="hidden" id="<?php echo $this->getFieldId('dob')?>" name="<?php echo $this->getFieldName('dob')?>" />
</div>
<div class="validation-advice" style="display:none;"></div>
</div>
<script type="text/javascript">
//<![CDATA[
var customer_dob = new Varien.DOB('.customer-dob', <?php echo $this->isRequired() ? 'true' : 'false' ?>, '<?php echo $this->getDateFormat() ?>');
//]]>
</script>
////////////////////////////
it work fine .this is just an idea.......
enjoy
Thats how I did it. Maybe someone can use it
Grab the dob code from some site or from Magento default theme
Create your own widget template in your theme folder, for example customer/widget/dob-custom.phtml
Call this widget in your template file, for example
echo $this->getLayout()->createBlock('customer/widget_dob')->setTemplate('customer/widget/dob-custom.phtml')->toHtml();
The issue I think is because the format of the DD is not correct. You are generating a day like 1, 2, 3, 4... while it should be 01, 02, 03, 04,.. etc..
Regards!
I recently switched from ncla's solution to this:
http://www.endreywalder.com/blog/change-date-of-birth-field-to-select-boxes-in-magento/
Instead of displaying 01-12 for months, this will output the actual names which I like much better.
Tested on Magento 1.9.2

Resources