smolik
smolik

Reputation: 11

Getting JavaScript data calculated from an HTML form, and adding it to the same form action as a new element to save with PHP

This is the form:

<form role="form" action="<?php echo $action; ?>" name="send_request" method="post" onsubmit="calcDist();" id="send_request">
  <div class="content">
    <div class="form-group">
      <input type="text" id="input-from" class="form-control" name="from" placeholder="<?php echo $entry_from; ?>" />
      <input type="text" id="input-to" class="form-control" name="to" placeholder="<?php echo $entry_to; ?>" />
      <input type="hidden" class="form-control" name="kmdist" value="">
    </div>
    <input type="submit" value="<?php echo $button_send; ?>" class="btn btn-default" />
  </div>
</form>

I'm computing the distance in calcDist(); between the given cities from the text inputs on submit and save the cities to the database through the form action, but I don't know how to add that distance variable to the post data array before the actual saving action.

Upvotes: 0

Views: 40

Answers (1)

GluePear
GluePear

Reputation: 7725

Just set the value of the hidden field kmdist in calcDist():

HTML:

<input type="hidden" class="form-control" name="kmdist" id="kmdist" value="">

Javascript:

document.getElementById('kmdist').value = calculatedDistance;

Upvotes: 1

Related Questions