Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2013
    Thanked 0 Times in 0 Posts

    Inserting javascript variable into mysql database using php

    I created a form that the user selects 2 addresses on. When the user submits the form it goes to another .php page to be processed and added to the mysql database.

    On the process page I've added the google distance matrix api. So now when the process page loads it tells me how many kilometers are between the two postal codes, perfect.

    However now I want to insert that distance into a mysql database without any more user intervention.

    When the google code runs it throws the distance in a javascript variable. I tried converting that to a PHP variable but no luck.

    Next I added a textbox which automatically fills with the javascript variable (distance).

    I then tried converting that to a php variable but again no luck.

    If I can get the number stored as a php variable I'm sure I can get it in the database but I'm having no luck with the javascript variable.

    This is the javascript
    var distanceInput = document.getElementById("distance");
    distanceInput.value = response.routes[0].legs[0].distance.value / 1000;

    This is the textbox
    PHP Code:
                    <label for="distance">Distance (km): </label>
    input type="text" name="distance" id="distance" readonly /> 

    Then I added this code to auto fill the textbox when the page loads.
    PHP Code:
    echo '<script type="text/javascript">    calcRoute();      </script>'

    I would like to use a regular sql insert like the following.
    PHP Code:
    INSERT INTO shipments (DistanceVALUES ('distanceInput'WHERE "I'll figure it out later" 
    Any ideas would be much appreciated.

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Thanked 1,007 Times in 1,004 Posts
    Next I added a textbox which automatically fills with the javascript variable (distance).
    Use an input in your form to show this. That way it will submit to your php and can be stored.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts