提交具有非标准POST数据格式的表单

克里斯蒂安·梅恩(Christian Mayne)

我在PHP页面中嵌入了一个标准HTML表单,该表单张贴到第三方托管和管理的外部Job Application API中。问题在于,它们的形式不会像往常一样加载$ _POST参数,而是希望使用以下格式的单个$ _POST参数(“ params”):

params="Prop2:title;Prop4:lastname;Prop6:firstname;"

依此类推-大约有30个字段。

如何采用输入形式并将其转换为单个长字符串?这是否需要某种形式的介于提交和发布之间的中介步骤(在这种情况下,我将如何进行中介步骤?),或者我是否在这里错过了一些更基本的东西?

下面的示例表单适用于已预先提交了所需参数的测试textarea输入,但是显然忽略了实际的firstname lastname输入。

<form id="#applicationForm" method="POST" action="https://www.example.com/eternal-api">

  <input id="lastname" name="lastname" type="text" placeholder="Last name (Required)" class="input-xlarge" required="">
  <input id="firstname" name="firstname" type="text" placeholder="First Name (Required)" class="input-xlarge" required="">
  <textarea name="params" rows="2" cols="150">Prop4:Test_lastname;Prop6:test_firstname;</textarea>
  <input type="submit" value="Submit Application" class="btn btn-success">

</form>

令人困惑的是,一些输入是上载字段,但是接下来我会担心的!

模糊数学

我会尝试一个非常基本的方法:

$('#applicationForm').submit(function(e) {

  var params = '';

  $(this).find('input, textarea, select').filter('[name]').each(function() {

    if ($(this).is('textarea')) {

      params += $(this).val();
      return;

    }

    params += $(this).attr('name') + ':' + $(this).val() + ';';


  });

  $(this).append($('<input>', {

    type: 'hidden',
    name: 'params',
    value: params

  }));
  

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="applicationForm" method="POST" action="https://www.example.com/eternal-api">

  <input id="lastname" name="lastname" type="text" placeholder="Last name (Required)" class="input-xlarge" required="">
  <input id="firstname" name="firstname" type="text" placeholder="First Name (Required)" class="input-xlarge" required="">
  <textarea name="params" rows="2" cols="150">Prop4:Test_lastname;Prop6:test_firstname;</textarea>
  <input type="submit" value="Submit Application" class="btn btn-success">

</form>

我循环遍历每个元素并收集值。然后我在隐藏的输入中附加您的值

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章