Jump to content

Recommended Posts

I wasn't sure if this should go in the JavaScript or PHP forum, so I decided JavaScript.

 

I have created a site that has a form where the user can create as many rows as wanted. I really have no idea how I can then get this information to use in PHP.

 

Here is my JavaScript function creating the rows:

Code:

 

function addDollyRow() {

	numDollies++;
	document.getElementById("numDollies").value = numDollies;
	document.getElementById("removeDolly").disabled = false;


	var dollyTable = document.getElementById('dollyTable');

	var lastRow = dollyTable.rows.length;
  		// if there's no header row in the table, then iteration = lastRow + 1
  		var row = dollyTable.insertRow(numDollies);
  	
  		// two blank cells
  		var blankCellOne = row.insertCell(0);
  		var blankCellTwo = row.insertCell(1);	
  		
  		// dolly list
  		var dollyListCell = row.insertCell(2);
  		var dollyListSel = document.createElement('select');
  		dollyListSel.name = 'dollyListRow[]';
  		dollyListSel.id = 'dollyList' + numDollies;
  		dollyListSel.options[0] = new Option('','-1');
  		dollyListSel.options[1] = new Option('Regular Duty Dolly Set - Collins-4.80 \'C\'','1');
  		dollyListSel.options[2] = new Option('Heavy Duty Dolly Set - Collins-5.70 \'C\'','2');
  		dollyListSel.options[3] = new Option('Super Duty Dolly Set - Collins-5.70 \'D\'','3');
  		dollyListCell.appendChild(dollyListSel);
  		
  		// dolly material list
  		var dollyMatCell = row.insertCell(3);
  		var dollyMatSel = document.createElement('select');
  		dollyMatSel.name = 'axleMaterial' + numDollies;
  		dollyMatSel.id = 'axleMaterial' + numDollies;
  		dollyMatSel.options[0] = new Option('','-1');
  		dollyMatSel.options[1] = new Option('Aluminum','1');
  		dollyMatSel.options[2] = new Option('Steel','2');
  		dollyMatCell.appendChild(dollyMatSel);
  		
	// quantity text cell
	var qtyCell = row.insertCell(4);
	var qtyInput = document.createElement('input');
	qtyInput.type = 'text';
	qtyInput.name = 'dollyQuantity' + numDollies;
	qtyInput.id = 'dollyQuantity' + numDollies;
	qtyInput.size = 3;
	qtyCell.appendChild(qtyInput);
}

I read something about naming each element something such as 'el[]' and then iterating through it on the PHP side, but that didn't make much sense to me. I have only been using PHP for a few weeks now so I'm pretty new. Thanks for the help!

Link to comment
https://forums.phpfreaks.com/topic/203317-get-data-from-dynamic-rows-to-php/
Share on other sites

I read something about naming each element something such as 'el[]' and then iterating through it on the PHP side, but that didn't make much sense to me.

 

That's indeed one way to do it. What that means is that you send an array using square brackets in the name attribute.

 

For example if you have to following fields in your form:

<input name="el[]" val="1" />
<input name="el[]" val="2" />
<input name="el[]" val="3" />
<input name="el[]" val="4" />

 

You can then loop through these values like so:

<?php
foreach($_POST['el'] as $item) {
    echo $item;
}

 

 

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.