Search code examples
codeignitervalidationselectradio-button

codeigniter input fields with same name


sorry if its a stupid question but i need a bit of a help.

I made a signup form, and i would like to people select 2 phone numbers.

If i select hungarian a hungarian phone type input slides down, if he selects ukraine than an ukraine phone input type slides down.

here is the html

    <input type='text' name='telefon' id='magyar' class='input_title' value='<?php echo set_value('telefon'); ?>' />

    <input type='text' name='telefon' id='ukran' class='input_title' value='<?php echo set_value('telefon'); ?>' />

    <div class='hiba'><?php echo form_error('telefon'); ?></div>    

magyar = hingarian 
ukran = ukraine
telefon = phone
telo_tipus = phoe type

my problem is the validation, if i select hungarian and fill it out it says i need to add a phone number but if i select ukraine its ok

here is the validation

$this->form_validation->set_rules('telefon', 'Telefon', 'required|callback_hasznalt_telefon');

could please someone point out what im missing with the validation?

i tried with na=telefon[] but in that case the validation wont work

the callback only validates if the phone is taken thats ok but here it is

function hasznalt_telefon()
    {
        $telefon = trim($this->input->post('telefon'));

        $query  = $this->db->query(' SELECT telefon FROM felhasznalok WHERE telefon = "'.$telefon.'" ');

        if($query->num_rows() > 0) 
        {
            $this->form_validation->set_message('hasznalt_telefon', 'Ez a telefonszám  már használatban van '); 
            return false;
        } 
        else 
        {
            return true;    
        }

    }

Solution

  • Your codeigniter code is fine, but your html/javascript is what needs to change.

    Instead of having two input fields with the same name (which the php script will only read the last one, btw), you should make a select field that changes what input type slides down from the 'telefon' field.

    I'm not sure what javascript you are using, but in jquery you can have the the input field event bound on selection from the select field.

    If you need more specific guidance for this, let me know and I'll edit my answer.

    <select id="telefon_type">
        <option>Telefon Type</option>
        <option value="magyar">Magyar</option>
        <option value="ukran">Ukran</option>
    </select>
    
    <input type="text" name="telefon" id="telefon" disabled />
    
    $("#telefon_type").bind("change", function() {
        $("#telefon").removeAttr("disabled");
    
        if ($(this).val() == "magyar") {
            $("#telefon").bind("focus",function() {
                // onfocus event for #telefon for magyar
            });
        } else if ($(this).val() == "ukran") {
            $("#telefon").bind("focus",function() {
                // onfocus event for #telefon for ukran
            });
        } else {
            $("#telefon").attr("disabled","disabled");
            $("#telefon").val('');
        }
    
    });
    

    Please note: I have not tested this code. The general idea behind it is that the filter you are running for the telefon field changes based on your selection of the select field.