提交表单并等待Web SQL查询

时间:2015-08-04 20:34:49

标签: javascript jquery forms web-sql

我使用基本的CGI表单电子邮件脚本(formmail.pl)将表单的内容作为电子邮件发送,但我也从表单字段中触发了一系列SQL UPDATE事件。问题是如果SQL语句没有完成我的数据库没有更新...如何设置它以便只在SQL更新完成后提交表单?

var tblName = "comment_collect"
var usrName = "***dney"

// FORM 
$("#allSubmitBtn").click(function (e) {
    //CHECK IF has a comment
    if (!notEmpty(document.getElementById('description1'))) {
        alert('Please enter a comment.');
        return false;
    }
    if (!notEmpty(document.getElementById('latlongit1'))) {
        alert('Sorry, there has been an error, please search for a location again.');
        return false;
    } else {
        currentNeighborhood = $('#neighborhoodName1').val();
        parcel = $('#parcel_id1').val();
        address = $('#pre_address1').val();
        userAddress = $('#UserAddress1').val();
        phoneNum = $('#phone1').val();
        emailAdd = $('#emailAddress1').val();
        userType = $('#userType1').val();
        otherUser = $('#otherUserType1').val();
        currentDescription = $('#description1').val();
        latlongy = $("input[name='latlongit1']").val();
        explainType = $('#explainType').val();
        currentProject = selectedCity.name;
        commentType = new Array();
        $("input:checkbox[name=commentType]:checked").each(function () {
            commentType.push($(this).val());
        });

        var sql = "INSERT INTO " + tblName + " (the_geom, project, description, name,comment_address,parcel_id,phone_number,email_address,comment_type,comment_type_other,user_type,user_type_other,profile_address,flag,loved) VALUES (ST_SetSRID(ST_GeomFromGeoJSON('";
        // var a = layer.getLatLng();
        // console.log(a);
        var sql2 = '{"type":"Point","coordinates":[' + latlongy + "]}'),4326),'" + currentProject + "','" + (currentDescription.replace(/'/g, "''")).replace(/"/g, "''") + "','" + (currentNeighborhood.replace(/'/g, "''")).replace(/"/g, "''") + "','" + address + "','" + parcel + "','" + phoneNum + "','" + emailAdd + "','" + commentType + "','" + explainType + "','" + userType + "','" + otherUser + "','" + userAddress + "','false','0')";
        var pURL = sql + sql2;
        console.log(pURL);
        submitToProxy(pURL);
        alert("Your Comments have been submitted");

        return true;
    }
});


var submitToProxy = function(q){
  $.post("php/callProxy.php",
    {
      qurl:q,
      cache: false,
      timeStamp: new Date().getTime()
    }, function(data) {
      console.log(data);
    });
}

submitToProxy连接到连接到CartoDB数据库API的php文件:

<?php
session_cache_limiter('nocache');
$cache_limiter = session_cache_limiter();
function goProxy($dataURL) 
{
    $baseURL = 'http://username.cartodb.com/api/v2/sql?';
    //          ^ CHANGE THE 'CARTODB-USER-NAME' to your cartoDB url
    $api = '&api_key=KEYHERE';
    //               ^ENTER YOUR API KEY HERE
    $url = $baseURL.'q='.urlencode($dataURL).$api;
    $result = file_get_contents ($url);
    return $result;
}
?>

我的电子邮件脚本是一个简单的CGI脚本......这就是表单的样子:

  <form method="post" action="cgi-bin/FormMail.pl">
        <input type="hidden" name="recipient" value="EMAIL" />
        <input id="phone" name="phone" class="phone" type="text" value="">
        <button type="submit" id="allSubmitBtn">Submit Comment</button>
  </form>

0 个答案:

没有答案