将输入从使用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个回答



您的Ajax调用应该是这样的:</ p>

  $ .ajax({\ 方法:'POST',
url:'your-post-url-here /',
data:$ form.serialize(),
dataType:'json',
success:function(result){

console.log(result);
}
});
</ code> </ pre>
</ div>

展开原文

原文

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);
  }
});

dounieqi6959
dounieqi6959 一个空数组。 我让它工作的目的是为了我必须做的事情,ajax中的数据值正在向我发送整个页面以及正确的PHP帖子值。
大约 3 年之前 回复
doulan9287
doulan9287 你得到了什么:<?php print_r($ _ REQUEST); ?>?
大约 3 年之前 回复
douhui4831
douhui4831 不起作用。
大约 3 年之前 回复
dpyic24480
dpyic24480 像这样$ var = $ _POST ['name']
大约 3 年之前 回复
duanjiao4763
duanjiao4763 如何从中获取输入数据到我的PHP代码?
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐