duane9322 2016-02-02 21:20
浏览 64

Javascript表单提交无法正确提交

I have this very simple form using Sweet Alert and for some reason can't get it to work. When I click on the link it triggers the alert and when the alert is confirmed it reloads the page because it submits the form.

But for some reason when $_POST['delete_alert'] is undefined.

My code to trigger the alert:

<a id="sa-warning">Delete</a>

My form:

<form id="form" action="index.php" method="POST">
    <button type="submit" name="delete_alert" value="delete">Delete</button>
</form>

And my Javascript:

$('#sa-warning').click(function() {
  swal({
    title: "Are you sure?",
    text: "You will not be able to recover this alert.",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    closeOnConfirm: false
  }, function() {
    document.forms["form"].submit();
  });
});

I'm testing wheter the form has been submitted properly using this PHP code which is located on index.php:

<?php 
if(isset($_POST['delete_alert'])) { 
    echo "form is submitted correctly"; 
} else { 
    echo "something is wrong"; 
} 
?>
  • 写回答

3条回答 默认 最新

  • dongyi1748 2016-02-02 22:58
    关注

    I think the problem is in document.forms["form"].submit();

    HTML forms send the submit name=value only if button is clicked. In this case it is not.

    So i think you should use click instead of submit, but in your case it wont work, because the click will be catched by your javascript function.

    So this is why the best solution si to use input

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度