Link to home
Start Free TrialLog in
Avatar of Jdizzleextreme
Jdizzleextreme

asked on

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??
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
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.