dongsu1539 2016-07-06 18:30
浏览 36
已采纳

如何使用表单输入将表单中的数据拉入会话

I am limited by pre-existing constraints and need to keep the structure the same for this project. I am generating forms using PHP and populating the page as needed from them.

I have a customer information form

<form action="index.php" method="POST" id="cust-form">
    <input type="text" name="cust-name"></input>
    <input type="submit" value="submit" name="submit">
</input>

Below that I have a search form

<form action="index.php" method="POST" id="form">
    <input type="text" name="search-tags"></input>
    <input type="submit" value="submit" name="Search">
</input>

And a variable amount of forms can appear on the rest of the page, normally just a variable process button to pass to a php backend.

I keep running into issues passing the customer data into a post format so it can be processed by the php back-end. I have tried with javascript

var form = document.getElementById('form');
var data = $('#cust-form').serialize();
data.split('&');
var cust_name = document.createElement("input");
cust_name.setAttribute("type", "hidden");
cust_name.setAttribute("name", "cust-name");
cust_name.setAttribute("value", data[0]);
form.appendChild(cust_name);

But that wont appear in the POST request that I receive. What would be the best way to get that request through without having to do a major overhaul to the code structure?

  • 写回答

1条回答 默认 最新

  • duanrong5167 2016-07-06 18:42
    关注

    Are you able to add the hidden "cust-name" input to the html, or is this something that you are unable to modify?

    According to MDN (https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute) using setAttribute to set the value works inconsitently. You should try this instead:

    cust_name.value = data[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题