CodeIgniter Forums
How to send a zero value in this form particulary case - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to send a zero value in this form particulary case (/showthread.php?tid=54236)



How to send a zero value in this form particulary case - El Forum - 08-29-2012

[eluser]Elias Rufino[/eluser]
Hello guys.

I have some work to do in a project that I didn't write. I have not much experience with CI but I did lots of things they asked me to do. But I have a problem with a form, and here is the problem:

The form ask for user address. It has a "number" to the residence, and nowadays it's working in this way:

views/form
Code:
(...)
         <tr>
          <td colspan="2"><span class="red-text">*</span> Number:<br />
           &lt;input name="numberx" type="text" id="numberx" size="15" value="&lt;?php echo set_value('numberx', @$numberx); ?&gt;" maxlength="6" /&gt;
          </td>
         </tr>

(...)

controllers/form
Code:
(...)
        $this->form_validation->set_rules('numberx', '<strong>Number</strong>', 'required|is_natural_no_zero');
(...)

So, the problem is, they asked me to put an option that the user could set that the address has no number (n/n), so I did the follow:

views/form

Code:
(...)
           &lt;input type="text" name="numberx" id="numberx" size="15" value="&lt;?php echo set_value('numberx', @$numberx); ?&gt;" maxlength="6"/&gt;
&lt;input type="checkbox" name="disablepo" here is a onclick equals to .. disablepo() ..(I can't put it here, don't know why)  /&gt; No number <br />

(...)


In my global.js, I used:

Code:
function disablepo() {
            var x;
            x = document.getElementById("numberx");
            if (x.disabled != true) {
                x.disabled = true;
            }
            else
            {
                x.disabled = false;
            }
    }

It worked. But in the case that user doesn't have a number, I must send a zero value to form, it will be placed in a database. And I really don't know how to send this zero in the form, if the user check "No number" option.

Can someone please give me a light?


Thank you in advance,

Elias Rufino


How to send a zero value in this form particulary case - El Forum - 08-29-2012

[eluser]CroNiX[/eluser]
Code:
&lt;input type="hidden" name="disablepo" value="0" /&gt;
&lt;input type="checkbox" name="disablepo" value="1" /&gt;
if the checkbox isn't checked, it will send 0 for the disablepo field. If it is, it will send 1.

Another way is to just use the checkbox, and in the controller do:
Code:
$disablepo = set_value('disablepo', 0);
which will set the value to 0 if it doesn't exist (meaning it wasn't checked so it wasn't sent) and 1 if it does.

Another way in the controller using only the checkbox...
Code:
$disablepo = (int)$this->input->post('disablepo');
which will force disablepo to be an integer which will be 0 if it doesn't exist since the posted value will return boolean FALSE, which you are converting to an integer (0).


How to send a zero value in this form particulary case - El Forum - 09-05-2012

[eluser]Elias Rufino[/eluser]
Thank you, it worked and give me new ideas about coding.

Again, thanks!!


Cya