Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - PHP Set_Radio on Radio Input

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.

like image 437
TheChes44 Avatar asked Nov 13 '22 20:11

TheChes44


1 Answers

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?

like image 78
Mikhail Avatar answered Nov 15 '22 08:11

Mikhail