user547794
user547794

Reputation: 14511

POST javascript variable with php form submission

I am using Goole Maps and want to include 2 javascript variables with a form post (longitude and latitute of the marker). Here's what I have:

 function saveData() {
      var name = escape(document.getElementById("name").value);
      var address = escape(document.getElementById("address").value);
      var type = document.getElementById("type").value;
      var latlng = marker.getLatLng();
      var lat = latlng.lat();
      var lng = latlng.lng();

I need to include "var lat" and "var lng" with an existing form post.

<form id="form1" name="form1" method="post" action="catchprocess.php">

Thanks for any help you con provide!

Upvotes: 1

Views: 2137

Answers (2)

Bhanu Prakash Pandey
Bhanu Prakash Pandey

Reputation: 3785

you can assign these lattitude and longitude inside hidden field in a form

  var lat = latlng.lat();
      var lng = latlng.lng();
    document.getElementById("t1").value=lat;
    document.getElementById("t2").value=lng;

   <form id="form1" name="form1" method="post" action="catchprocess.php">
        <input type="hidden" name="lat" id="t1">
        <input type="hidden" name="long" id="t2">
//form end

In catchprocess.php,you can access thes lat,long through

$_POST['lat'] and $_POST['long'] 

Upvotes: 4

miqbal
miqbal

Reputation: 2227

var inp = document.createElement('input');
inp.setAttribute('type', 'text'); // or hidden
inp.setAttribute('name', 'lat');
inp.setAttribute('value', lat);

document.getElementById("form1").appendChild(inp)

Upvotes: 1

Related Questions