I have select input in my form for manufacturers.
我已经为我的表格选择了制造商的输入。
<div class="form-group">
<label for="manufacturer">Manufacturer</label>
<select id="manufacturerSelect" name="manufacturer" class="form-control">
<option disabled selected value> -- select an manufacturer -- </option>
<?php foreach ($manufacturers as $manufacturers_item): ?>
<option value="<?=$manufacturers_item['id'];?>" <?php echo set_select('manufacturer',$manufacturers_item['id'], ( !empty($manufacturer) && $manufacturer == $manufacturers_item['id'] ? TRUE : FALSE )); ?> ><?=$manufacturers_item['name'];?></option>
<?php endforeach; ?>
<option disabled>──────────</option>
<option value="24" <?php echo set_select('manufacturer','24', ( !empty($manufacturer) && $manufacturer == '24' ? TRUE : FALSE )); ?> >Other</option>
</select>
<?php echo form_error('manufacturer'); ?><br />
</div>
If "other" (value == 24) is checked additional input is asked:
如果选中“其他”(值== 24),则会询问其他输入:
$('body').on('change', '#manufacturerSelect', function() {
if ($(this).val() == 24) {
$("#otherManufacturerSelect").removeClass('hidden');
} else {
$("#otherManufacturerSelect").addClass('hidden')
}
});
And HTML:
和HTML:
<div id="otherManufacturerSelect" class="form-group">
<label for="otherManufacturer" >What is it then?</label>
<input type="text" name="otherManufacturer" class="form-control">
<?php echo form_error('otherManufacturer'); ?><br />
</div>
CSS:
CSS:
.hidden {
display: hidden;
}
Now if user picks "other" as manufacturer addition input is displayed. Form validation rule for otherManufacturer is added in server side if manufacturer == 24
. The problem is that the other manufacturer input is displayed every time user get response from server. I could add class="hidden"
by default to other manufacturer div but if the form validation doesnt run other manufacturer field will not be displayed again to user.
现在,如果用户选择“其他”作为制造商,则显示添加输入。如果制造商== 24,则在服务器端添加其他制造商的表单验证规则。问题是每次用户从服务器获得响应时都会显示其他制造商输入。我可以默认将class =“hidden”添加到其他制造商div,但如果表单验证没有运行,则其他制造商字段将不再显示给用户。
What I need is PHP IF condition inside:
我需要的是PHP IF条件里面:
<div id="otherManufacturerSelect" <?php if(/*???*/):?>class="hidden"<?php endif; ?> class="form-group">
So that class="hidden"
would be added only if manufacturer is not "other". but I cannt think of rigth condition.
因此,只有当制造商不是“其他”时,才会添加class =“hidden”。但我无法想到严峻的条件。
Any help would be appreciated!
任何帮助,将不胜感激!
EDIT
编辑
Controller:
控制器:
public function create()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('manufacturer', 'Manufacturer', 'required');
if($this->input->post('manufacturer') == '24'){
$this->form_validation->set_rules('otherManufacturer', 'Other manufacturer', 'required');
}
$data['manufacturers'] = $this->puzzles_model->getManufacturers();
if ($this->form_validation->run() === FALSE)
{
$this->load->view('puzzles/create', $data);
}
else
{
/* do the upload, return upload errors or save in db*/
}
}
1 个解决方案
#1
1
In your particular case this would fix the problem:
在您的特定情况下,这将解决问题:
<div id="otherManufacturerSelect" class="form-group <?php if(isset($manufacturer) && $manufacturer !== '24') { echo 'hidden'; } ?> ">
<label for="otherManufacturer" >What is it then?</label>
<input type="text" name="otherManufacturer" class="form-control">
<?php echo form_error('otherManufacturer'); ?><br />
</div>
Then you can remove the JS snippet. The additional form will be hidden on server side (class="hidden" will be set).
然后你可以删除JS片段。附加表单将隐藏在服务器端(将设置class =“hidden”)。
I saw that you're using var $manufacturer in the same template. I can't see your controller and how you're passing variables but instead of $manufacturer you can also use $_GET['manufacturer'] or $_POST['manufacturer'] (depending on your form action method). Notice: $_GET['manufacturer'], $_POST['manufacturer'] and $_REQUEST['manufacturer'] is NOT sanitized input. When using $manufacturer I assume that it's sanitized in your controller.
我看到你在同一个模板中使用var $ manufacturer。我看不到你的控制器以及你如何传递变量但是你也可以使用$ _GET ['manufacturer']或$ _POST ['manufacturer'](取决于你的表单操作方法)而不是$ manufacturer。注意:$ _GET ['manufacturer'],$ _POST ['manufacturer']和$ _REQUEST ['manufacturer']未经过清理输入。使用$ manufacturer时,我认为它已在您的控制器中进行了清理。
#1
1
In your particular case this would fix the problem:
在您的特定情况下,这将解决问题:
<div id="otherManufacturerSelect" class="form-group <?php if(isset($manufacturer) && $manufacturer !== '24') { echo 'hidden'; } ?> ">
<label for="otherManufacturer" >What is it then?</label>
<input type="text" name="otherManufacturer" class="form-control">
<?php echo form_error('otherManufacturer'); ?><br />
</div>
Then you can remove the JS snippet. The additional form will be hidden on server side (class="hidden" will be set).
然后你可以删除JS片段。附加表单将隐藏在服务器端(将设置class =“hidden”)。
I saw that you're using var $manufacturer in the same template. I can't see your controller and how you're passing variables but instead of $manufacturer you can also use $_GET['manufacturer'] or $_POST['manufacturer'] (depending on your form action method). Notice: $_GET['manufacturer'], $_POST['manufacturer'] and $_REQUEST['manufacturer'] is NOT sanitized input. When using $manufacturer I assume that it's sanitized in your controller.
我看到你在同一个模板中使用var $ manufacturer。我看不到你的控制器以及你如何传递变量但是你也可以使用$ _GET ['manufacturer']或$ _POST ['manufacturer'](取决于你的表单操作方法)而不是$ manufacturer。注意:$ _GET ['manufacturer'],$ _POST ['manufacturer']和$ _REQUEST ['manufacturer']未经过清理输入。使用$ manufacturer时,我认为它已在您的控制器中进行了清理。