T. Hegemann T. Hegemann - 1 year ago 61
PHP Question

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">

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">

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();
var cust_name = document.createElement("input");
cust_name.setAttribute("type", "hidden");
cust_name.setAttribute("name", "cust-name");
cust_name.setAttribute("value", data[0]);

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?

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download