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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?