Help about javascript removeclass
I have a sign up form
<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>
开发者_JS百科 </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 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"> </div>
<div class="fright">
<input type="submit" value="Save" name="submit" id="regsubmit">
<input type="reset" value="Clear" ></div>
</form>
and my javascript function
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');
}
}
This works like this:.
If I chose 2(Student), it will only show the this fields: username, pass, cpass, fname, mname, email_address
If I chose 1(company), it will show all fields including the fields above and cname, cposition, caddress, ccontact_no
Now the problem is when I fill up all fields then chose 2(students) it will still save all post data in my db. What I want is even if I fill up the all fields then chose 2(student) those in companydetails must now be save.! Anyone know to fix this? or expert in javascript?
You should add PHP to your tags.
But to answer your question: this is a server side issue. Solving it with JavaScript isn't enough.
In your PHP code, wherever you write your INSERT or UPDATE query, you must check for role being 2 and not add your extra fields if it's not.
To avoid having the extra fields being passed to your server when unnecessary, you can update your code like this:
if ($('#role').val() == '1') {
// ...
$('#cposition').disabled = false;
// etc. for the other inputs
}
else {
// ...
$('#cname').disabled = true;
// etc.
}
精彩评论