简体   繁体   中英

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.

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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM