首页 > 解决方案 > action="javascript:void(0) 时提交表单

问题描述

我有 html 步骤表单 我需要在表单通过验证并填写所有字段后提交表单。我有一个动作控制器 register.php 但在我的 html 表单中我也有 action="javascript:void(0);" 那么如何提交我的表单操作。

<div class="form-bottom">
  <div class="row">
    <div class="form-group col-xl-4 col-lg-4 col-md-12 col-sm-12 col-12">
      <input type="text" class="form-control" placeholder="addres 1" id="add1" name="" value="" |>
    </div>
    <div class="form-group col-xl-4 col-lg-4 col-md-12 col-sm-12 col-12">
      <input type="text" class="form-control" placeholder="address 2" id="add2" name="" value="">
    </div>
    <div class="form-group col-xl-4 col-lg-4 col-md-12 col-sm-12 col-12">
      <input type="text" class="form-control" placeholder="city" id="city" name="" value="" |>
    </div>
  </div>
  <div class="row">
    <div class="form-group col-xl-4 col-lg-4 col-md-12 col-sm-12 col-12">
      <input type="text" class="form-control" placeholder="Home Phone" id="contact_number" name="" value="">
    </div>
  </div>
  <div class="form-group col-xl-4 col-lg-4 col-md-12 col-sm-12 col-12">
    <input type="checkbox" name="vehicle" value="Car" checked> I Agree<br>
  </div>
  <button type="button" class="btn btn-previous">Previous</button>
  <button type="submit" class="btn">Submit</button>
</div>

标签: javascripthtmlforms

解决方案


您可以使用 ajax 查询将数据发送到 php 脚本。

该代码插入 onsubmit 函数:

var formData = $(form_selector).serialize(); //Getting data from form
$.ajax({
    type: "POST", 
    url: URL to php script, 
    data: formData, 
    success: function(data) {
        //Action if data successfully sended, it's not nessesary 
    } 
});

您可以在https://api.jquery.com/Jquery.ajax上了解有关 jQuery ajax 的更多信息


推荐阅读