5

I am working on a website, and I have run into a problem. In order to pre-set or re-input slected values, Codeigniter allows you to use set_radio to solve your problem, seen here:

<td>Do You Have a PayPal Account <span class="error">*</span> : </td>
<td colspan="2" align="left" valign="top"><label>   
  <!-- used for validation -->
    <input type="hidden" id="temp_mid" value="" />
    <input name="mail_account" type="radio" id="p_same" value="same" onclick="dohide_show(this.value);" <?php echo set_radio('mail_account', 'same'); ?>/> Yes(same as email address as above.)<br/>
    <input name="mail_account" type="radio" id="p_diff" value="diff" onclick="dohide_show(this.value);" <?php echo set_radio('mail_account', 'diff'); ?>/> Yes(different than above.)<br/>
    <input name="mail_account" type="radio" id="p_checkInMail" value="checkInMail" onclick="dohide_show(this.value);" <?php echo set_radio('mail_account', 'checkInMail'); ?>/> No(I would prefer to receive a check in the mail.)                  
     <?php echo form_error('mail_account'); ?>           

 </td>

This works perfectly fine after validation, as the previous radio button has been selected if there is an error in another part of the form. BUT, when trying to resubmit the page after the user has corrected the error, valditation fails on the radio selection, asking me to click on the radio buttons again, even if the radio button is visually selected on the option I chose before. So I have to choose the radio button again before I can resubmit the form. The alert called is seen here:

function validate_playpal()
        {   
            var temp=$('#temp_mid').val();
            if(temp=="")
            {
                alert('Please select the PayPal account option.');                  
                $('#p_same').focus();
                return false;
            }
            if(document.getElementById('p_diff').checked== true && document.getElementById('paypal_emailid').value=='')
            {
                alert('Please provide your PayPal email address.');
                document.getElementById('paypal_emailid').focus();
                return false;
            }

            if(document.getElementById('paypal_emailid').value!='')
            {
                if(!email_format(document.getElementById('paypal_emailid').value)){
                    alert('Please enter valid email address.');
                    document.getElementById('paypal_emailid').value="";
                    document.getElementById('paypal_emailid').focus();
                    return false;               
                }
            }
            return true;
        }

Does anyone know why this isn't working, even though it is repopulating the form correctly? Thanks.

EDIT:

For everyone who asked for what Mid_temp does, it appears to me that it just hides the buttons if they do not need to be selected. In the code I have, these are the only places it is mentioned:

    $('#bx').hide();
if(document.getElementById('p_diff').checked==true){    
    $('#bx').show();
}

function dohide_show(val){
    if(val=='diff')
    {       
        $('#bx').show();
        $('#paypal_emailid').val('');
        document.getElementById('p_diff').checked=true;
        $('#temp_mid').val('clicked');
    }
    else if(val=='same')
    {       
        $('#bx').hide();
        $('#paypal_emailid').val($('#email_id').val());
        $('#temp_mid').val('clicked');  
    }
    else
    {
        $('#bx').hide();
        $('#paypal_emailid').val($('#email_id').val());
        $('#temp_mid').val('clicked');
    }

}

and here, in a completely separate file:

function hide_box()
            {
                $('#bx').hide();
                $('#paypal_emailid').val('');
                $('#temp_mid').val('clicked');
            }
            function show_box()
            {
                $('#bx').show();
                $('#paypal_emailid').focus();
                $('#temp_mid').val('clicked');
            }

Also, Paypal Email ID is just checking the paypal email address that the user put in if they have a paypal account. Thanks for the help everyone.

4
  • please provide the dohide_show(this.value); function Commented Oct 21, 2012 at 16:23
  • What alert is called? What is paypal_emailid element? What function is populate the temp_mid element? Provide more information about your problem and I will try to help you Commented Oct 21, 2012 at 16:31
  • I will try to get you more information on this tomorrow, as I don't have access to it now. Sorry Commented Oct 21, 2012 at 17:09
  • The email ID just sees if the user has a paypal email, then validates it, if he/she doesn't have one, then it doesn't worry about it. The alert called is,"Please Select the Paypal Account Option". Temp_mid just sees whether to show this set of question at all. I have put the new code on the edit. Commented Oct 22, 2012 at 18:38

3 Answers 3

1

I see that you doesn't repopulate the id="temp_mid" field but check it here if(temp=="") . Maybe you need to repopulate it too?

Sign up to request clarification or add additional context in comments.

2 Comments

try to replace the <input type="hidden" id="temp_mid" value="" /> with <input type="hidden" id="temp_mid" value="<?php echo ($this->input->post('mail_account')) ? 'clicked' : ''?>" />
It means repopulate the temp_mid field with clicked value if mail_account field is passed in the POST
0

What error message are you getting, "Please select the PayPal account option."?

This will always fail

<input type="hidden" id="temp_mid" value="" />

......
var temp=$('#temp_mid').val();
if(temp=="")
{
    alert('Please select the PayPal account option.');                  
    $('#p_same').focus();
    return false;
}

3 Comments

well, it doesn't always fail....Only when something else fails. But wait, why does that always fail? Well, I always get Please select the Paypal option after something else that fails validation is fixed.
Please update your code above with any changes you made. What does temp_mid do?
just hides the radio boxes or not. Added that code to the edit.
0

As @Mikhail suggested you are not repopulating the value for the temp_mid hidden field. In order to avoid having to repopulate it you could just dispose that field and detect if an option for the radio button has been selected using jQuery in the following way:

function validate_playpal()
{   
    if($('[name="mail_account"]:checked').size() > 0)
    {
        alert('Please select the PayPal account option.');                  
        $('#p_same').focus();
        return false;
    }
    ...
    return true;
}

Notice the if statement in the third line $('[name="mail_account"]:checked').size() > 0.

Hope it helps-

Comments

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.