简体   繁体   中英

Codeigniter: Inserting JavaScript values into a database

I have a couple of textboxes that the users enters numeric values and this is calculating the total with JavaScript.

The problem is that when I need to insert the data into my database, the total is NULL . I understand that it is client side script.

This is my form:

<tr>
    <th>Normal time</th>
    <td><input name="normal_monday" value="" onblur="findTotalmonday()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normal_thus" value="" onblur="findTotalthus()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normal_wens" value="" onblur="findTotalwens()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normal_thu" value="" onblur="findTotalthu()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normal_fri" value="" onblur="findTotalfri()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normal_sat" value="" onblur="findTotalsat()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normal_sun" value="" onblur="findTotalsun()" onkeyup="calculateTotal()" class="normaltime text-center" type="text" size="2"  id="normaltime"></td>
    <td><input name="normaltimetotal[]" value="" onkeyup="calculateTotal()" class="normaltimetotal text-center" type="text" size="2" id="totaltime1" readonly></td>
</tr>

This is my JS:

function findTotalmonday(){
    var arr = document.getElementsByName('monday');
    var tot=0;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('totalmonday').value = tot;
}

This is my Controller:

$userData = array(
    'name'              => $this->input->post('name'),
    'employee_code'     => $this->input->post('employee_code'),
    'weekno'            => $this->input->post('weekno'),
    'startdate'         => $this->input->post('startdate'),
    'enddate'           => $this->input->post('enddate'),
    'normal_mon'        => $this->input->post('normal_monday'),
    'normal_thus'       => $this->input->post('normal_thus'),
    'normal_wens'       => $this->input->post('normal_wens'),
    'normal_thu'        => $this->input->post('normal_thu'),
    'normal_fri'        => $this->input->post('normal_fri'),
    'normal_sat'        => $this->input->post('normal_sat'),
    'normal_sun'        => $this->input->post('normal_sun'),
    'normal_time_total' => $this->input->post('normal_time_total'),
);

And this is the error:

(name, employee_code, weekno, startdate, enddate, normal_mon, normal_thus, normal_wens, normal_thu, normal_fri, normal_sat, normal_sun, normal_time_total, created, modified)
VALUES ('Werner', 'WPR', '2019-W04', '2019-01-22', '2019-01-22', '8', '8', '8', '8', '8', '8', '', NULL, '2019-01-22 11:16:39', '2019-01-22 11:16:39')

Now is their a way to change my code to work or is there an easier way achieving this?

Form fields which are 'readonly' don't always submit data as part of the form, create a 'hidden' element with the same name, that will submit the data:

<input name="normaltimetotal" value="" onkeyup="calculateTotal()" class="normaltimetotal text-center" type="text" size="2" id="totaltime1" readonly>
<input name="normaltimetotal" value="" type="hidden">

However I'm a bit confused why you have a readonly field, but a 'onkeyup' event attached to it ... please let me know if this solves your immediate issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM