Create an Array, echo the results of the array

Hello experts,

"...And by the way, I'm new at programming."

I created a PHP/MySQL/Javascript file. The javascript allows a user to dynamically add more form select elements. The select fields are dynamically populated by a MySQL database, using PHP. I want to create an array of the select forms, so I can echo out all of the values associated with them.

Here is my basic code:
<select style="width: 300px" name="" id="name">
	<?php while($row = mysql_fetch_array($result)){
	?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name']; ?></option>
	<?php
	}
	?>
</select>

Open in new window


So, for instance, the user clicks "add". An identical form element to the one above is created dynamically. I want to be able to increase the name of the form element by one, each time a user clicks the "add" button. So, if the first select element's name is "name", then when the user clicks on the "add" button to add another select element, its name becomes "name-1". If the user clicks on the "add" button again, then the select element's name becomes "name-2".

How would I determine how many elements a user added and how would I echo out all of their values??
JdizzleextremeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Instead of incrementing the name by 1 instead d othis

<select style="width: 300px" name="names[]">

Open in new window


Now you can create as many dynamically as you like and when you post the form back you will get an array of values i.e.
<?php
$names = $_POST['names'];
print_r($names);
?>

Open in new window

$names will now contain an array of all the <select> with name="name[]"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawel WitkowskiSenior Javascript DeveloperCommented:
Basically code bellow assumes that you have at least one form existing in document. This way its simplest for you I think. Otherwise you would need to do some more coding. This way you can clone whole container, so If you want to have also a button with addSelect() function inside you could simply clone a div element. Here is how I see it:

<div id="firstSelect">
<select style="width: 300px" name="name-1" id="name">
	<?php while($row = mysql_fetch_array($result)){
	?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name']; ?></option>
	<?php
	}
	?>
</select>
<button onclick="addSelect()"></button>
</div>

<script type="text/javascript">
var selectToClone = document.getElementById("firstSelect");
var index = 2;
function addSelect() {
   var newSelect = selectToClone.cloneNode(true);
   selectToClone.parentNode.appendChild(newSelect);
   newSelect.setAttribute("name", "name-" + (index++));
}
</script>

Open in new window

0
Chris StanyonWebDevCommented:
Go with Julian's suggestion - name the SELECTs as an array!

If you're going to be cloning, then remove the ID - an ID needs to be unique!
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ray PaseurCommented:
You're creating a time bomb by using MySQL.  Get off it, and choose one of the other data base extensions.  This article tells why MySQLis being removed from PHP and what you must do to keep your scripts running in the future.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Ray PaseurCommented:
Looking at your "basic code" above, (and without any test data, which is the key element that is missing from this question) one could guess that the PHP would generate something like this:
<option value="1">Bill</option>
<option value="2">Jack</option>
<option value="3">Jill</option>

Open in new window

With those options you might expect that the HTML would submit some information that your PHP action script could use, but it would not.  The reason that nothing will come through to the action script is because the name= attribute of the select tag is empty.  You will want to walk before you run on this exercise.  Try changing things a small bit at a time and see if you can get the HTML and PHP correct first.  Once that's going, then try adding the JavaScript.  Your select tag will work better if you write it like this:
<select style="width: 300px" name="xyz" id="name" multiple>

Open in new window

Once your HTML has submitted the form to the action script, use this to see the contents of the request:
<?php var_dump($_REQUEST);

Open in new window

Also, please learn about the SSCCE.  It will be easier for you to get answers that focus on the central issues of the question if you deconstruct the problem into its component parts and show us (1) what data you have, and (2) what output you want to get.
0
Slick812Commented:
greetings  Jdizzleextreme, ,  you ask -  "How would I determine how many elements a user added and how would I echo out all of their values??"
As you have described your setup, you can use javascript to add whatever inputs you want to any form, and you can use the JS examples here to add a SELECT to your FORM, , However, these SELECT will always be empty!, you can not use PHP (server side) to populate the Options from a SELECT database retrieve.
You more or less have two options to populate the options,
ONE: to load javascript arrays from PHP SELECT that can populate the Options for ALL user additions made
OR
TWO: to use a javascript AJAX to contact the server and get back the SELECT database retrieve.

AJAX is not so easy, but it is used to good effect on many web sites now.
0
RobOwner (Aidellio)Commented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
Ray PaseurCommented:
No offense meant, but this process is a waste of time if the parties involved do not understand the technologies involved.  We are just trying to help other member of EE, and the closing + review + repeat + admin detracts from the experience.  A lot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.