【问题标题】:Duplicate form submition through $("#formname").submit();通过 $("#formname").submit(); 重复提交表单
【发布时间】:2012-08-07 03:00:06
【问题描述】:

我有一行 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;
    }
}

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

有什么想法吗?

【问题讨论】:

  • 给db字段一个唯一索引,问题解决了。
  • var_dump($exists) 在您插入之前查看您的逻辑是否有效。
  • 您是说它发布了两次还是您指的是您的 SQL 代码?类“.checkvalueonchange”是否在执行任何 javaScript 函数?另外,如果你运行 jQuery(".savevalueonchange").data("events") 连接了多少事件?

标签: javascript php jquery duplicates submission


【解决方案1】:

尝试在 autosave() 中添加return false;,如下所示:

function autoSave() {

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

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

       return false;  // add here
   }
}

它会阻止提交两次。

【讨论】:

    【解决方案2】:
    <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;"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      相关资源
      最近更新 更多