Nyaro
Nyaro

Reputation: 177

Pass Text box value to Check box value

I have a form which has a question and the answer are the check-boxes and in the last answer there is an other please specify option when i check that check-box the text-box enables and i can write in it.

My answers are saved in an array.But now what the problem is when i submit the form i want the value of the other please specify option to be the value of the text-box which is filled when the option is checked. Here is the form that i have:

<table>      
<tr>
      <td colspan="3" style="border-bottom:none">d.&nbsp;If you checked the boxes " very poor or poor clients", which reference point/benchmark do you use for estimating the poverty level of your clients? (Check all that apply):        </td>
      <td>
            <input type="checkbox" name="sdd1[]"  value="Persons in the bottom 50% of those living below the poverty line established by the national government"/>Persons in the bottom 50% of those living below the poverty line established by the national government <br />
            <input type="checkbox" name="sdd1[]" value="Person living on less than US $1 a day international poverty line"/>Person living on less than US $1 a day international poverty line<br />
            <input type="checkbox" name="sdd1[]" id="check"  onclick="disableMe('mfi_1_d_i_a',this)" value="mfi_1_d_i_a" />Other(please specify):
            <input type="text" name="mfi_1_d_i_a"  class="init" id="mfi_1_d_i_a" maxlength="30" disabled="disabled"/>        </td>  
</tr>
</table>

Upvotes: 0

Views: 1371

Answers (2)

Jacob
Jacob

Reputation: 8334

You can actually take advantage of some of the features of input names.

  • An input won't appear in $_POST if it is disabled.
  • An input won't appear in $_POST if it doesn't have a name.

So in your case, if you take the name off the "Other" checkbox. And set the name on the textbox to be sdd1[], then everything should work as you want.

What's actually happening is the "Other" checkbox is never being sent, but because it enabled the textbox when it is checked, then the textbox value will only be sent if Other is checked...

Hope that makes sense.


That being said, your script now relies on Javascript, which is not always recommended. If a user goes to your website with Javascript disabled then they cannot enter a value in the Other textbox at all.

A more general solution would be to load the page with the textbox enabled, and disable it with Javascript. Therefore if a user doesn't have Javascript they can still enter text in the textbox as it will still be enabled. If they have Javascript you can use your disabled/enable Javascript event when the Other checkbox is checked/unchecked.

Then in the PHP, check for the "mfi_1_d_i_a" value in the sdd1 array, if its present, read the value of the textbox.

Upvotes: 2

S L
S L

Reputation: 14318

Well i suppose you can resolve this problem by storing your value in database or server script rather than in checkboxes.

<input type="checkbox" name="sdd1[]"  value="1"/>Value 1 <br/>
<input type="checkbox" name="sdd1[]" value="2"/>value 2 <br/>
<input type="checkbox" name="sdd1[]" id="check"  onclick="disableMe('mfi_1_d_i_a',this)" value="1"/>Other(please specify):

<input type="text" name="mfi_1_d_i_a"  class="init" id="mfi_1_d_i_a" maxlength="30" disabled="disabled"/>

And on php script

switch($_POST['sdd1']){
    case 1:
        //store value1;
        break;
    case 2:
        //store value2;
        break;
    case 3:
        //store text value
        break;
    default:
        //store some value;
        break;                              
}

Upvotes: 0

Related Questions