简体   繁体   中英

Passing calculated javascript variable value to text field using php codeigniter

I'm trying to fetch javascript variavle value to text field, but I'm getting an error.

[object Object]

在此处输入图片说明

I'm trying to calculate difference between two dates and want to show that in text field and after that want to save it in database.

When I'm displaying the result in span tab and it gives me proper result. 在此处输入图片说明

Here is my code: View file

<div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11"> 
                        <div class='input-group date'  name="exam_date"  id='from_date' data-date="" data-date-format="yyyy-mm-dd">
                            <input type='text' class="form-control" id="datepicker1" id="yes" name="property_gas_issue_date" placeholder="Issue Date " />
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11">
                        <div class='input-group date'  name="exam_date"  id='from_date' data-date="" data-date-format="yyyy-mm-dd">
                            <input type="text" class="form-control" id="datepicker8" name="property_gas_expiry_date" placeholder="Gas expiry date"/>
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11">
                        <!-- <input type="text" id="diff" name="property_gas_certificate_duration_days" placeholder="duration in days" value="" class="form-control"> -->
                        <strong><span> Renewal days count:</span> <span id='diff'> - </span> <span> Days</span></strong>
                    </div>
                </div>
            </div>

Note: In above code I just comment textbox code.

JS code:

<script>
  $('#datepicker1').datepicker();
  $('#datepicker8').datepicker();

  $('#datepicker8').change(function () {
      var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
      $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);
  });
</script>

This JS code for textbox field:

<script>
  $('#datepicker1').datepicker();
  $('#datepicker8').datepicker();

  $('#datepicker8').change(function () {
      var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
      var result = $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);

      document.getElementById("diff").value = result;
  });
</script>

Any kind of help is welcome, thanks in advance.

Updated question:

here is my code:

<script>

  var date1 = new Date();
  date1.setMonth(date1.getMonth() + 12);
  $('#datepicker4').datepicker("setDate", new Date());
  $('#datepicker5').datepicker("setDate", date1);

  //$('#datepicker4').datepicker();
  $("#datepicker4").datepicker({ onSelect: function(dateText) { var dateofDatePicker1 = dateText ;
  $('#datepicker5').trigger('change'); } });

  $('#datepicker5').datepicker();
  $('#datepicker5').change(function () {
      var gasDiff= $('#datepicker5').datepicker("getDate") - $('#datepicker4').datepicker("getDate");
      var result = gasDiff / (1000 * 60 * 60 * 24) * -1;

      document.getElementById("gasDiff").value = result;
  });
</script>

output: 在此处输入图片说明

In above picture the result textbox is empty.

Update result variable in here, try it

 <script>
    $('#datepicker1').datepicker();
    $('#datepicker8').datepicker();

    $('#datepicker8').change(function () {
        var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
        var result =  diff / (1000 * 60 * 60 * 24) * -1 ;

        document.getElementById("diff").value = result;
    });
</script>    

To Add month to date use date.js plugin. Please reference datejs and use below to add month as below and traigger change

       $("#datepicker4").datepicker({
            onSelect: function (dateText) {
                var nextMonth = new Date(dateText).add(1).month().toString('MM/dd/yyyy');
                $('#datepicker5').val(nextMonth);
                $('#datepicker5').trigger('change');
            }
        });
        $('#datepicker5').datepicker();
        $('#datepicker5').change(function () {
              var diff = $('#datepicker4').datepicker("getDate") - $('#datepicker5').datepicker("getDate");
              $('#diff').val(diff / (1000 * 60 * 60 * 24) * -1);
        });

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