dongsi0625 2017-06-26 19:37
浏览 61

将输入从使用JavaScript和AJAX的HTML表单传递到PHP POST方法

I have a form with several inputs using Bootstrap and JavaScript that I need to pass to my PHP script using the POST method.

My JavaScript:

$(document).ready(function() {
      $("#contact_form")
        .bootstrapValidator({
      feedbackIcons: {
    valid: "glyphicon glyphicon-ok",
    invalid: "glyphicon glyphicon-remove",
    validating: "glyphicon glyphicon-refresh"
  },
  fields: {
    name: {
      validators: {
        stringLength: {
          min: 2
        },
        notEmpty: {
          message: "message"
        }
      }
    },
    n2: {
      validators: {
        stringLength: {
          min: 2,
          message: "message"
        },
        notEmpty: {
          message: "message"
        }
      }
    },
    n3: {
      validators: {
          stringLength: {
          min: 3,
          message: "message"
        },
        notEmpty: {
          message: "message"
        },
      }
    },
    n4: {
      validators: {
          stringLength: {
          min: 18,
          message: "message"
        },
        notEmpty: {
          message: "message"
        },
      }
    },
    puesto: {
      validators: {
        stringLength: {
          min: 4,
          message: "message"
        },
        notEmpty: {
          message: "message"
        }
      }
    },
    n6: {
      validators: {
        stringLength: {
          min: 4,
          message: "message"
        },
        notEmpty: {
          message: "message"
        }
      }
    },

    n7: {
      validators: {
          stringLength: {
          min: 4,
          message: "message"
        },
        notEmpty: {
          message: "message"
        },
      }
    },
  }
})
.on("success.form.bv", function(e) {
  $("#success_message").slideDown({ opacity: "show" }, "slow"); // Do something ...
  $("#contact_form").data("bootstrapValidator").resetForm();

  // Prevent form submission
  e.preventDefault();

  // Get the form instance
  var $form = $(e.target);
  console.log($form);

  // Get the BootstrapValidator instance
  var bv = $form.data("bootstrapValidator");

  // Use Ajax to submit form data
  $.post(
    $form.attr("action"),
    $form.serialize(),
    function(result) {
      console.log(result);
    },
    "json"
  );

});

});

This is the type of form am using:

<div class="container">
<script src="ce.js"></script>
<form class="well form-horizontal" action=" " method="post"  id="contact_form">
<fieldset>

<!-- Form Name -->
<legend>Title</legend>

<!-- Text input-->

<div class="form-group" action="actualizar.php" method="post">
<label class="col-md-4 control-label">Nombre</label>  
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input  name="name" placeholder="Name" class="form-control"  type="text" id="name">
</div>
</div>
</div>
<!-- Several other forms below this -->

I need to obtain the input in my PHP like this;

<?php
    $var = $_POST['name']; 
    echo $var;
 ?> 

I have all the code in one PHP file.

Its currently not obtaining data in the POST, I know the AJAX in the JS is using a post method but I have no clue of how to access that data. I appreciate all your help.

  • 写回答

1条回答 默认 最新

  • dqdz6464 2017-06-26 19:43
    关注

    Your Ajax call should be like this:

    $.ajax({
      method: 'POST',
      url: 'your-post-url-here/',
      data: $form.serialize(),
      dataType: 'json',
      success: function(result) {
        console.log(result);
      }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题