Ajax表单:从表单获取id并提交

时间:2013-07-07 14:34:23

标签: php jquery html mysql ajax

我试图将ajax表单放在while元素中,但不起作用。 可能不能以ajax形式重复id。

<?php while(..){ ?>
<form id="cancel-server" action="process.php" method="POST">
     <input type="hidden" name="task" value="cancel-server" />
     <input type="hidden" name="serverid" value="<?php echo $row['sid']; ?>" />
     <button type="submit" id="ah">
          <i class="icon-remove"></i> Otkaži narudžbinu
     </button>    
</form> 
<?php } ?>

jquery的:

$('#cancel-server').ajaxForm({ 
    success: function(result){
        var result=trim(result);

        if(result=='success'){
            $.poruka('', 'Success!'); 
        }else{
            $.poruka('', result); 
        }
    }       
}); 

PHP:

case 'cancel-server':
    $serverid = $_POST['serverid'];

    query_basic("DELETE FROM `serveri_naruceni` WHERE `id` = '".$serverid."'");

    echo 'success';
break;

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码段:

<script>
    function _Submit(form){
       $('#cancel-server'+form.id).ajaxForm({ }););
          return false;
       }
 </script>

<form id="cancel-server<?php echo $row['id']; ?>" action="process.php" method="POST" onsubmit="javascript: return _Submit(this);">
    ...

</form>