Need help with dynamically created form fields and $_POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Need help with dynamically created form fields and $_POST

Post by spedula »

Okay, I'm really new at this and need some help.

I wrote a javascript script that creates form fields dynamically (onClick button) and appends a number to their ID, so each newly created field has a unique ID. I'm using POST to pass the values of these fields. So far I have only been able to get the script to tell me how many fields were created. Now I'm stuck here.

The ultimate goal that I need to reach is for the script to recognize how many fields were created and create a unique variable for each field that holds the value of what ever was entered into each respective form. and then creating a variable that holds all those variable's as a string with HTML line breaks in it so that it can be read correctly when echo'd or emailed

I have no idea how to do this and have been messing with it for more than 8 consecutive hours now. Here is what I have so far and honestly. I have no idea how I even got this far, I'm just learning PHP now.

Code: Select all

$getURLnumber = array();
	$urlValue = $_POST['numberofurl'];
	while($urlValue > 0)
	{
		$i++;
		$URL = 'URL';
		$getURL = $URL.$i;
		$getURLnumber[] = $_POST[$getURL];
		$urlValue--;
	}
any help is appreciated here, I'm not asking you to write it for me, but just a push in the write direction. Thanks.

EDIT:

I think I got it.

Code: Select all

foreach ($getURLnumber as $permURL)
	{
		$p++;
		$URL.$p = $permURL;
	}
But now my issue is that I need to email this info. I can of course write something like this:

Code: Select all

$URLall = $URL. '<br><br>' .$URL1. '<br><br>' .$URL2. '<br><br>' .$URL3. '<br><br>' .$URL4. '<br><br>' .$URL5. '<br><br>' .$URL6. '<br><br>' .$URL7. '<br><br>' .$URL8. '<br><br>' .$URL9. '<br><br>' .$URL10. '<br><br>' .$URL11. '<br><br>' .$URL12. '<br><br>' .$URL13. '<br><br>' .$URL14. '<br><br>' .$URL15. '<br><br>' .$URL16. '<br><br>' .$URL17. '<br><br>' .$URL18. '<br><br>' .$URL19. '<br><br>' .$URL20. '<br><br> This Message was sent to you from: ' .$email;
but that's just sloppy and far from elegant. There has to be an easier way to do this, and one that doens't have a limitation based on how many times I feel copy and pasting that.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with dynamically created form fields and $_POS

Post by requinix »

You're going about this the wrong way. Very wrong way. Use arrays.

For starters, if you name the input field with a []

Code: Select all

<input type="text" name="field[]">
then $_POST["field"] will be an array.

After that there are a lot of functions available. Like implode.
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Need help with dynamically created form fields and $_POS

Post by spedula »

I see what you're saying. Here's my issue with putting "field[]" as the name though:

the name field is created with javascript when the user clicks "Add new field", and then given it's name with javascript as well. Should I just go ahead and make every single new field created having the same name (field[])?

If so, I only guessing that they will be appended numbers automatically?
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Need help with dynamically created form fields and $_POS

Post by spedula »

Oh wow,

it works 100% now.

Thank you very much! :)
Post Reply