4

I have built an HTML form for address data, and I want to compute the latitude and longitude of the address before I commit all form fields to a SQL database via POST variables.

Right now my console shows that I successfully edited the hidden "latInput" and "lngInput" field DOM with the lat/lng values. However, that change is not being captured by the POST variables in my PHP script.

Hidden form fields:

<input type="hidden" name="latInput" id="latInput" value="">
<input type="hidden" name="lngInput" id="lngInput" value="">

jQuery setting/checking DOM updates to hidden fields:

//insert new lat/lng variables to the hidden form inputs, so PHP can access POST and insert into SQL database
$("#latInput").attr('value',localLat);
$("#lngInput").attr('value',localLng);

//confirm hidden input fields were set with lat/lng values
console.log("latInput set to: " + $("input[name=latInput]").val());
console.log("lngInput set to: " + $("input[name=lngInput]").val());

PHP script checking the POST variables for lat/lng, and committing all fields to database:

$lat = $_POST['latInput'];
$lng = $_POST['lngInput'];

echo('<p>POST variable for lat = '.$lat.'</p>');
echo('<p>POST variable for lng = '.$lng.'</p>');

//choose SQL fields (column names), then choose HTML form fields ("name" attribute)
$sql = "INSERT INTO $tablename (name,address1,city,state,zip,latitude,longitude) VALUES ('$_POST[firstName]','$_POST[address1]','$_POST[city]','$_POST[state]','$_POST[zipCode]','$_POST[latInput]','$_POST[lngInput]')";

Please note--all the other form fields (address1, city, etc.) are successfully passing POST variables and entering the database on submit. And even though the DOM is updating for the hidden latInput and lngInput fields, this does not pull through to my local $lat and $lng variables. POST still returns no value. I've tried updating the original hidden latInput and lngInput fields with dummy data like below, and that dummy data passes through fine.

Dummy data:

<input type="hidden" name="latInput" id="latInput" value="1.234">
<input type="hidden" name="lngInput" id="lngInput" value="2.345">

Any ideas why this is dropping off?

Thank you for your help!

mpastore
  • 69
  • 1
  • 5

2 Answers2

1

I had the same problem. This answer helped me. The Solution is to use a name selector instead of id.

Form:

<form id="my-form" action="post">
  <input type="hidden" name="latInput">
  <input type="hidden" name="lngInput">
  <input type="submit" value="Submit">
</form>

JQuery

$('#my-form input[name=latInput]').val(localLat);
$('#my-form input[name=latInput]').val(localLng);

These values will be successfully passed to POST.

Anthony P.
  • 46
  • 3
0

The proper way to set input value is using val() function.

$("#latInput").val(localLat);
$("#lngInput").val(localLng);
rrk
  • 14,861
  • 4
  • 25
  • 41
  • Rejith I used this originally and tried it again--still does not work. I am updating the #latInput and #lngInput fine, but that value is not submitting as a POST variable. – mpastore Aug 16 '15 at 20:18