automatically add selected to dropdown menu using GET variable - forms

I am using a captcha in a form. When the user submits an invalid answer to the captcha, the page reloads repopulating the fields with the user data. My question is, is there a better (or easier) way to check which dropdown value was selected.
This is what I have now that works:
php
<?php
$state = $sf_params->get('state');
?>
html
<span>State </span>
<select size="1" name="state">
<option value="" selected="selected"></option>
<option value="AL" <?php if ($state == "AL") { echo "selected = 'selected'"; } ?>>Alabama</option>
<option value="AK" <?php if ($state == "AK") { echo "selected = 'selected'"; } ?>>Alaska</option>
<option value="AZ" <?php if ($state == "AZ") { echo "selected = 'selected'"; } ?>>Arizona</option>
<option value="AR" <?php if ($state == "AR") { echo "selected = 'selected'"; } ?>>Arkansas</option>
Is there another solution to getting the GET variable, then parsing the options, looking for the matched value?

Well, it's not really a good practice to build a form on your own. Usually you should use widget from sfForm class, etc ..
But, if you really want to build this kind of stuff, you should bring back a good helper from sf1.0 that helps you building form tag. In your case, the function options_for_select and select_tag might be useful:
options_for_select:
Returns a formatted set of <option> tags based on optional $options array variable.
select_tag:
Returns a <select> tag, optionally comprised of <option> tags.
options_for_select took as second parameter, the selected value you want.
So just, create a file called lib/helper/FormHelper.php and put these two functions inside. Don't forget to load the helper in your view :
<?php use_helper('Form') ?>
And use options_for_select & select_tag to build your select.

First move the values of the select to an array in the Controller like
$states = array('AL' => 'Alabama', ... );
Then you can use the Helper j0k mentioned or go by feet:
By assigning the vars in the controller.
$this->states = $states;
In the Template:
<?php foreach ($states as $key => $value): ?>
<option value="<?php echo $key ?>" <?php if ($state == $value) { echo "selected = 'selected'"; } ?>><?php echo $value ?></option>
<?php endforeach; ?>
But I also recommend to use the Helper.

Related

SemanticUI Multiple select dont post multiple values

Im using Semantic UI for design and some functionalities and I failed to make multiple value posting in dropdown select - it adds blocks into the select input, but when submitting, posts only numeric string - one of the input values (instead of array). Seems to post the highest number from selection.
Can anyone tell me, how to make it working? (loading previous values works perfect);
<select class="ui dropdown" id="subjs" name="subj" multiple>
<?php
$selected = $cats->getsubj();
$subs = $cats->getsubj(true);
if (isset($subs) && count($subs) > 0)
for ($i = 0; $i < count($subs); $i++) {
$opt = (in_array($subs[$i], $selected)) ? ' selected="selected"' : '';
?>
<option class="subjs" value="<?php echo $subs[$i]['ID']; ?>" <?php echo $opt; ?>><?php echo $subs[$i]['title']; ?></option>
<?php
}
?>
</select>
JS:
<script>
$('.ui.dropdown').dropdown({
maxSelections: 2
});
</script>
PHP:
var_dump($_POST);
the script is listing of categories from database into select as options. Value for each option is it's ID (integer). One article may be liked with more than one category
All advices apreciated... Thanx
Well for all, who make stupid mistakes, simple add '[]' to the multiple select name parameter to post array.
<select name="x[]"></select>

CAKEPHP Confirming on Submit

I have a submit button in my form like this :
<?php echo $this->Form->end(__('Submit')); ?>
and I want to embed variables that are part of the input form.
For example, I want to embed a variable that is input here $this->Form->input('user_id');.
Does anyone know how to do this? I currently have this onsubmit as part of the options array, but it throws errors when I try to embed variables.
<?php echo $this->Form->create('Shift', array(
'onsubmit' => 'return confirm("Creating shift for '$id' on ");'
)); ?>
Now making an edit to this question. So the Javascript solution worked fine. Issue now is that I want the alert box to display the user's name, which is what is displayed in the dropbox. However, the HTML tags contain the user's ids. Here is the HTML
<div class="input select required">
<label for="my_user_id">User</label>
<select name="data[Shift][user_id]" id="my_user_id" required="required">
<option value="1">john doe</option>
<option value="2">john johnson</option>
Is there a way to access the value between the <option> tags using Javascript?
You need to use double quotes:
<?php
echo $this->Form->create('Shift', array(
'onsubmit' => "return confirm(\"Creating shift for '$id' on \");"
));
?>
Another option is to use $this->Form->postLink() and set $confirmMessage parameter if it is a simple state shift form.
You can use JQuery
echo $this->Form->create('Shift', array('id' => 'myform'));
echo $this->Form->input('user_id', array('id' => 'my_user_id', 'type'=>'text'));
After the form:
<?php echo $this->Html->scriptBlock('
$(document).ready(function() {
$("#myform").submit(function(event) {
alert("Creating shift for " + $("#my_user_id").val());
});
});
');
?>

PHP Populate Select box and set based on an existing value

I couldn't find a solution on here, thought I wasn't sure what to search for. I have a select box that is populated from my database using PDO. However I was unsure on how to set the selected value to a team that has already been set from the DB.
Update The code only shows the team that matches the variable, however I need to show all teams in the drop down.
<option <?php if($row4['team_id'] == $team_id) { ?>
selected="selected" > <?php }
echo $row4['team_name'];?>
</option>
<?php } ?>
OLD code
$stmt4 = $conn ->prepare('SELECT team_id, team_name FROM team_tbl ORDER BY team_name');
$stmt4->execute();
while($row4 = $stmt4->fetch(PDO::FETCH_ASSOC)) {
echo '<option <? if ($row4['team_id'] == $team_id){?> selected="selected"<? } ?>>'.$row4['team_name'].'</option>';
}
?>
<option <?php if($row4['team_id'] == $team_id) { ?> selected="selected" <?php } ?>></option>
Add the above code to the option field.

Magento - Disable customer of Changing / Reset email

For security reasons I need to disable the possibility in form the user to change his email. Once set it can't be change. How can I do this?
I have found :
http://www.magentocommerce.com/boards/viewthread/8622/
Maybe usefull for someone ...
Make the following changes:
In template/customer/account/dashboard/info.phtml
<div class="inner-head">
<h5><?php echo $this->__('Contact Information') ?></h5>
Edit
</div>
<p>
<?php echo $this->htmlEscape($this->getCustomer()->getFirstname()) ?>
<?php echo $this->htmlEscape($this->getCustomer()->getLastname()) ?><br />
<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?><br />
<?php echo $this->__('Change Password') ?>
</p>
and Replace with:
<div class="inner-head">
<h5><?php echo $this->__('Contact Information') ?></h5><br />
</div>
<p>
<?php echo $this->htmlEscape($this->getCustomer()->getFirstname()) ?>
<?php echo $this->htmlEscape($this->getCustomer()->getLastname()) ?><br />
<?php echo $this->__('Change Password') ?>
</p>
Extracted and adapted from the magento community forums.
The problem was that we had an extension that override the user model/controller/view, example: social login, and because we decided not to use it we Disable it in the advanced settings.
Because it was disable, the list of users was not showing in the back-end, but I could create a new customer, yet in the New customer view the Addresses were not showing either.
So, we activated all the extensions again and suddenly the customers list appears. So like this we found out which extension was breaking and fixed it.
In file :
app\code\core\Mage\Customer\controllers\AccountController.php
/**
* Change customer password action
*/
public function editPostAction()
{
if (!$this->_validateFormKey()) {
return $this->_redirect('*/*/edit');
}
if ($this->getRequest()->isPost()) {
$customer = Mage::getModel('customer/customer')
->setId($this->_getSession()->getCustomerId())
->setWebsiteId($this->_getSession()->getCustomer()->getWebsiteId());
$fields = Mage::getConfig()->getFieldset('customer_account');
$data = $this->_filterPostData($this->getRequest()->getPost());
//=========
// ADD THAT // customer cannot change his email // Le customer ne peut pas modifier son email
if(isset($data['email'])){
$data['email'] = $this->_getSession()->getCustomer()->getData('email');
}
//========END
foreach ($fields as $code=>$node) {
if ($node->is('update') && isset($data[$code])) {
$customer->setData($code, $data[$code]);
}
}
$errors = $customer->validate();
if (!is_array($errors)) {
$errors = array();
}

"Available Product Listing Sort By" will not remove options from drop-down

I want to remove/hide some attribute sort options in my categories.
For this I unchecked "use all attributes" and selected the attributes I want to display in the sort select.
After this I cleared cache and reindexed categories and products data.
But I still have all attributes showing in the sort by select. Can somone help me please?
I'm using a modified toolbar.phtml to hide the "position" sort option, but I think this has nothing to do with the problem:
<div class="sort-by">
<label><?php echo $this->__('Sort By') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<?php if ($_order != $this->__('Position')) : // Remove "Position" from the sort option list ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<?php if($this->getCurrentDirection() == 'desc'): ?>
<img src="<?php echo $this->getSkinUrl('images/i_desc_arrow.gif') ?>" alt="<?php echo $this->__('Set Ascending Direction') ?>" class="v-middle" />
<?php else: ?>
<img src="<?php echo $this->getSkinUrl('images/i_asc_arrow.gif') ?>" alt="<?php echo $this->__('Set Descending Direction') ?>" class="v-middle" />
<?php endif; ?>
</div>
EDIT:
Here's an image from my display settings inside the category:
But inside my storeview all options are displayed
The problem for me was exactly what the error was saying, it just was hard to understand. You can't pick a default sort option that isn't enabled
Okay, fixed the problem:
I changed the settings in all store views, but it didn't changed in all sotre views. When I tried to change the setting Use Default Value in my "languagesviews". I get the error "Default Product Listing Sort by not exists on Available Product Listing Sort By".
So I "just" need to change all categorysettings of each language.
EDIT:
After editing some categories and checking/unchecking "use default values" the error is gone and I set all "use default values" to yes.