通过$(“#formname”)提交重复的表单.submit();

时间:2012-08-09 19:54:41

标签: javascript php jquery duplicates submission

我有一行HTML下拉列表,每当用户选择其中一个选项时,它会通过提交表单的类调用jQuery函数。

<form id="workorderform" action="saveworkorder.php" method="post">
    <select name="applicationtype" class="checkvalueonchange savevalueonchange">
         <option></option>
         <option value="Uplift" <?php echo ($wo['applicationtype']=='Uplift')?'selected':''; ?>>Uplift</option>
     </select> 
 </form>

savevalueonchange类调用

$(".savevalueonchange").change(function() {
    autoSave(); 
});

然后调用

function autoSave() {
    if($("#wostatus").val()=='open'){
         $("#workorderform").submit();
    }
 }

将信息发布到saveworkorder.php

//remove existing chargecodes
$sql = "DELETE FROM workorderchargecodes WHERE compresscoid = '".trim($_REQUEST['formid'])."'";
mssql_query($sql, $dbconn);
//now save chargecodes  
$code_prefix = 'code_';
foreach($_POST as $thisPostKey => $thisPostValue) {
    if((substr($thisPostKey, 0, strlen($code_prefix)))==$code_prefix) {
        if(trim($thisPostValue)!=''):
             $exists=false;
            $sql = "SELECT * FROM workorderchargecodes WHERE compresscoid='".trim($_REQUEST['formid'])."' AND code='".substr($thisPostKey, strlen($code_prefix), strlen($thisPostKey))."'";
            $res = mssql_query($sql, $dbconn);
            while($arr = mssql_fetch_assoc($res)){
                $exists=true;
            } 
            if($exists==false){
                $sql = "INSERT INTO workorderchargecodes (
                    compresscoid,
                    code,
                    time
                    ) VALUES (
                    '".trim($_REQUEST['formid'])."',
                    '".substr($thisPostKey, strlen($code_prefix), strlen($thisPostKey))."',                         '".$thisPostValue."'
            )";
                mssql_query($sql, $dbconn);
            }   
        endif;
    }
}

正如您所看到的,我在将数据库插入数据库之前从数据库中选择代码,并且不知何故仍然会收到重复的费用代码。我已经搞砸了几个月了,它仍然在发生。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

尝试在自动保存()中添加return false;,如下所示:

function autoSave() {

   if($("#wostatus").val()=='open'){

       $("#workorderform").submit();

       return false;  // add here
   }
}

它会阻止提交两次。

答案 1 :(得分:0)

<form id="workorderform" action="saveworkorder.php" method="post" onsubmit="return false;">
    <select name="applicationtype" class="checkvalueonchange savevalueonchange">
         <option></option>
         <option value="Uplift" <?php echo ($wo['applicationtype']=='Uplift')?'selected':''; ?>>Uplift</option>
     </select> 
 </form>

使用此表格

它使用obsubmit="return false;"