提交调用js函数的Button然后发布到mysql

时间:2013-09-18 17:54:31

标签: javascript php html mysql geocoding

对我所做的简短而甜蜜的描述是一个联系表单,用于对用户地址进行地理编码(一旦他们点击提交)并将所提取的lat和&将值变为隐藏字段。

我有一切工作,但我有两个单独的按钮,我只想要一个。因此,用户必须先点击一个按钮,然后才能点击提交按钮,这是愚蠢的。 “Find Geopoints”按钮调用js函数并将lat和lng值返回到表单上的隐藏字段中,然后按下提交按钮,它将所有表单数据发布到MySQL数据库中。

同样,一切都在我的代码中工作我只需要将表单中的两个按钮合二为一,它仍然可以工作。

这是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>

这是表格:

<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>

这是用于将数据发布到MySQL的PHP​​

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

?>

1 个答案:

答案 0 :(得分:15)

为表单提供ID

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

摆脱提交按钮,只需拥有,

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

然后用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);
        }
    });
}

最后在您的PHP代码中更改此行,

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

代替,

if($_SERVER['REQUEST_METHOD'] === 'POST')