Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在CodeIgniter中使用PHP在条件下显示表单字段?_Php_Html_Css_Codeigniter - Fatal编程技术网

如何在CodeIgniter中使用PHP在条件下显示表单字段?

如何在CodeIgniter中使用PHP在条件下显示表单字段?,php,html,css,codeigniter,Php,Html,Css,Codeigniter,我在表格中为制造商选择了输入 <div class="form-group"> <label for="manufacturer">Manufacturer</label> <select id="manufacturerSelect" name="manufacturer" class="form-control"> <option disabled selected value> -- select

我在表格中为制造商选择了输入

<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>
和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>
现在,如果用户选择其他作为制造商,则显示添加输入。如果制造商==24,则在服务器端添加otherManufacturer的表单验证规则。问题在于,每次用户从服务器获得响应时,都会显示其他制造商的输入。我可以在默认情况下将class=hidden添加到其他制造商div中,但如果表单验证未运行,则其他制造商字段将不会再次显示给用户

我需要的是PHP,如果条件在里面:

类别=表格组>

因此,仅当制造商不是其他制造商时,才会添加class=hidden。但我想不出正确的情况

任何帮助都将不胜感激

编辑

控制器:

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*/
    }
}

在您的特定情况下,这将解决问题:

<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>
然后可以删除JS代码段。附加表单将在服务器端隐藏class=将设置为隐藏

我看到您在同一个模板中使用var$manufacturer。我看不到您的控制器以及如何传递变量,但您也可以使用$\u GET['manufacturer']或$\u POST['manufacturer']代替$manufacturer,具体取决于您的表单操作方法。
注意:$\u GET['manufacturer']、$\u POST['manufacturer']和$\u REQUEST['manufacturer']不是经过消毒的输入。使用$manufacturer时,我假设它已在控制器中清理。

消息:语法错误,意外隐藏的T_常量\u封装的_字符串,应为“,”或“;”对不起,我已经编辑了我的答案。现在可以了。我使用的是if语句的速记语法。如果你的PHP版本不允许,你可以用:我想你的意思是。然而,无论我使用哪种方法,我都会得到以下错误:Message:Undefined variable:manufacturer我认为如果我弄明白了,isset将在清空之前被移动!没有isset部分,因为我使用-选择制造商-所以在第一次显示表单时,无法设置制造商,因此条件不会运行。此外,我还必须添加JS代码段,因为如果用户选择其他,他需要被提示立即进入其他电子邮箱,而不是在提交表单之后——这将是糟糕的用户体验。
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*/
    }
}
<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>