2011-06-06 43 views
0

我有一個註冊表格幫助有關JavaScript removeclass

<form class="sform" method="post" action="<?=base_url()?>login/create_member" > 
       <div class="rleft">User Type <span style="color:red">*</span></div> 
       <div class="fright"> 
       <select name="role" id="role" onchange="rToggle()"> 
       <option value="1">Company</option> 
           <option value="2">Student</option> 
       </select> 
       </div> 
       <div class="rleft">Username <span style="color:red">*</span></div><div class="fright"> 
        <input name="username" type="text" id="username" size="20" title="Username" class="rtb" value="<?php echo set_value('username');?>"> 
       <?php echo form_error('username');?></div> 
       <div class="rleft">Password <span style="color:red">*</span></div><div class="fright"> 
        <input name="password" type="password" id="password" size="20" title="Password" class="rtb" value="<?php echo set_value('password');?>"> 
       <?php echo form_error('password');?></div> 
       <div class="rleft">Confirm&nbsp;Password <span style="color:red">*</span></div><div class="fright"> 
        <input name="password2" type="password" id="cpassword" size="20" title="Confirm Password" class="rtb" value="<?php echo set_value('password2');?>"> 
       <?php echo form_error('password2');?></div> 
       <div class="rleft">Last Name <span style="color:red">*</span></div><div class="fright"> 
        <input name="lname" type="text" id="firstname" title="First Name" class="rtb" value="<?php echo set_value('lname');?>"> 
       <?php echo form_error('lname');?></div> 
       <div class="rleft">First Name <span style="color:red">*</span></div><div class="fright"> 
        <input name="fname" type="text" id="lastname" title="Last Name" class="rtb" value="<?php echo set_value('fname');?>"> 
       <?php echo form_error('fname');?></div> 
       <div class="rleft">Middle Initial <span style="color:red">*</span></div><div class="fright"> 
        <input name="mname" type="text" id="middlename" size="5" title="Middle Initial" value="<?php echo set_value('mname');?>"> 
       <?php echo form_error('mname');?></div> 
       <div class="rleft">E-mail Address <span style="color:red">*</span></div><div class="fright"> 
        <input NAME="email_add" type="text" id="email" title="Email" class="rtb" value="<?php echo set_value('email_add');?>"> 
       <?php echo form_error('email_add');?></div> 
       <div id="companydetails"> 
        <div class="rleft">Position/Title <span style="color:red">*</span></div><div class="fright"> 
         <input NAME="cposition" type="text" id="cposition" title="Position/Title" class="tb"> 
        </div> 
        <div class="rleft">Company Name <span style="color:red">*</span></div><div class="fright"> 
         <input NAME="cname" type="text" id="cname" title="Company Name" class="tb"> 
        </div> 
        <div class="rleft">Office Address <span style="color:red">*</span></div><div class="fright"> 
         <input NAME="caddress" type="text" id="caddress" title="Office Address" class="tb"> 
        </div> 
        <div class="rleft">Contact Number <span style="color:red">*</span></div><div class="fright"> 
         <input NAME="ccontact_no" type="text" id="ccontact_no" title="Office Address" class="tb"> 
        </div>      
       </div>     
       <div class="rleft">&nbsp;</div> 
       <div class="fright"> 
        <input type="submit" value="Save" name="submit" id="regsubmit"> 
        <input type="reset" value="Clear" ></div> 



      </form> 

和我的javascript功能

function rToggle() 
{ 
    if ($('#role').val() == '1') { 
     $('#companydetails').hide(); 
     $('#cposition').removeClass('rtb');  
     $('#cname').removeClass('rtb');    
     $('#caddress').removeClass('rtb');  
     $('#ccontanct_no').removeClass('rtb');  
    } 
    else { 
     $('#companydetails').show(); 
     $('#cposition').addClass('rtb');  
     $('#cname').addClass('rtb');    
     $('#caddress').addClass('rtb');  
     $('#ccontanct_no').addClass('rtb');  
    } 
} 

這個工作是這樣的:

如果我選擇2(學生),它只會顯示此字段:用戶名,傳球,公共社會行動中心,FNAME,MNAME,EMAIL_ADDRESS

如果我選擇1(公司),它會顯示包括所有領域上述領域和CNAME,cposition,caddress,ccontact_no

現在的問題是,當我填寫,然後選擇2(學生)的所有字段還是會保存在我的數據庫中的所有POST數據。我想要的是即使我填滿了所有的領域,然後選擇2(學生)在公司細節的人現在必須保存。 任何人都知道要解決這個問題?或在JavaScript的專家?

+0

您使用JQuery的,對不對?如果是,請將其包含在標籤中。 – 2011-06-06 10:54:48

回答

0

您應該將PHP添加到您的標籤。

但要回答你的問題:這是一個服務器端問題。用JavaScript來解決是不夠的。
在您的PHP代碼中,無論您編寫INSERT或UPDATE查詢,您都必須檢查角色是否爲2,如果不是,則不要添加額外的字段。

爲了避免被傳遞到服務器的額外字段時不必要的,您可以更新這樣的代碼:

if ($('#role').val() == '1') { 
    // ... 

    $('#cposition').disabled = false; 
    // etc. for the other inputs 
} 
else { 
    // ... 

    $('#cname').disabled = true; 
    // etc. 
}