简体   繁体   English

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

[英]How do I pull data from a form into a session using an out of form input

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. 我正在使用PHP生成表单并根据需要填充页面。

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. 并且可变数量的表单可以出现在页面的其余部分,通常只是一个变量进程按钮以传递给php后端。

I keep running into issues passing the customer data into a post format so it can be processed by the php back-end. 我一直在遇到将客户数据传递到帖子格式的问题,因此可以通过php后端进行处理。 I have tried with javascript 我试过用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. 但是这不会出现在我收到的POST请求中。 What would be the best way to get that request through without having to do a major overhaul to the code structure? 在不必对代码结构进行大修的情况下,获得该请求的最佳方法是什么?

Are you able to add the hidden "cust-name" input to the html, or is this something that you are unable to modify? 您是否能够将隐藏的“cust-name”输入添加到html中,或者这是您无法修改的内容?

According to MDN ( https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute ) using setAttribute to set the value works inconsitently. 根据MDN( https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute )使用setAttribute来设置值不一致。 You should try this instead: 你应该试试这个:

cust_name.value = data[0];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Angular根据表单输入从MongoDB中提取数据 - Using Angular to pull data from MongoDB based on form input 如何使用赛普拉斯将数据输入到 iframe 的表单输入中? - How do I enter data into a form input in an iframe using cypress? 如何将JavaScript数据提取到表单输入字段中 - How to pull javascript data into a form input field 如何通过HTML表单输入文本并使用Javascript从JSON文件中检索数据作为响应? - How do I input text via an HTML form and retrieve data from a JSON file as a response using Javascript? 如何从MySQL数据库中提取数据以在页面加载时预先填充表单? - How do I pull data from my MySQL database to pre populate a form on page load? 如何从AngularJS表单输入中制作一个永久变量? - How do I make a permanent variable out of AngularJS form input? 如何使用Vue从表单获取所有数据 - How do I get all data from a form using Vue 如何使用表单输入数据更新我的 API 数据? - How do I update my API data using form input data? 删除并重新添加表单元素时,如何防止表单输入数据被重置? - How do I prevent form input data from being reset when I remove and re-append the form element? 如何从一种形式的输入字段中获取数据,并使用JQuery使用它以另一种形式填充相同的输入类型? - How can I take data from one an input field of one form and use it to populate the same input type in a different form using JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM