Note that there are some explanatory texts on larger screens.

plurals
  1. POSubmit Button that calls js function and then posts to mysql
    primarykey
    data
    text
    <p>A short and sweet description of what I am making is a contact form that geocodes a users address(once they click submit) and puts the fetched lat &amp; lng values into a hidden field.</p> <p>I have everything working but I have two seperate buttons and I only want one. So at the moment the user has to first click one button before they can click the submit button, which is dumb. The "Find Geopoints" button calls a js function and returns the lat and lng values into the hidden fields on the form and after that you would press the submit button and it posts all of the forms data into the MySQL database.</p> <p>Again, everything is working in my code I just need to make both buttons in the form into one and it still work.</p> <p><strong>This is the Javascript:</strong></p> <pre><code>&lt;script type="text/javascript"&gt; var geocoder; function initialize() { geocoder = new google.maps.Geocoder(); map = new google.maps.Map(document.getElementById("map_canvas")); } function updateCoordinates(latlng) { if(latlng) { document.getElementById('lat').value = latlng.lat(); document.getElementById('lng').value = latlng.lng(); } } function codeAddress() { var address = document.getElementById("address").value; geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { updateCoordinates(results[0].geometry.location); } else { alert("The address you entered could not be found: " + status); } }); } &lt;/script&gt; </code></pre> <p><strong>This is the form:</strong></p> <pre><code>&lt;form align="center" method="post"&gt; &lt;label for="firstName"&gt;First Name:&lt;/label&gt; &lt;input type="text" name="firstName"/&gt;&lt;br/&gt; &lt;label for="lastName"&gt;Last Name:&lt;/label&gt; &lt;input type="text" name="lastName"/&gt;&lt;br/&gt; &lt;label for="address1"&gt;Address 1:&lt;/label&gt; &lt;input type="text" id="address" name="address1"/&gt;&lt;br/&gt; &lt;label for="address2"&gt;Address 2:&lt;/label&gt; &lt;input type="text" name="address2"/&gt;&lt;br/&gt; &lt;label for="city"&gt;City:&lt;/label&gt; &lt;input type="text" id="city" name="city"/&gt;&lt;br/&gt; &lt;label for="state"&gt;State:&lt;/label&gt;&lt;select id="state" name="state"&gt; &lt;option value="CA"&lt;?php echo(isset($_POST['state'])&amp;&amp;($_POST['state']=='CA')?' selected="selected"':'');?&gt;&gt;CA&lt;/option&gt; &lt;/select&gt;&lt;br/&gt; &lt;label for="zip"&gt;ZIP Code:&lt;/label&gt;&lt;input type="text" id="zip" name="zip"&gt; &lt;br/&gt; &lt;label for="location"&gt;Location:&lt;/label&gt;&lt;select name="location"&gt; &lt;option value="Curb"&lt;?php echo(isset($_POST['location'])&amp;&amp;($_POST['location']=='Curb')?' selected="selected"':'');?&gt;&gt;Curb&lt;/option&gt; &lt;option value="Garage"&lt;?php echo(isset($_POST['location'])&amp;&amp;($_POST['location']=='Garage')?' selected="selected"':'');?&gt;&gt;Garage&lt;/option&gt; &lt;option value="Driveway"&lt;?php echo(isset($_POST['location'])&amp;&amp;($_POST['location']=='Driveway')?' selected="selected"':'');?&gt;&gt;Driveway&lt;/option&gt; &lt;option value="FrontDoor"&lt;?php echo(isset($_POST['location'])&amp;&amp;($_POST['location']=='FrontDoor')?' selected="selected"':'');?&gt;&gt;Front Door&lt;/option&gt; &lt;option value="SideDoor"&lt;?php echo(isset($_POST['location'])&amp;&amp;($_POST['location']=='SideDoor')?' selected="selected"':'');?&gt;&gt;Side Door&lt;/option&gt; &lt;option value="Inside"&lt;?php echo(isset($_POST['location'])&amp;&amp;($_POST['location']=='Inside')?' selected="selected"':'');?&gt;&gt;Inside&lt;/option&gt;&lt;/select&gt;&lt;br/&gt; &lt;input type="hidden" id="lat" name="lat" value="" /&gt;&lt;br&gt; &lt;input type="hidden" id="lng" name="lng" value="" /&gt;&lt;br&gt; &lt;input type="button" value="Find Geopoints" onclick="codeAddress()"/&gt; &lt;input name="submit" type="submit" id="submit" value="Submit"/&gt; &lt;/form&gt; </code></pre> <p><strong>This is the PHP for posting the data to MySQL</strong></p> <pre><code>&lt;?php if(isset($_POST['submit'])) { $con = mysql_connect("localhost","x","y"); if (!$con){ die('Could not connect: ' . mysql_error()); } mysql_select_db("x", $con); $sqlCmd = sprintf("INSERT INTO x (firstName, lastName, address1, address2, city, state, zip, location, lat, lng) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", mysql_real_escape_string($_POST["firstName"]), mysql_real_escape_string($_POST["lastName"]), mysql_real_escape_string($_POST["address1"]), mysql_real_escape_string($_POST["address2"]), mysql_real_escape_string($_POST["city"]), mysql_real_escape_string($_POST["state"]), mysql_real_escape_string($_POST["zip"]), mysql_real_escape_string($_POST["location"]), mysql_real_escape_string($_POST["lat"]), mysql_real_escape_string($_POST["lng"])); //echo $sqlCmd; //die(); mysql_query($sqlCmd); mysql_close($con); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload