CodeIgniter Forums

Full Version: Help about javascript
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Wondering Coder[/eluser]
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>
                <div class="rleft">Username <span style="color:red">*</span></div><div class="fright">
                    &lt;input name="username" type="text" id="username" size="20" title="Username" class="rtb" value="&lt;?php echo set_value('username');?&gt;"&gt;
                &lt;?php echo form_error('username');?&gt;</div>
                <div class="rleft">Password <span style="color:red">*</span></div><div class="fright">
                    &lt;input name="password" type="password" id="password" size="20" title="Password" class="rtb" value="&lt;?php echo set_value('password');?&gt;"&gt;
                &lt;?php echo form_error('password');?&gt;</div>
                <div class="rleft">Confirm&nbsp;Password <span style="color:red">*</span></div><div class="fright">
                    &lt;input name="password2" type="password" id="cpassword" size="20" title="Confirm Password" class="rtb" value="&lt;?php echo set_value('password2');?&gt;"&gt;
                &lt;?php echo form_error('password2');?&gt;</div>
                <div class="rleft">Last Name <span style="color:red">*</span></div><div class="fright">
                    &lt;input name="lname" type="text" id="firstname" title="First Name" class="rtb" value="&lt;?php echo set_value('lname');?&gt;"&gt;
                &lt;?php echo form_error('lname');?&gt;</div>
                <div class="rleft">First Name <span style="color:red">*</span></div><div class="fright">
                    &lt;input name="fname" type="text" id="lastname" title="Last Name" class="rtb" value="&lt;?php echo set_value('fname');?&gt;"&gt;
                &lt;?php echo form_error('fname');?&gt;</div>
                <div class="rleft">Middle Initial <span style="color:red">*</span></div><div class="fright">
                    &lt;input name="mname" type="text" id="middlename" size="5" title="Middle Initial" value="&lt;?php echo set_value('mname');?&gt;"&gt;
                &lt;?php echo form_error('mname');?&gt;</div>
                <div class="rleft">E-mail Address <span style="color:red">*</span></div><div class="fright">
                    &lt;input NAME="email_add" type="text" id="email" title="Email" class="rtb" value="&lt;?php echo set_value('email_add');?&gt;"&gt;
                &lt;?php echo form_error('email_add');?&gt;</div>
                <div id="companydetails">
                    <div class="rleft">Position/Title <span style="color:red">*</span></div><div class="fright">
                        &lt;input NAME="cposition" type="text" id="cposition" title="Position/Title" class="tb"&gt;
                    <div class="rleft">Company Name <span style="color:red">*</span></div><div class="fright">
                        &lt;input NAME="cname" type="text" id="cname" title="Company Name" class="tb"&gt;
                    <div class="rleft">Office Address <span style="color:red">*</span></div><div class="fright">
                        &lt;input NAME="caddress" type="text" id="caddress" title="Office Address" class="tb"&gt;
                    <div class="rleft">Contact Number <span style="color:red">*</span></div><div class="fright">
                        &lt;input NAME="ccontact_no" type="text" id="ccontact_no" title="Office Address" class="tb"&gt;
                <div class="rleft">&nbsp;</div>
                <div class="fright">
                    &lt;input type="submit" value="Save" name="submit" id="regsubmit"&gt;
                    &lt;input type="reset" value="Clear" &gt;&lt;/div>

and my javascript function
function rToggle()
    if ($('#role').val() == '1') {
    else {
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?

El Forum

That's happening because even if those form fields are getting hidden, their values are not being emptied and are getting submitted along with the visible fields.

In the rToggle() function, in addition to hiding/showing the required form fields, you must also clear out all the values that were typed into the form that's about to be hidden.

Check this out:

El Forum

You can also try making two separate forms, with different submit button names "submit1" and "submit2"... display only the relevant form using jQuery, just like you are doing now. Finally, in your CI code, check which submit button was clicked and reroute accordingly:

    //do something
else if($this->input->post('submit2'))
    //do something else