0

I have an Placeholder Value input box that is set to display:none; on page load due to it being controlled jQuery and shows when the Placeholder dropdown box is selected to yes.

My issue is that I have used the Codeigniter Validation for the form and if I have an error on the form the page reloads the Placeholder dropdown box states yes but the Placeholder Value input box disappears.

Would my best bet be to create a callback_ function that basically states if yes is selected show the input box?

jQuery:

              $("#add_fields_placeholder").change(function()
    {
        if($(this).val() == "yes")
    {
        $('label[for="add_fields_placeholderValue"]').show();
        $("#add_fields_placeholderValue").show();
    }
    else
    {

        $('label[for="add_fields_placeholderValue"]').hide();
        $("#add_fields_placeholderValue").hide();
    }
        });

View:

<label for="add_fields_placeholder">Placeholder: </label>
<select name="add_fields_placeholder" id="add_fields_placeholder">
    <option value="">Please Select</option>
    <option value="yes" <?php echo set_select('add_fields_placeholder','yes', ( !empty($placeholderType) && $placeholderType == "yes" ? TRUE : FALSE ));?>>Yes</option>
    <option value="no" <?php echo set_select('add_fields_placeholder','no', ( !empty($placeholderType) && $placeholderType == "no" ? TRUE : FALSE ));?>>No</option>
</select>

<label for="add_fields_placeholderValue">Placeholder Text: </label>
<input type="text" name="add_fields_placeholderValue" id="add_fields_placeholderValue" value="<?php echo set_value('add_fields_placeholderValue'); ?>">

Controller Validation:

$this->form_validation->set_rules('add_fields_placeholder', 'Placeholder', 'trim|required|xss_clean|callback_dropdown_check');

$this->form_validation->set_rules('add_fields_placeholderValue', 'Placeholder Text','trim|xss_clean');
4
  • I guess you need to check the actual state of the dropdown so every time your page loads you'll check the state and make the necessary changes to the other field. I think that this question is not PHP related since there is no PHP errors so if I'm not mistaken could you please change the tags? =) Commented Aug 15, 2012 at 0:20
  • You used javascript to display the hidden field? If so, you just need to call this also in the load of the page ;) Commented Aug 15, 2012 at 1:21
  • @SérgioMichels Could you provide me an example it seems I could not get one stackoverflow.com/questions/11961248/… I was thinking this was the case Commented Aug 15, 2012 at 1:59
  • @JessMcKenzie please, post your code that shows the hidden field. Commented Aug 15, 2012 at 2:10

2 Answers 2

1

When the page loads, you will need to trigger the change of the add_fields_placeholder. Just make sure that the yes option is selected. The code will be something like:

function initChangeEvent() {
  $("#add_fields_placeholder").change(function() {
    if($(this).val() == "yes") {
        $('label[for="add_fields_placeholderValue"]').show();
        $("#add_fields_placeholderValue").show();
    } else {
        $('label[for="add_fields_placeholderValue"]').hide();
        $("#add_fields_placeholderValue").hide();
    }
  });
}

$(function(){
  initChangeEvent();
  $("#add_fields_placeholder").trigger('change');  
})
Sign up to request clarification or add additional context in comments.

2 Comments

It does not seem to load the input box when yes is selected no error
Still not showing the input for you? I just tried the link and it works.
0

You can also place this code in document.ready function. It will work

if( $("#add_fields_placeholder").val() == "yes")
{
    $('label[for="add_fields_placeholderValue"]').show();
    $("#add_fields_placeholderValue").show();
}
else
{

    $('label[for="add_fields_placeholderValue"]').hide();
    $("#add_fields_placeholderValue").hide();
}
    });

1 Comment

It does not seem to load the input box when yes is selected no error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.