php - javascript array Not Working properly

923

I am facing some problems arrays in php,,

<input  placeholder="Start Time"
value="'.date("H.i").'" type="text" name="starttime[]" id="starttime"/>
</div>
<div >
<input  name="endtime[]" id="endtime" onchange="myfunction1()" value="00.00" type="text" placeholder="End Time">

This is my ajax code and when I change end time It must be show other text box

<input class="form-control" name="kmcost" id="exkms" type="text" placeholder="Cost">

But single entry working but second entry(array) Not woeking my javascript code is here please help me..

my javascript is here..

function myfunction1()
{
    var ftime = document.getElementById("starttime").value;
    var etime = document.getElementById("endtime").value;

    var timeanswer = etime - ftime;

    var localtottme = document.getElementById("textbox3").value;
    if(timeanswer > 8)
    {
        var totextme = timeanswer-8;
    }
   var exkms = document.getElementById('exkms');
exkms.value=totextme;
}
92

Answer

Solution:

Try this:

function myfunction1()
{
    // fetch value of element as string, x2
    var ftime = document.getElementById("starttime").value;
    var etime = document.getElementById("endtime").value;

    // fetch hour as string, then convert to integer, x2
    ftime = parseInt(ftime.split(':')[0]);
    etime = parseInt(etime.split(':')[0]);

    // calculate hour difference, initialize remainder variable to 0
    var hourDifference = etime - ftime;
    var eightHourRemainder = 0;

    // check if hour difference is more than 8, if so then calculate remainder variable
    var localtottme = document.getElementById("textbox3").value;
    if(hourDifference > 8)
    {
        eightHourRemainder = hourDifference - 8;
    }

    // set element value to remainder variable
    var exkms = document.getElementById('exkms');
    exkms.value=eightHourRemainder;
}

People are also looking for solutions to the problem: php - html data to array for mySQL insertion

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.