Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit Button that calls js function and then posts to mysql

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 & lng values into a hidden field.

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.

Again, everything is working in my code I just need to make both buttons in the form into one and it still work.

This is the Javascript:

<script type="text/javascript">

    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);
            }
        });
    }
</script>

This is the form:

<form align="center" method="post">
    <label for="firstName">First Name:</label> <input type="text" name="firstName"/><br/>
    <label for="lastName">Last Name:</label> <input type="text" name="lastName"/><br/>
    <label for="address1">Address 1:</label> <input type="text" id="address" name="address1"/><br/>
    <label for="address2">Address 2:</label> <input type="text" name="address2"/><br/>
    <label for="city">City:</label> <input type="text" id="city" name="city"/><br/>
    <label for="state">State:</label><select id="state" name="state">
    <option value="CA"<?php echo(isset($_POST['state'])&&($_POST['state']=='CA')?' selected="selected"':'');?>>CA</option>
</select><br/>
    <label for="zip">ZIP Code:</label><input type="text" id="zip" name="zip">
    <br/>
    <label for="location">Location:</label><select name="location">
    <option value="Curb"<?php echo(isset($_POST['location'])&&($_POST['location']=='Curb')?' selected="selected"':'');?>>Curb</option>
    <option value="Garage"<?php echo(isset($_POST['location'])&&($_POST['location']=='Garage')?' selected="selected"':'');?>>Garage</option>
    <option value="Driveway"<?php echo(isset($_POST['location'])&&($_POST['location']=='Driveway')?' selected="selected"':'');?>>Driveway</option>
    <option value="FrontDoor"<?php echo(isset($_POST['location'])&&($_POST['location']=='FrontDoor')?' selected="selected"':'');?>>Front Door</option>
    <option value="SideDoor"<?php echo(isset($_POST['location'])&&($_POST['location']=='SideDoor')?' selected="selected"':'');?>>Side Door</option>
    <option value="Inside"<?php echo(isset($_POST['location'])&&($_POST['location']=='Inside')?' selected="selected"':'');?>>Inside</option></select><br/>
    <input type="hidden" id="lat" name="lat" value="" /><br>
    <input type="hidden" id="lng" name="lng" value="" /><br>
    <input type="button" value="Find Geopoints" onclick="codeAddress()"/>
    <input name="submit" type="submit" id="submit" value="Submit"/>
</form>

This is the PHP for posting the data to MySQL

<?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);
}

?>
like image 311
RugerSR9 Avatar asked Sep 18 '13 17:09

RugerSR9


1 Answers

Give the form an ID,

<form id="geoform" align="center" method="post">

Get rid of the submit button and just have,

<input type="button" value="Submit" onclick="codeAddress()"/>

And then submit with JS,

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);
            // Trigger the form submission here
            document.getElementById("geoform").submit();
        } else {
            alert("The address you entered could not be found: " + status);
        }
    });
}

And finally in your PHP code change this line,

if(isset($_POST['submit']))

to this instead,

if($_SERVER['REQUEST_METHOD'] === 'POST')
like image 199
PherricOxide Avatar answered Oct 13 '22 00:10

PherricOxide