Jump to content

Trying to add form fields on the fly with ajax, but data not in $_POST global


noochies

Recommended Posts

Hello, here is my problem.

 

I want to add input fields to a form depending on what is selected in another drop down input field. I have this part working. However, when I submit the form, I don't get any of the additional data in the $_POST variable.

 

I'll give you an example.

 

This is a service ticket type application. When someone is entering a ticket, they choose the category of help they need from a drop down box. Examples are 'Hire/Exit', 'Software Purchase', 'Password Problems'.

 

Depending on what category they pick, I'm using AJAX with the drop down box onChange event to write in additional input fields specific to that category. For example, if they choose 'Software Purchase', I want to add input fields for the name of the software, the quantity, and the chartfield information for purchasing, to the form for the user to fill in.

 

But when I try to test this and I hit submit, I get all the other form element data except the data from the input elements I dynamically added (with AJAX) in the $_POST global variable.

 

Is this even possible?

Link to comment
Share on other sites

This is possible..

Try making a div, and then use a loop for depending on how many text fields you have.

You could also use the number of the loop to identify the text fields names.

 

I'm not the best of Ajax/Javascript scripters but here goes..

<script type="text/javascript>
function AddField(Type, Number, Div) {
var Element = document.getElementById(Div);
Element.innerHTML = '<form action="script.php" method="post">';
for(i=0;i<=Number;i++){
Element.innerHTML .= '<input type="' + Type + '" name="Field' + i + '">'; }
Element.innerHTML .= '<input type="submit">';
}
</script>

 

Sorry if it doesn't work.  ::)

Link to comment
Share on other sites

I'm not sure I see what you are trying to say....

 

I've got the application putting the fields into the div. I see the entire form and I can fill it out.

 

The problem is that those fields that I added dynamically, their data does not show up in the $_POST var when I submit.

Am I misunderstanding what you are trying to say?

Link to comment
Share on other sites

Well, I put together the simplest form possible with the simplest ajax possible, and it did work. So, yeah there must be something in my code that is messing something up. My elements have both names and ids so thats not it...

 

I guess now I'll just pick the code apart until I find out whats messing with it.

But at least I know that it DOES work!

 

Thanks for your help!

Link to comment
Share on other sites

Hello,  I recently implemented what you are trying to do as a solution for a product order form for our customer service reps.  Basically when they add a product, that product and all of its information is stored in a series of hidden fields.  Perhaps this snippet of code will help:

 

var prodsDiv = document.getElementById("addedProdsDiv");
prodsDiv.innerHTML += '<input type="hidden" name="idX'+prodCount+'" value="'+id+'" id="idX'+prodCount+'" readonly>';
prodsDiv.innerHTML += '<input type="hidden" name="prodX'+prodCount+'" value="'+product+'" id="prodX'+prodCount+'" readonly>';
prodsDiv.innerHTML += '<input type="hidden" name="priceX'+prodCount+'" value="'+price+'" id="priceX'+prodCount+'" readonly>';
prodsDiv.innerHTML += '<input type="hidden" name="qtyX'+prodCount+'" value="'+qty+'" id="qtyX'+prodCount+'" readonly>';
prodsDiv.innerHTML += '<input type="hidden" name="totalX'+prodCount+'" value="'+total+'" id="totalX'+prodCount+'" readonly>';

 

Don't worry about the prodCount, price, qty, and other variables in there.  Those are just passed into the function.  Specifically you may want to use the prodCounter idea though.  Basically each time this function is called it increments this prodCount hidden field by one, so I can have unique names for my hidden fields.  In case you are wondering about the readonly parameter, that prevents the user from modifying these hidden fields, even with something like FireFox web developer which gives you direct access to hidden fields.  Not sure if its IE compatible code, since the company I work for standardized on FireFox  8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.